Practices I want to do more often:
Paying close attention to detail and writing code with care: The book emphasizes that clean code looks like it was written by someone who cares. This means not only focusing on getting the code to function correctly, but also ensuring it is easy to read, understand, and maintain.
Example: Instead of quickly writing a function with a generic name like processData(), I would take the time to give it a descriptive name such as validateUserInputAndSaveToDatabase(). This demonstrates care in making the code’s purpose clear.
Example: When writing a conditional, instead of using a complex and hard-to-read logic, I would use explanatory variables to break it down into easier-to-understand parts. For instance, instead of if ((user.getAge() > 18) && (user.getLocation().equals(“US”) || user.getLocation().equals(“CA”))), I would use boolean isAdult = user.getAge() > 18; boolean isUSorCA = user.getLocation().equals(“US”) || user.getLocation().equals(“CA”); if (isAdult && isUSorCA).
Leaving the code cleaner than I found it (Boy Scout Rule): This means making small improvements whenever I work with existing code.
Example: If I encounter a function with duplicated code, instead of ignoring it, I would refactor it into a separate method. This reduces redundancy and makes the code easier to maintain.
Example: If I see a variable with a cryptic name like tmp, I would rename it to something meaningful such as customerName, improving readability.
Example: If a conditional statement is overly complex, I might break it up into multiple smaller if statements to clarify the intent.
Practices I want to avoid:
Writing messy code to meet deadlines: The sources clearly state that messy code will slow you down and cause you to miss deadlines.
Example: I will avoid skipping unit tests or writing them poorly just to finish a feature faster. The book highlights that clean code has unit and acceptance tests, and I understand that tests are important to not only find bugs, but also for documentation and design purposes.
Example: I will avoid creating long, complex functions and instead aim to keep functions small and focused. The book also emphasizes that functions should do one thing, and should not mix levels of abstraction. If I write a function and realize that it does more than one thing I would refactor it to smaller more focused functions.
By applying these principles, I can contribute to creating code that is not only functional but also well-designed, easy to maintain, and enjoyable to work with. These examples are based on the concepts described in chapter one of the book, such as writing clean code, paying attention to detail, and avoiding messy practices, and they align with the key themes