javascript - If statements in a function -


is doing this:

function number(a, b){     if(a + < 1) return + + b;      if(a + > b) return + - b;      return + i; } 

considered bad practice opposed this?:

function number(a, b){     if(a + < 1){         return + + b;     }else if(a + > b){         return + - b;     }else{         return + i;     } } 

i think without curly braces, might run problems either linters or minifiers. other that, it's personal taste. people argue "no braces" approach because of brevity, while people prefer curly braces readability (this option i'm in favor of).

as far consistency, it's easier stick using curly braces, because won't have single-line if blocks.

as far not using else, that's fine when there's return statement embedded in block. if there's not return inside block, , both if conditions met, it'll end running both sets of code. have unintended side effects. example:

if(a) dosomething(); if(b) dosomethingelse(); 

can different (or similar to):

if(a) {     dosomething(); } else if (b) {     dosomethingelse(); } 

so answer depends on several things, such style preference (individual , team), technologies minifiers , linters, , intended execution.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -