Exploring the Hesitancy of Skilled Programmers to Use 'if' Statements
Written on
Chapter 1: Introduction to If-Less Programming
The trend of if-less programming encourages developers to minimize the use of ‘if’ statements in their code. While this philosophy is tied to the principles of object-oriented programming, many developers, especially those with limited experience, find themselves wrestling with complex nested if structures when tasked with modifying or refactoring existing code. This challenge is often heightened in legacy systems or non-OOP languages that rely heavily on global variables.
For instance, when a product owner requests a change during a meeting, it often leads to a situation where the scrum manager, while perhaps amused, is secretly dreading the technical discussions that follow. Such requests are generally part of an ongoing cycle of improvements and bug fixes that software requires.
Frequently, the original developers who understood the code intricately are no longer available, either reassigned or departed from the company. Although documentation exists, it is typically outdated, leading to confusion for the developers who have to implement the changes.
Faced with urgent deadlines—perhaps due to a marketing event or an important client meeting—the developer struggles to grasp the necessary modifications without sufficient guidance.
Section 1.1: The Struggle with Legacy Code
The developer often starts their work in the Integrated Development Environment (IDE), attempting to identify where edits are needed. They may make tentative changes and test them using the debugger to see how these alterations affect the overall functionality. Instead of halting to discuss a comprehensive refactoring with their supervisor, they often settle for a quick fix, introducing cascading ifs based on previous modifications.
The code gets pushed to continuous integration, and if they are fortunate, the unit tests pass. However, integration tests are less likely to succeed since the new functions were not initially included in the testing suite. As the developer marks the ticket as resolved, the testers—often uninformed about the changes—quickly verify and close the ticket.
Section 1.2: The Ripple Effect of Changes
As time passes, the initial change can lead to unforeseen issues. For example, one of the numerous reports generated by the software might not reflect the desired outcomes due to the modifications. This prompts the scrum master to pass the issue to another developer, who may add yet another layer of if statements to address the new requirements.
This cycle of updates can proliferate ifs throughout the code, leading to a tangled web of conditions as developers scramble to address each new issue.
Chapter 2: Long-Term Consequences of Excessive If Statements
The first video, "If Your Code Looks Like This... You're A GOOD Programmer," explores the nuances of coding practices and how they can impact long-term maintenance.
The second video, "EVERYTHING About Learning to Code is F**KED!" discusses the challenges new coders face in understanding the complexities of programming.
As years progress, changes accumulate, leading to bloated methods. A function that began as thirty lines of code may swell to one hundred and fifty, filled with nested if statements for every possible scenario. This complexity can create a chaotic environment, often worse than spaghetti code, where modifying one section requires alterations across multiple areas.
In retrospect, the new functionality could have been better integrated if the developers had analyzed the requirements more thoroughly. A strategic approach, such as creating subclasses, could have alleviated the need for excessive if statements, promoting cleaner and more maintainable code.
To truly enhance code maintainability, programmers should strive to limit the use of if statements to essential scenarios, encapsulating them within dedicated methods rather than scattering similar conditions throughout the codebase.
In conclusion, while eliminating if statements won't magically resolve all issues, minimizing their use can lead to more agile and maintainable code. Thoughtful programming practices, including the use of polymorphism and clear documentation, can mitigate the risks associated with convoluted logic.
For further insights, consider subscribing to the newsletter at PlainEnglish.io. Join our community on Twitter and LinkedIn, and connect with us on Discord for more discussions.