Clean Code
No one enjoys walking into a mess, including programmers. That is right, we are talking about code. More importantly, clean code. In 2018, Robert Cecil Martin wrote “Clean Code: A Handbook of Agile Software Craftsmanship” that defined clean code as code that was maintainable, easy to read, and could be scaled. This is also the ideal standard of practice, because it makes developing things easier for everyone involved.
Code Smells
Another aspect of development is the Code Smells, which is just the opposite side of the coin from clean code. Code smells are signs or indicators that something might be wrong with the code, even if it is technically working, as indicated by the Tech Target article, “Understanding Code Smells and How Refactoring Can Help.” This can include any areas where the code seems inefficient, difficult to read, or prone to bugs, making it harder to maintain or scale. Understanding and addressing code smells quickly can help prevent larger issues late in development.
Both concepts work together to provide code that is easy to understand, maintain, and scale. The smell is the symptom while the clean code practice is the solution. Articles, such as Codacy’s “What is Clean Code? A Guide to Principles and Best Practices” describe guidelines such as following the established code writing standards. Other recommended standards that I think would be important to implement include writing short functions that only do one thing, avoiding duplicate code or logic, and avoiding using hard-coded numbers.
Examples
Implementing best practices should happen quickly. In the context of practical application, I plan to apply these practices to completing the 3D Escape Room Challenge Capstone project for my senior software engineering project. Below are examples of common code smells and how clean code practices address them.
Large functions
A single function, updatePlayerState()
exists, and handles multiple tasks such as updating health, inventory, and quest progress, making the function long and difficult to troubleshoot.
The solution is to break the function into smaller functions with a single purpose, resulting in updatePlayerHealth()
, updateInventory(), and updateQuestStatus()
. By dividing the tasks, the code becomes more readable and easier to test and debug. This method also allows for specific updates without risking other functionality.
Duplicate Code
Many objects have the ability to be interacted with using the same logic, rewritten in each class, leading to an increase in technical debt.
Instead, creating a reusable class with shared logic allows the object type to inherit from the ObjectInteraction
class and use the objectPickUp()
method. This reduces redundancy and ensures consistent behavior across interactable objects.
Hard-Coded Numbers
Directly embedding numeric values such as speed = 5
in multiple places makes it unclear what the number represents or why it was chosen. Adjusting these values later requires updating each occurrence, increasing the opportunity for errors to occur.
Defining constants or configuration variables with meaningful names, such as const DEFUALT_SPEED = 5;
, allows us to clarify the intent and makes the value easy to change in a single location if needed.
Conclusion
Overall, clean code is the foundation for maintainable, scalable, and efficient application development. By addressing code smells like large functions, duplicate code, and hard-coded numbers with clean code practices, developers can create systems that are easy to understand and evolve. As I continue developing the 3D Escape Room Challenge Capstone project, these principles will help ensure the project is robust and easy to enhance in the future.