What is the benefit of terminating if … else if constructs with an else clause?

前端 未结 12 1069
不思量自难忘°
不思量自难忘° 2020-11-27 13:37

Our organization has a required coding rule (without any explanation) that:

if … else if constructs should be terminated with an el

12条回答
  •  没有蜡笔的小新
    2020-11-27 13:51

    This is the equivalent of requiring a default case in every switch.

    This extra else will Decrease code coverage of your program.


    In my experience with porting linux kernel , or android code to different platform many time we do something wrong and in logcat we see some error like

    if ( x < 0 )
    {
        x = 0;
    }
    else if ( y < 0 )
    {
        x = 3;
    }
    else    /* this else clause is required, even if the */
    {       /* programmer expects this will never be reached */
            /* no change in value of x */
            printk(" \n [function or module name]: this should never happen \n");
    
            /* It is always good to mention function/module name with the 
               logs. If you end up with "this should never happen" message
               and the same message is used in many places in the software
               it will be hard to track/debug.
            */
    }
    

提交回复
热议问题