JavaScript is a very forgiving language. It’s easy to write code that runs but has mistakes in it.
In this article, we’ll look at the best practices for writing unusual control structures and boolean expressions in our code.
Recursion
Recursion is when a function calls itself. We may want to do that to do operations on tree structures.
We can also use them in place of loops.
When we write recursive code, we’ve to consider a few things.
Make Sure the Recursion Stops
Infinite recursion will probably crash our program. Therefore, we should make sure that recursion ends.
We should make sure that our recursion code has a base case.
Use Safety Counters to Prevent Infinite Recursion
If our recursive code doesn’t have a clear base case, we should add a safety counter to our code to stop infinite recursion.
For instance, we can write it as follows:
const fn = (safetyCounter = 0) => {
if (safetyCount === 100) {
return;
}
//...
fn(safetyCounter + 1);
}
We increment safetyCounter
by 1 each time we call fn
until it reaches 100, then it stops.
Limit Recursion to One Function
We should never write code that is cyclically recursive. So code where A calls B, B calls C, and C calls A isn’t good.
They’re hard to detect and we can run into problems with infinite recursion easily.
Therefore, we should just have a function call itself and nothing else.
Keep an Eye on the Stack
The call stack is where all the functions that are called are listed in one collection in LIFO order.
We got to make sure that we don’t overflow that with our recursive code.
A safety counter makes sure that we don’t overflow the stack.
Don’t Use Recursion for Factorials or Fibonacci Numbers
Recursion for factorials or Fibonacci numbers is slower than with iteration.
Also, the memory use of these operations required for computing Fibonacci numbers and factorials are unpredictable.
It’s also a lot harder to understand than the iterative version.
Table-Driven Methods
Table driven method is a schema that lets us look up information is a table rather than logic structures.
We can use that to rewrite logic in a much easier way in situations we can look up one thing from another.
To do this with JavaScript, we can use a map or an object.
For instance, we can write the following to look up the day of the week by a number:
const dayMap = {
0: 'Sunday',
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
}
This lets us look up the day of the week by writing:
dayMap[1]
to get Monday.
Using true and false for Boolean Tests
If we do boolean tests, we should use true
and false
to compare our boolean expressions.
Most people won’t know other values are opposites better than true
and false
.
Since JavaScript has a boolean type, we can use that easily.
Compare Boolean Values to true and false Implicitly
We can compare booleans without writing out true
or false
.
If it’s not written out, then we compare it against true
. For instance, if we have:
if (numApples > 1) {
//...
}
that’s the same as:
if (numApples > 1 === true) {
//...
}
Break Complicated Tests into Partial Tests with New Boolean Variables
We shouldn’t create long boolean expressions for checks.
If we have to check many conditions, then we should assign some expressions to their own variables and use those.
Move Complicated Expressions into Boolean Functions
Moving complicated expressions into boolean functions is another way to make long boolean expressions easier to read.
It works the same way as assigning items to variables.
For instance, we can write the following to put our expression into the boolean function:
const isInRange = (val) => {
const MIN = 1;
const MAX = 10;
return val >= MIN && val <= MAX;
}
Since the function has a name, it’s easier to check what those expressions do from the name than from the boolean expressions.
Conclusion
Recursion shouldn’t be used for everything. If there’s an iterative solution to a problem, then we should use that.
If we’re booleans, then we shouldn’t compare values with true
or false
directly.
Finally, we can use a lookup table object instead of using conditional statements to check simple items.