How We Achieved the Best Code Quality in My Career
Join the DZone community and get the full member experience.Join For Free
the project was a greenfield project so there was no legacy code to inherit which gave us the opportunity to create something great, but of course also the opportunity to screw up completely. in fact, i was involved in a similar project some years ago that had the same opportunities but came out with all but great code quality.
in this post my recently finished project has the codename
while the other project with not-so-good code quality has codename beta .
alpha and beta similarities
the settings of the alpha and beta projects were quite similar. they had about the same time frame, budget and team size. the systems were built for similar organizations. both projects set out with a high level of ambition, but while project alpha sustained in keeping that level to the end of the project, beta slipped behind badly. i’m proud to have been part of alpha’s success. i prefer to not think of my contribution to beta’s nasty, messy and stinking code.
the first success factor of project alpha is the clear architecture. i might be a bit biased here as i was the architect of alpha, but this is my blog so i can write that my own architecture is great and nobody can object.
wait, that’s not true. i do allow comments on the posts… i’d better explain myself.
the important word in clear architecture is not architecture but clear .
a clear architecture is extremely important for any greenfield project. for the alpha project the basic building blocks were asp.net mvc3, entity framework 4.3.1 code first with migrations and jquery ui. based on those building blocks an architecture was defined with the project split up in a few different assemblies and each assembly in namespaces. the key success factor is that there was an overall plan for how the assemblies and namespaces were defined and what functionality should go were. it doesn’t matter very much what the overall plan look like. what matters is that it exists and that it is clear and communicated to all developers.
looking back at project beta a couple of mistakes were done. the first was design by committee . i do believe in a democratic design process where all opinions are taken into account. the problem is when the resulting architecture is a compromise. that’s what we got in project beta. several architectural decisions ended up as compromises between two alternatives effectively getting all the disadvantages of both alternatives and none of the advantages.
the second big mistake was the lack of enforcement of the architecture. in all projects (including alpha) there are times when some developer on the team hasn’t understood the architecture and writes code that doesn’t fit in. in beta, there were no routines for finding out and correcting those issues before it was to late. in alpha we fixed it in time, thanks to the code reviews.
insane compiler warning levels
both projects set out with a bold ambition and enabled all code analysis warnings available. in project alpha we soon realised that some of the warnings were not relevant for us so they were disabled quickly. the first thing that got disabled was the entire internationalization block since this application is custom built for swedish use.
in project alpha we never had more than 3 or 4 warnings in the checked in code base. those warnings were mostly that freshly created namespaces contained too few types. if the namespace wasn’t filled with more types it was removed and the types in it merged into another namespace.
other warnings were corrected or sometimes suppressed in the code if there was a good reason for it.
in project beta people simply ignored warnings they didn’t see as relevant. the warning list grew over time and was soon impossible to handle. the last time i looked at it a full rebuild issued about 4000 warnings. some of them minor code analysis warnings, some of them major issues such as incorrect if statements giving unreachable code.
last, but definitely not least on my list is code reviews. early in the alpha project we decided that code reviews should be part of our definition of done (dod). whenever a developer in the project had coded and tested a task it was not marked as “done”, but rather as “ready for review”. i and another senior developer on the team monitored items in the “ready for review” state and looked through them. often there would be comments and things that needed to be improved. in that case the item was sent back as “in progress” for the developer that had done the initial coding. that way all developers got immediate feedback on their own work and had to fix any issues in their own code.
that of course included myself and my code too. i had to go back and fix naming, documentation comments and move shared logic to helper methods to get my code approved. the reviews helped both junior and senior developers to improve code quality.
i cannot emphasize the code reviews enough. thanks to them several cases were people unknowingly were solving the same problem twice were found and refactored into a common solution. thanks to them we found security issues. thanks to them we found performance bottlenecks that would strike once the system has production volumes of data. thanks to them we got a common style and naming convention in the code. thanks to them relevant discussions on design and architecture regularly took place. thanks to them we got a collective ownership of the code.
i don’t think i have to mention that there were no code reviews at all in project beta. the code produced differed vastly in style and quality depending on which developer had written it. when it was bug fix time it was long forgotten who wrote what code, so there were no feedback to each developer.
the key to great code quality
the one key success factor to great code quality is communication. the architectural discussions and code review discussions in project alpha is what kept all code aligned to the architecture (and sometimes made us change the architecture to fit reality). in project beta there were not enough of those discussions and they often didn’t end up in any clear decisions.
now i’m just about to start a new project. one of the project alpha devs that will also be involved in the new project told me last week that he really wanted us to do code reviews in the new project too. that dev has actually sometimes had a hard time with my reviews, where i have sent back items two, three or four times before approving them. still he thinks that the reviews are valuable. that is a sign of a true will to improve, a true will to learn.
in project alpha everybody wanted to learn and improve. in project beta people were happy that things compiled. the key to great code quality is communication, but that will only ever take place if the developers do want it, if they see it as a chance to learn rather than a risk of getting caught writing ugly code.
starting my next project i look forward to the reviews. i know that they will be a great source for learning, both for me and the rest of the team.
Published at DZone with permission of Anders Abel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.