Sharper Minds Technologies has release their first Android game, Hexaddicus. Hexaddicus is a mixture of mental math, memory, and puzzle solving. The game feature two modes of play, a timed mode and a puzzle mode.
In a timed game players try to connect numbers together that add up to a target that changes after each turn. As the player gets better the target get harder. In the lite version, there is a short and long variation of the time mode, whereas the full version features four modes.
Hexaddicus also features a puzzle mode where players must use every number on the board. This mode has elements similar to Sudoku and a word searches. There are five levels of difficulty ranging from very easy, which should take less than a couple minutes, to very hard, which could take an hour or more.
It’s amazing at how developers can abuse the most basic construct in almost any language, the if-then-else clause. First, let’s examine a pretty common block of language independent code.
0 simple-method (args...) 1 if cond then 2 some code here 3 if cond then 4 more code here 5 if cond then 6 deeply nested code 7 else 8 more deeply nested code 9 return code
Harmless and easy to read, right? The answer is mostly dependent on the code inside the if-then-else clauses, but, even with that aspect aside, this code can be much simpler. For a human to understand a function they must understand all paths that lead to all exits. So, for this function there is only one exit on line 9 but 4 ways to get there. As an exercise try to find all of paths to line 9.
0 simple-method (args...) 1 if cond is false then return code # 1 way to get here 2 some code here 3 if cond then 4 more code here 5 if cond then 6 nested code 7 return code # 2 was to get here 8 not so deeply nested code 9 return code # 1 way to get here
Return from a method as soon as you can.
There are other nice perks to this way of thinking which include…
- Tighter variable scopes
- Less horizontal indentions which means easier to scan
- Fewer bugs in future edits by reducing the chance of corrupting a critical path
In summary, exit as soon as possible using negative conditions if needed. If highly nested logic is in fact needed considering moving the inner blocks to a new function for readability and to keep the variable scope as small as possible.