B.sc from University of Pretoria (Graduated 2005) · Upvoted by , Started programming 8 bit computers in 1981 and , M.S. Computer Programming & Information Technology, Birla Institute of Technology and Science, Pilani (2012) · Updated 9y ·
There are many many good ones already mentioned !
My little gem is the "ArrowHead Anti-Pattern"
Try to reduce NESTED IF's
- It goes like this:
- if(a > 0){
- //one or two lines simple code (updated)
- if( b > 10 ){
- //one or two lines simple code (updated)
- if(c > 0) {
- //complex code
- }
- }
- }
- See the arrowhead above ?
- Rather I prefer:
- if (a < 1 ){
- return
- }
- if (b < 11 ){
- return
- }
- if (c < 1 ){
- return
- }
- //complex code
- BUT !
- Steve Barnes below has an even better solution that takes care of the early returns
- bool OK = FALSE;
- OK = (a > 0); // Comment why
- OK &= (b > 10); // Comment why
- OK &= (c > 0); // Comment why
- if OK { // Good to go
- // Do your stuff
- }
- return;
512.7K views ·
View upvotes
· 1 of 128 answers
Something went wrong. Wait a moment and try again.