Three things you can do to avoid a messy code base
How can I avoid that my code base gets messy over time to avoid accumulating technical debt?
In my estimation, at almost any project you will find yourself in the situation that you think your whole code base is a mess and that you are wondering how you could let it slip. After all you are a clean code fan, aren’t you?
After all you tried your best to keep the house clean and now you are faced with this tangled spaghetti-code project which you just want to get working.
Here’s three things how you can completely avoid or mitigate ever running into this issue again
1. Write unit and integration tests to force yourself to write clean code
I know I know, some people are too cool for tests. I hope you are not one of them. It not only helps you to have more confidence in the functional fitness of your software, but it has quite a few interesting side effects.
If you have the habit of unit- and integration-testing everything you will quickly realize that you need to make your code very modular. After all a unit test is just testing one little functionality in your project. If you put all your code in one file and in long functions, you will not be able to write tests easily.
So the rule of thumb is: if you cannot easily unit and integration test your code, your code base is probably messy. Force yourself to test your functionality properly and you will automatically do the right refactorings as you add new features
2. Use a linter or the standard code formatting rules of the IDE of your choice
Having a common way of formatting the code becomes more important the more people work on one project. This does not mean that you should not have a linter when you are doing the hero hacker kind of thing. Sorry, not sorry.
If you use a statically typed language like Java, then you can just use the IDE’s default formatting settings. This makes it easier for all contributers
3. Research how other people structure projects
Try to get a little bit of inspiration on how to organize your project by looking at popular open source projects that are known for clean code. The goal here is not to understand what the code base in detail but how files are grouped together and how the testing is set up