Code Quality Is A Feature, Not An Afterthought

I must admit something… I must admit that I get more and more frustrated with the lack of code quality in all projects I work on as a contractor. Some are okay, but most are very far from it. What frustrates me more is that most of this poor-quality code I analyze is written or supervised by senior software engineers. Since I don't work permanently at a company anymore, I can't make sure that code quality is a feature, not an afterthought. By the time a company gets in trouble and hires me to work on their codebase, it's almost too late to implement code quality since it usually means major changes to the code and architecture.
 
As an example, this year I was hired for a contract about 1 ½ months before user accepting testing started on the first version of three projects. These projects aren't even in production yet and they already have 4,200 code violations (one every 4 lines of code), including violations from StyleCop. These three projects also needed major architectural changes so that the code can be reusable and unit testable, something I discuss a lot in my conference sessions. This is an example on when it's almost too late to hire me, since I fear most of the work I did in the first 1 ½ months will never make it into the master branch.
 
On a contract a few years ago, their codebase had over 50,000 issues (not including StyleCop). I worked on it for about a year… just fixing their code issues. I didn't even add one feature in that year! As with many contract jobs, they ran out of funding at the end of their fiscal year, so I moved on to another contract. About six months later they hired me back and in that six months, they introduced over 10,000 new code issues!
 
Now do you understand why I get frustrated? I've been writing about and speaking about code quality for a very long time. I drill code quality into my students heads in all classes I teach. All my code sessions at conferences are wrapped around code quality. I write about code quality in all my books. Heck, I have the only pure .NET coding standards book out there!
 

Management Doesn't Care, So You Have To

 
Let's face it, management at most companies only care about two things. The first is features, features and more features. Adding features is the only way to sell product. The second thing they care about is adding those features at the lowest cost possible. Due to these two things, management does not care about code quality which always includes proper architecture, since to them, it isn't a feature. They can only see what features cost now with no worry about how much it will cost the company in the future if code quality isn't a feature from the very beginning. Especially when it comes to fixing bugs, which isn't a feature either.
 
At one company I worked at, towards the end of my time there, I was in a major meeting with managers and vice presidents. They were talking about all the features that needed to be added to the product to sell more product. Towards the end of the meeting I was getting frustrated and I finally raised my hand and said, “The features you want will either be impossible or take so long that our competitor will release it before us and we will lose customers if you don't break down and re-architect the database.” They all looked at me with expressions like they have never heard this before until my boss said, “Dave has been telling you this for four years, why don't you listen to him?” Not long after that they laid off all the high-level software engineers in my department, including myself. Guess what happened? They lost customers.
 
So, it's up to you, the coder, to care about code quality or it won’t happen. Why should you care? You will be the one that will have to maintain the code, add features and fix bugs. If your projects are a pile of spaghetti code, then you will be just putting band aid over band aid on top of it until it finally falls down and you have to start all over again, hopefully right this time. It will be worse if the database is poorly architected too as I discussed in the example above. So why not do it right in the first place?
 
For me, it's also a matter of pride. I want to check into source control the best code possible. It's even more important to me as a contractor since I will be at the company for a limited time, so I can't put it off until later.
 

Learning Code Quality

 
Again, to learn code quality, it must be important to you. You must care about it and once you have been doing it as long as I have, it just becomes second nature. So much so that these days, it's painful for me to work on projects that code quality isn't a feature and I'm forced to use bad practices. Now that I think of it, maybe I should charge companies a higher hourly rate depending on the state of their code!
 
I was lucky in my beginner days to be mentored by great senior software engineers and learned best practices, most I still use today.
 
Good code quality practices never really go out of style, you just keep learning even better practices. So, if you can find a great team to work with, then that will be one of the best ways to learn good code quality practices. In the interview, you should ask them how they implement code quality (let me know what they say).
 
Books
 
Books are a good way to learn too. I always suggest getting books that can be used for many, many years like Clean Code by Robert C. Martin or Refactoring by Martin Fowler. Depending on the programming language you use, get the best book on object-oriented programming you can find. Also, books on design-patterns are a must too. When you learn the things in these books, you can use that knowledge for your entire programming career. I am a very big proponent of object-oriented programming that was first used back in the 1950's, before I was even born! If you are a .NET developer, then my books can help too.
 
Community Events
 
Community driven conferences, like Code Camp's, are a good place to learn to since they are run by software developers just like you, so they know the importance of code quality. The bigger paid conference is usually not a good place to learn since they are driven by profit. I've previously written about this in my article titled Soft Skills – As Important If Not More Important Than Technical Skills At Conferences.
 
Another great place to learn are local community events like User Groups, Meetup's and even your local C# Corner Chapter. Most of these types of groups will have multiple events each month, again driven by software engineers in your area. Most of these events are free, so why not take advantage of it? One of the reasons I'm able to see the whole picture when it comes to architecting applications is because I founded a user group here in San Diego, California and ran it for 20 years.
 
Online Training
 
Online videos could be a good way too. But I'm not aware of very many on the subscription-based sites, like Pluralsight, except for the one titled Defensive Coding in C# by Deborah Kurata. There are free sites out there too like Channel 9 and my video site.
 
Websites
 
There are tons of websites out there such as Coding Best Practices on C# Corner and the Code Quality category on my site. All the code on my site follows good code quality, unless I am showing a bad code example. I make sure of this. So, follow sites that shows all the examples using normal coding standards and code quality patterns. You can also check out Microsoft Pattern's and Practices, which is now on GitHub (not sure why since it's harder to use). I'm not aware of any blog that just focuses on code quality. If you find one, please let me know!
 

Tools Help Find Issues & Help You Learn

 
Visual Studio extensions and other tools can help you find and fix issues with your code. Code analysis is a very important part of coding and there are hundreds of rules that these tools use. Not only should you run these tools before you commit any code to source control you have been working on, but they also should be part of the build process, so the state of the code can be easily seen. This is even more important if you hire contractors!
 
I have written about my work flow before I check in code in the article My Workflow Before I Submit Code Changes. In this article I write in detail the three tools I use. I use StyleCop (free) from Microsoft, then CodeIt.Right from Submain.com and then Analyze (for .NET Framework) in Visual Studio. Other extensions that I use include Code Cracker, GhostDoc (for documenting code), Productivity Power Tools 2017/2019, StopOnFirstBuildError just to name a few.
 
Another tool that all developers needs is a good refactoring tool. I use CodeRush from DevExpress and there is also ReSharper from Jet Brains. The refactoring that is built into Visual Studio, doesn't have enough features, so I suggest getting one of these two tools.
One great side-effect for most of these tools is that they teach you good coding practices! Once you fix the same issue over and over, eventually you will just code it correctly. This is one of the reasons on why I know what I know when it comes to code quality. When you are trying to justify the cost of any of these tools, remember to tell your boss that you will become a better developer. They are worth every penny!
 

Summary

 
I hope I encouraged you to make code quality a feature of all code that you write. Make sure you bring it up in any planning meetings you might attend. I also gave you some suggestions on how to learn good coding practices that includes using tools for Visual Studio. How do you learn about good code quality practices? Please make a comment below.


Similar Articles
McCarter Consulting
Software architecture, code & app performance, code quality, Microsoft .NET & mentoring. Available!