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

丶灬走出姿态 提交于 2019-12-11 01:04:42

问题


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;
        }

回答1:


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 
    



回答2:


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.




回答3:


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.



回答4:


In general, using goto is considered to be bad practice (and rightfully so), but using goto solely for a forward jump out of structured control statements is usually considered to be OK, especially if the alternative is to have more complicated code.

Here's an example:

for (...) {
    for (...) {
        ...
        if (something)
            goto end_of_loop;
    }
}

end_of_loop:

Here you check some other acceptable usages of goto.

So, goto would be considered a bad practice. But, as I said, it still can be used.



来源:https://stackoverflow.com/questions/22689392/what-is-the-better-pratice-duplicate-code-or-use-goto-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!