A table-driven method is a scheme that allows you to look up information in a table rather than using logic statements (if and case) to figure it out.
Virtually anything you can select with logic statements, you can select with tables instead. In simple cases, logic statements are easier and more direct. As the logic chain becomes more complex, tables become increasingly attractive.
With a table-driven method, you must address how to look up entries, and what data should be stored in the table.
Direct Access Tables
A table-driven approach generates less code and is easier to change without the need to recompile.
Put a lookup key transformation in its own method to guard against different transformations in different places.
Stair-Step Access Tables
This puts the upper end of consecutive ranges into a table and works well with a binary search for larger lists.
Indexed Access Tables
Indexed access schemes offer two main advantages. First, if each of the entries in the main lookup table is large, it takes a lot less space to create an index array with a lot of wasted space than it does to create a main lookup table with a lot of wasted space.
The second advantage, even if you don’t save space by using an index, is that it’s sometimes cheaper to manipulate entries in an index than entries in a main table.
- Tables provide an alternative to complicated logic and inheritance structures. If you find that you’re confused by a program’s logic or inheritance tree, ask yourself whether you could simplify by using a lookup table.
- One key consideration in using a table is deciding how to access the table. You can access tables by using direct access, indexed access, or stair-step access.
- Another key consideration in using a table is deciding what exactly to put into the table.
Even if a complicated conditional expression is only used once, moving it into its own method is useful for improving readability.
Organize numeric tests so that they follow points on a number line.
Comparing a character against \0 instead of 0 reinforces that the expression works with character data instead of logical data.
Null statements are uncommon, so make them obvious, such as a comment inside the braces explaining why one is used.
Taming Dangerously Deep Nesting
Use a break block, try to flatten, move some of the nested blocks into their own methods, or use polymorphism.
Complicated code is a sign that you don't understand your program well enough to make it simple.
A Programming Foundation: Structured Programming
The core of structured programming is a simple idea that a program should use single-entry, single-exit control constructs.
- Making boolean expressions simple and readable contributes substantially to the quality of your code.
- Deep nesting makes a routine hard to understand. Fortunately, you can avoid it relatively easily.
- Structured programming is a simple idea that is still relevant: you can build any program out of a combination of sequences, selections, and iterations.
- Minimizing complexity is a key to writing high-quality code.