The Three Laws of Readable Code
Writing readable code is crucial for collaboration and maintainability. Inexperienced developers often create code that is difficult to understand. This article outlines three fundamental laws for writing code that is easy to read, understand, and modify.
Law 1: Avoid Deep Nesting
The Problem with Deeply Nested Code
Deeply nested code is a common sign of inexperience. The more levels of nesting, the harder it is to keep track of the conditions that lead to a specific block of code. By the time you reach the core logic, your brain is taxed, making understanding the code's purpose difficult.
Using Inversion to Reduce Nesting
One technique to simplify nested code is inversion. Instead of executing inner logic only if a condition is not met, you can check if the condition is met and exit early. This effectively collapses the nested structure by one level. Repeat this process for other conditionals to flatten the code.
Benefits of Reduced Nesting
When conditions are handled upfront, readers no longer need to hold those conditions in their memory while trying to understand the main logic. Once the code executes past a certain point, previous conditions can be safely discarded, allowing for a clearer focus on the subsequent code block.
Merging Related If Statements
Another optimization is to merge related if
statements. For example, authentication and authorization checks can often be combined, though this may sacrifice some granularity in logging or error handling.
Code Extraction
Extraction involves moving complex logic into separate methods or functions. This is especially useful for if
statements with complicated conditions. Giving the extracted function a descriptive name improves readability. The main function becomes a summary of the code's overall functionality.
Law 2: Avoid Code Duplication
The Problem with Code Duplication
Code duplication poses maintainability problems. If a change is required in a duplicated section, developers must search the entire codebase to find and modify every instance. Overlooking one instance can lead to bugs and inconsistencies.
Extracting Duplicated Logic
The solution is to extract duplicated code into a shared function or method. Both parent functions can then call this shared function. Any changes only need to be made in one place, reducing errors and improving maintainability. This also makes the code less convoluted and more readable.
Law 3: Use Meaningful Naming
The Importance of Good Naming
Obscure or cryptic naming is a significant barrier to code readability. The third law emphasizes that names should be meaningful to anyone who might read the code.
Following Naming Conventions
While various naming conventions exist, the key is to be consistent and choose names that clearly reflect the purpose of variables, functions, and classes. Meaningful names make the code's intent immediately clear. Revising code with unclear names to use more descriptive alternatives greatly improves readability and comprehension.