开发者

Best practice for passing parameters, which can take two values

开发者 https://www.devze.com 2023-03-19 08:01 出处:网络
How to write better? public void Foo(bool isStart) { // Code [Common] if (is Start) { // Code [Start] } else {

How to write better?

public void Foo(bool isStart) {
   // Code [Common]
   if (is Start) {
      // Code [Start]
   } else {
      // Code [End]
   }
   // Code [Common]
}

or

public enum MyEnum {
   Start, End
}
public void Foo(MyEnum param) {
   // Code [Common]
   switch (param) {
      case MyEnum.Start:
         // Code [Start]
         break;
      case MyEnum.End:
         // Code [End]
         break;
   }
   // Code [Common]
}

Update:开发者_如何学C I'm looking for a small solution. "Common", "Start" and "End" parts are very short, I do not want to split Foo into several methods.


How about:

public class Foo
{
    public void Start()
    {
        PreCommon();

        // Code [Start]

        PostCommon();
    }

    public void Stop()
    {
        PreCommon();

        // Code [Stop]

        PostCommon();
    }

    private void PreCommon()
    {
        // Code [Pre-Common]
    }  

    private void PostCommon()
    {
        // Code [Post-Common]
    }    
    ...

}

Methods that have a single responsibility are easier to read, easier to understand and easier to maintain.


it just depends on the situation and your approach. for instance YAGNI says you aren't going to need the enum so might as well stick with the bool. but then again if you know you are going to need it, or think you may, then probably the second is the way to go. OR, really if you are going for something that's more expressive, i like the second way better because it makes it obvious to the caller what is being set; true/false is not nearly as descriptive as MyEnum.Start and MyEnum.Stop.


I'm not sure what would be best, it would depend on a number of factors but using an enum in place of a bool like you did here is not the way to go.

Here's another option. Works well if you have more than two cases of code to work with (if that was your point of the enum).

public void Foo(Action unique)
{
   // Code [Common]
   unique();
   // Code [Common]
}

private void StartCode()
{
    // Code [Start]
}

private void EndCode()
{
    // Code [End]
}

// call it
Foo(StartCode);


My rule of thumb is to take a few minutes when I get to this situation, and really think about if I can make a justification in future scenarios or versions for a 3rd case (necessitating an enumeration or completely new type). If I cannot think of a 3rd case, I always go with bool because they're just easier to test.

I always name on the positive side of things and begin the propertyname with a form of 'to be'... such as "IsActive" or "HasChildren"


How about this ↓

abstract class FooBase
{
    public abstract void DoSomthingBegin();
    public abstract void DoSomthingEnd();

    public void Foo()
    {
        // Code [Common]
        DoSomthingBegin();
        DoSomthingEnd();
        // Code [Common]
    }
}

class FooBegin : FooBase
{
    public override void DoSomthingBegin()
    {
        Console.WriteLine("OnBegin");
    }
}

class FooEnd : FooBase
{
    public override void DoSomthingBegin()
    {
        Console.WriteLine("OnEnd");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消