Profile photo for Jacques du Rand

There are many many good ones already mentioned !
My little gem is the "ArrowHead Anti-Pattern"

Try to reduce NESTED IF's

  1. It goes like this: 
  2. if(a > 0){ 
  3. //one or two lines simple code (updated) 
  4. if( b > 10 ){ 
  5. //one or two lines simple code (updated) 
  6. if(c > 0) { 
  7. //complex code 
  8. } 
  9. } 
  10. } 
  11.  
  12. See the arrowhead above ? 
  13.  
  14. Rather I prefer: 
  15. if (a < 1 ){ 
  16. return 
  17. } 
  18. if (b < 11 ){ 
  19. return 
  20. }  
  21. if (c < 1 ){ 
  22. return 
  23. }  
  24. //complex code 
  25.  
  26. BUT ! 
  27. Steve Barnes below has an even better solution that takes care of the early returns 
  28.  
  29. bool OK = FALSE; 
  30. OK = (a > 0); // Comment why 
  31. OK &= (b > 10); // Comment why 
  32. OK &= (c > 0); // Comment why 
  33. if OK { // Good to go 
  34. // Do your stuff 
  35. } 
  36. return; 
View 100+ other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025