In an ongoing battle to remain productive while delivering quality code, we are always on the lookout for the practices that help us do our best work.
Code inspection plays a great role in this quest.
“Code inspection is more cost-effective and isolates defects more efficiently than testing.” — IFSQ
I know that not everyone is a big fan of the practice, but the benefits of code inspections are too monumental to ignore. Detecting bugs via code inspection not only saves time, but also makes development more efficient, improves the overall quality, and simplifies the code among other things.
What is code inspection exactly?
Simply put, it is a form of formal review used for scanning the code for any malfunctions at the early stages and detecting space for improvement.
In one of the previous blogs, I explained how to configure the SonarQube static code analyzer in order to improve code quality. If you are wondering how to do that, you can check the following link. However, if you are interested in a code analyzer that requires minimum configuration and which can be set up in just a few minutes, this is a great starting point.
This blog will be a short transcript about the free command-line version of Jetbrain’s ReSharper tool. Check out their official site to learn more, because the tool itself is very well documented. If you you are not familiar with their products yet, you should definitely check out those as well.
ReSharper Command Line Tools is a set of FREE standalone tools that execute ReSharper code inspections outside of visual studio and additionally find code duplicates.
The tools can be used without installing ReSharper and can be integrated with continuous integration or code quality management servers.
These are the tools included in the CLT package:
- InspectCode executes hundreds of ReSharper code inspections
- DupFinder detects duplicated code in the whole solution or narrower scope
- CleanupCode instantly eliminates code style violations and ensures a uniform code base
The only prerequisite is to download CLT package and extract it to the preferred location:
Once you do this, the tools are ready for use! You can execute the following demand from Windows PowerShell:
.\inspectcode.exe solution-path\solution.sln --output =output-path\file.xml
You will get an XML file that looks like this:
You’ll notice it’s definitely not the most readable form of a report, but there’s no need to panic. To transform XML file to an HTML page, you can download XSLT scripts from their GitHubpage:
.\Transform-Xslt.ps1 input-file-path\file.xml output-file-path\page.html
Since we use XSLT scripts for transformation, this means we can also inject some custom CSS in order to make the report more readable.
I used bootstrap for this purpose:
<head> <title>Resharper InspectCode Report</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"/> </head>
Now we have a report that is readable and nicely formatted:
And that’s it!
This is all you need to get started with static code analysis.
In case you want to go beyond basic code inspections, check out the links below because CLT package comes with other great tools:
- Duplication Finder – a free command-line tool that finds duplicates in C# and Visual Basic .NET code. By default, it considers code fragments as duplicates not only if they are identical, but also if they are structurally similar, even if they contain different variables, fields, methods, types or literals.
- Cleanup Code – a free command-line tool that can perform code cleanup to instantly eliminate code style violations in a project or solution and ensure a uniform code base.
- CLT package – a NuGet package for command-line tools.
Why bother with all this?
“The difference between an amateur and a professional is in their habits. An amateur has amateur habits. A professional has professional habits (hence; code inspecting is a fantastic pro habit). We can never free ourselves from habits. But we can replace bad habits with good ones.”
– Steven Pressfield