Clean Code: What Advice Would You Give To Novice Programmers?
I'm a big fan of the 97 Things series. Rather than depending on one author to be an expert on a particular topic, everyone gets together to pass on their own real world experience. I'd like to give this crowdsourcing approach a go in this article to gather all the advice that you would pass on to novice developers. With students started in their university courses, it's the perfect time to gather your thoughts.
To get things going, here are a few of my own thoughts:
Test Driven Development Really Is Worth It
Whether you take the purist view that you need to have a failing test first, or you simply consider what tests you will write straight after the code, having your test cycle tightly coupled to your coding process makes your life easier. Less bugs will come back, and most importantly, you'll think more critically about what you're going to write. Anytime that I've seen test driven development in practice, it's helped quality. The sooner you get used to the process the better.
Don't Rely On Comments Too Much: Make Your Code Self-Explanatory
Don't get me wrong here, comments are great. But you don't really need to put comments around the getters and setters. You should apply that methodology to standard methods too. Make the method name as explanatory as possible (without being too verbose), and add comments to describe only what the caller really needs to know. It's a delicate balance, but you'll get used to it.
Don't Let Exceptions Disappear Into A Black Hole
Checked exceptions exist for a reason. As a result of the code completion functionality provided by most IDEs you can easily add a try/catch around a block of code. There's nothing worse than seeing the default //TODO in the catch statement. Other times you will see that the exception is simply printed to the console (not even logged). This all seems like another purist view, but when you're trying to find the cause of a particular bug and find that the exception which should have been thrown to the caller was simply ignored, you'll see why it's important to treat exceptions with the respect they deserve.