Wednesday, May 11, 2016

I Learned How to Write Code From Reality TV

If a code base is going to stay clean, readable, and maintainable, then the developers working on it need to actively put in effort to keep it that way every time they touch the code. My personal philosophy when writing new or working on existing code can be summed up by three reality TV personalities.

The first is to "Make It Work."


Get the code accomplishing the task that you need it to. Doing TDD? Write your unit tests, then dive into the code and get to work. Change code until those new unit tests pass. Hack away at it. Just get something together that does what it is supposed to do and fulfilling the requirement you are working on. Make it work!

Second, "Make it Right."


If you cut corners or used ugly code to get your feature working, now is the time to pay the piper. Start refactoring that code. Look for places that you duplicated logic, and find a way to make that code shared between both places. If you did TDD, you have your unit tests to catch any time you break the working functionality. If you didn't do TDD, add your unit tests now. Look objectively at your code: is it maintainable? Or did you try out a cool coding trick that in retrospect over-complicates the logic? Will other developers after you be able to understand it? Do you need to extract some methods so it's more clear how things are flowing? In short, make sure your code follows good coding practices and standards.

Third, "Don't **** it Up."


Run the full unit test suite to make sure you didn't break anything else or create unwanted side affects. If you refactored other parts of the code, run through those flows on your own and ensure they still work the way you expect. Be your brothers' and sisters' keeper, and ensure that what you did won't block their work. Make sure you test your edge cases, and add unit tests for them if you haven't already.