If there is no code after the if statement and the end of the function - just swap the logic and return early. and how do you know when something can be flattened? this simple refactoring reduced the depth of the if statements from ~5 to 1.many of the other functions could be flattened just like this. I may have missed something, but hopefully you get the point. If ( cell.opened || cell.flagged ) return So - your handleClick function could be rewritten (with stuff removed) as: meaning, if you find that you're nesting "ifs" more than a couple of levels deep, the code may be a candidate for flattening. It can be incredibly beneficial (for readability, catching logic errors, etc.) to "exit early" from "if" statements. Can i make a (hopefully useful) comment about programming style - something that someone shared with me a long time ago when reading my code that i have found to be very valuable over the years?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |