What is the better pratice: duplicate code or use goto statement?

I'm studing C# now, and came across the following situation, what's the better pratice, duplicate the code like "EX 1" or use goto statement like "EX 2"? I don't want a personal opnion.

        // EX 1: 

        switch (a)
        {
            case 3:
                b = 7;
                c = 3; // duplicate code <-|
                break; //                    |
            case 4:    //                    |
                c = 3; // duplicate code --|
                break;
            default:
                b = 2;
                c = 4;
                break;
        }


        // EX 2: 

        switch (a)
        {
            case 3:
                b = 7;
                goto case 4; // not duplicate code and use goto statement
            case 4:
                c = 3;
                break;
            default:
                b = 2;
                c = 4;
                break;
        }

Example 1 pros and cons

+ Common structure
+ Simple to understand logic
- More lines of code
- Code repetition

Example 2 pros and cons

+ Fewer lines of code
+ No code repetition
- It complicates logic
- It is not commonly used in production code

Bottom line

I would prefer example 1, because, in this particular instance, the savings are minimal, but the logic gets more complicated. Goto , arguably, increases the chances of a bug if more people starts working on the same code, as difficulty of code increases. Let's have a look at this embarrassing bug. Had developers not used a goto , there wouldn't be such a problem!

Bonus points

  • You can use enums for case selection, so case 3: => case CarPart.SteeringWheel
  • make sure each case has a break;
  • make sure there is a default case
  • consider using polymorphism and inheritance instead of a switch case

    ICarPart part1 = new SteeringWheel();
    ICarPart part2= new Mirror();
    var parts = new List<ICarPart>() {part1, part2};
    
    // now call your original method on the parts
    // no more need for a switch case 
    

  • It really depends.

    Is case 3 a special case of case 4?

    In that situation then a goto may be in order, because if we at a later point in time add some new behaviour to case 4 then we will get that automatically for case 3 as well.

    If case 3 & 4 are unrelated, then duplicating code is better.

    If your real case is this small, with so few lines, I would prefer duplicating code, because of simplicity and readability.


    I personally dislike goto since it makes your code less easy to understand and reproduce.

    I see no major issues with your first code sample as it is. If you need to, you could also split processing of b and c if that makes sense.

    You should consider what is more important:

  • Code readability;
  • Minimal number of lines of code;
  • How often does this code change? When it is changing a lot and the dependency might get lost, you probably don't want to use goto .
  • 链接地址: http://www.djcxy.com/p/84500.html

    上一篇: 无法将“默认”置于切换块

    下一篇: 什么是更好的实践:重复代码或使用goto语句?