Is it necessary to write else part in every if condition?

后端 未结 11 1616
-上瘾入骨i
-上瘾入骨i 2021-02-19 05:16

The question I asked might be closed, But i just want to know that is it necessary to write else part of every if condition. One of my senior programmer said me that \"you shoul

11条回答
  •  一向
    一向 (楼主)
    2021-02-19 05:46

    I know I am late but I did a lot of thinking over this and wanted to share my results.

    In critical code, it is imperative for every branch is accounted for. Writing an else is not necessary, but leave a mark that else is not necessary and why. This will help the reviewer. Observe:

    //negatives should be fixed
    if(a < 0) {
        a+=m;
    }
    //else value is positive
    

提交回复
热议问题