relopmen.blogg.se

Simple minesweeper javascript - code
Simple minesweeper javascript - code











simple minesweeper javascript - code simple minesweeper javascript - code

If ( gameOver ) return // NOTE: logic check change. but no code after the block before returning from the function. 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

simple minesweeper javascript - code

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?













Simple minesweeper javascript - code