For my clean code article, I chose an article I found on the SOLID Principals.
I have been using these principals to direct my thoughts when coding at my work and they often help me to think through all of the ways I can work to improve my code.
The most common one that I have been working to improve on is dependency inversion. It is something that has helped me massively in the past, but is easy for me to skip over when I first start working on something.
It is the idea that you should rely on abstractions for complex logic, etc. instead of relying on consecrations. So rather than directly integrating complex or specific mechanisms, you abstract the mechanism and build out an abstracted handler to take care of the process. This allows you to swap in and out major components very easily later on.
Code Smells
This article had a very interesting code smell listed which they called ‘Shotgun Surgery‘. This code smell warns agains classes containing similar code that is easily needing to be modified across a large number of abstract classes.
To avoid this, care needs to be taken when abstracting out classes and watching for external relations when making a classes functions. A fix for this may be to remove code from inside the class that doesn’t need to be there, or to extract common code into a single function or another class.
For a good example of the Shotgun Surgery code smell, this article has a great example on it. https://dzone.com/articles/code-smell-shot-surgery
Leave a Reply