java - If-Else, return statement and local variable tradeoffs -


i have 3 ways write code, listed each pro's , con's best coding practice ? although code not important, meant return decimal number result if 0's stripped off decimal series, example: 11 == 10 0's eliminated.

1.pro's : no need local variables store return value. con's : multiple return statements.

public static int getstrippednumber(int num) {     int numberofdecsets;     if ((numberofdecsets = num % 10) == 0) {         return num - numberofdecsets;     } else {         return num - ( numberofdecsets + 1 );     } } 

2.pro's : no need local variables store return value. con's : multiple return statements.

public static int getstrippednumber(int num) {     int numberofdecsets;      if ((numberofdecsets = num % 10) == 0) {         // numbers 10, 20, 100 etc.         return num - numberofdecsets;     }      return num - ( numberofdecsets + 1 ); } 

doubt: advised use 'else' when 'if' returns ? option 1 uses else option 2 not use else. dont know whats right thing, , answers on conflicting in duplicate

3.pro: single point of return. con: variable used.

public static int getstrippednumber(int num) {     int numberofdecsets;     int returnval;      if ((numberofdecsets = num % 10) == 0) {         returnval = num - numberofdecsets;      } else {         returnval =  num - ( numberofdecsets + 1 );     }      return returnval; } 

one of collegues use adheres strictly "single point of return" style, coupled irrational fear of null values. means code ends looking this:

int contrived(type* a, type* b, int c) {     int ret = 0;     if (a != null) {         if (b != null) {             switch (c) {                 case 1:                      ret = 2;                      break;                 case 2:                      if (a == b) {                          ret = 3;                      }                      else {                          ret = 4;                      }                      break;                  default:                      ret = -1;                      break;             }         }     }     return ret; } 

i call triangle code: can see code becomes indented quickly. in contrived example i've struggled achieve 4 levels of indentation, in real life i've seen much worse!

compare own style, prefer check preconditions , return if fail, elide temporary variables:

int contrived(type* a, type* b, int c) {     if (a == null)         return 0;      if (b == null)         return 0;      switch (c) {         case 1:             return 2;         case 2:             return (a == b) ? 3 : 4;         default:             return -1;     } } 

with few well-placed comments, simplified indentation makes code more readable , easier maintain in opinion.

another 'pro' style making change less radically affect indentation, meaning fewer lines change when submitting source control. smaller change sets mean simple, discrete diffs (i.e. not lots of useless whitespace changes) , makes merges simpler in future.


Comments

Popular posts from this blog

ruby on rails - Is it the correct way to implement belongs_to relation with factory girl? -

geolocation - Windows Phone 8 - Keeping background location tracking active beyond four hours -

Uncaught TypeError: Cannot read property 'parentNode' of null javascript -