什么是更好的pratice:重复代码或使用goto语句? [英] What is the better pratice: duplicate code or use goto statement?

查看:135
本文介绍了什么是更好的pratice:重复代码或使用goto语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我STUDING C#现在,并在下面的情况来了,有什么更好的初步实践,重复,如EX 1的代码或使用像EX 转到语句2?
我不希望个人速递

  // EX 1:

开关(一)
{
案例3:
B = 7;
C = 3; //重复代码< - |
中断; // |
壳4:// |
C = 3; //重复的代码 - |
中断;
默认:
B = 2;
C = 4;
中断;
}


// EX 2:

开关(A)
{
案例3:
B = 7;
的goto case 4; //不重复的代码和使用goto语句
案例4:
C = 3;
中断;
默认:
B = 2;
C = 4;
中断;
}


解决方案

例1利弊



 
+通用结构
+简单的理解逻辑
- 代码
多行 - 代码重复



例2利弊



 
+更少代码
+无码重复
线 - 这复杂的逻辑
- 这是不常用的生产代码


$ b $使用b

底线



我宁愿实施例1,因为,在此特定情况下,节省是最小的,但逻辑变得更复杂了。 转到,可以说,增加了错误的机会,如果越来越多的人开始对相同的代码工作,为代码增加难度。让我们来看看的这种尴尬的错误。没有使用开发商转到,就不会有这样的问题!



加分




  • 您可以使用枚举的情况下选择,因此案例3: =>

  • 确保每个情况 $ C>有一个突破;

  • 确保有一个默认案例

  • 考虑使用多态和继承,而不是一个开关的情况下

      ICarPart第一部分=新的SteeringWheels(); 
    ICarPart第二部分=新的镜像();
    VAR部分=新的List< ICarPart>(){第一部分,第二部分};

    //现在请你对部分
    //一个开关的情况下
    不再需要原来的方法



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 
    

这篇关于什么是更好的pratice:重复代码或使用goto语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆