This Post Stinks
Do you smell that? This week, we’re talking about Code Smells!
During the development of our Capstone project this week, I’ve found myself being a little bit more self-conscious about my workspace hygiene. For the last several years now, the vast majority of my programming projects have been a solo mission. So, now that I’m on a development team for a long-term project, it feels a bit like I’m standing in a crowded elevator and suddenly aware that I’m starting to sweat. My team members can see, and are expected to closely review, all the code I write for this project. While part of me would like to keep any ugly, messy, confusing, or half-baked ideas hidden from the world (does anyone remember Monica’s closet from Friends?), I know that’s not actually going to make me more confident in the code I put out there.
So how do I avoid this feeling? If I want to stop worrying about my code stinking up the place, then I need to understand just where the smells come from and address the root cause of the issue. This week, I did some research on Code Smells so that I can be confident that I’m putting out fresh, clean code.
To understand more about the different kinds and causes of code smells, I ended up finding an article dedicated to this topic on the Coding Horror blog [2]. This article lists several types of smells that I’ve definitely been guilty of in the past, such as Middle-Manning (where a whole class ends up delegating all of its work to other classes [4]) or Indecent Exposure (where too much in a class is publicly accessible – allowing other classes to see things they shouldn’t! [3]), and some that I’ve been able to avoid so far.
I’d really like to avoid repeating these mistakes in the future by getting rid of any classes that have become middle-men and making private anything that shouldn’t be exposed. This way, my code will be less complicated and easier to read. Of course, I can keep refactoring my code after it’s written to clean up any messes I made while developing. But, how can I avoid the need to refactor my smelly code after I’ve already put in all that work writing it? The Coding Horror article I read made me realize that the best way to treat this smelly code disease is actually to prevent the stink from happening in the first place.
In his book Clean Code: A Handbook of Agile Software Craftsmanship [1], Robert Martin emphasizes that a core value to keep in mind when writing code is that we, as programmers, need to remember we’re writing for an audience. Other people need to be able to read our code and understand what it is doing. This can help prevent code smells in multiple ways. By writing for an audience, we make sure our ideas are conveyed clearly and that there is no ambiguity in what our code is doing. Akin writing a grocery list, when we write notes that only we will read, it becomes messy and hard for others to understand. But, when we write for someone else, we subconsciously shift to a different, more understandable style. This can help us out in the long run, too – when we come back to code we wrote years ago, we’ll be able to understand it and jump back in right away if it was written with new readers in mind.
This is one thing I am going to try and apply to my future coding endeavors. If I write with my teammates in mind, I think that will put me in a clean headspace and allow me to convey thoughts in uncomplicated, easily-understandable ways. Overly complicated code is eventually going to start stinking, and I think writing for an audience will help me adhere to cleaner practices.
References:
- [1] R. C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship. Pearson Education, 2009.
- [2] “Code Smells,” Coding Horror. Accessed: Jan. 16, 2024. [Online]. Available: https://blog.codinghorror.com/code-smells/
- [3] “Indecent Exposure.” Accessed: Jan. 16, 2024. [Online]. Available: http://luzkan.github.io/smells/indecent-exposure
- [4] “Remove Middle Man.” Accessed: Jan. 16, 2024. [Online]. Available: https://refactoring.guru/remove-middle-man
- [5] “Remove Middle Man.” Accessed: Jan. 16, 2024. [Online]. Available: https://refactoring.guru/smells/middle-man
Leave a Reply