Tips and Tricks

Everything you need to know about code inspections

By now you might have heard about Visual Assist’s code inspections—it has been a staple of almost all recent updates and we hosted two webinars discussing it. This article sums it all in an easy to read and convenient way.

What are code inspections?

Code inspections is a feature in Visual Assist that automatically checks source code for issues and code smells, and suggests the appropriate fix. The main benefit of using code inspection is better overall code quality with minimal effort.

Code inspections work by utilizing code checkers, sometimes referred to as static analyzers. These are tools that analyze your source code passively—it happens as you type your code. You do not need to actually compile and execute your code to detect a problem (i.e. finding bugs and errors using run-time techniques such as testing).

Code issues are underlined in blue. Line information and a description of the issue is also shown in the VA Code Inspection Results window.

It looks for common patterns, broken coding conventions, and potential bugs in the code using algorithms and special techniques. The purpose of code checkers is to catch problems early in the development process, which can save a lot of time and effort later on. 

What issues can it detect?

The issues and code smells that can be detected vary widely from stylistic infractions on coding convention, all the way to system critical errors.  Here are some of the common fixable issues:

  • Detecting and updating obsolete coding syntax
  • Highlights possible readability issues and broken coding conventions
  • Checking type-related errors (such as making invalid type conversions or assigning values of incompatible types, using uninitialized variables, or.)
  • Wasted resources due to inefficient memory allocation/deallocation.
  • Other issues of non-obvious, but potentially dangerous, incorrectness

Visual Assist’s code inspection feature is based on LLVM’s /Clang-Tidy code analysis engine so you can be sure that it covers a variety of checks targeted at finding security and API usage bugs, dead code, and other logic errors. Additionally, we extend the functionality through the use of ‘evaluated’ checkers (more below).

READ: More examples from a blog by Bartek from C++ Stories

Evaluated and unevaluated checkers

Code checkers are added gradually to the list of Visual Assist code inspections. All checkers that are in the list are thoroughly evaluated, checked, and tested. Evaluated checkers often include changed functionality that is not in the base clang-tidy, making them more reliable and safe. This is a key part of the value of the checkers we include. Using evaluated checkers guarantees that the code will be refactored properly and any suggested fix will not break anything.

On the other hand, unevaluated checkers are still based on Clang-Tidy’s extensive list but have not undergone the same quality assurance tests as its counterparts. Almost a hundred unevaluated checkers have been added in Visual Assist 2023.2.

Note that unevaluated checkers will still suggest the recommended fix and behave like normal checks, but users are advised to manually ensure that everything is working correctly. The benefit that this brings is that developers can know what and where a potential issue lies—which is oftentimes the bulk of the work.

Why use code inspections to fix issues and refactor code?

Running code inspections lists down issues in a window for you to examine before implementing fixes.

Code inspections read source code line by line, analyzing the structure, logic, and syntax. It pays attention to specific patterns or coding conventions that may indicate potential issues. As they encounter a line of code, it uses a set of rules and algorithms to determine if the code adheres to best practices or if there are any deviations.

Using code inspections automates a lot of the manual checking and verification that developers would traditionally do. With it, it no longer matters how familiar a developer is on a project, or how large a solution is—it all goes under the same level of scrutiny.

it provides some of the following benefits as well:

  • Be able to utilize modern coding practices without actually knowing them.
  • Improve code quality with minimal refactoring risk.
  • Detecting potential code issues early.

WATCH: Understand how code inspections are evaluated

How to use code inspections to eliminate code smells

First, make sure to enable it by navigating to Extensions -> VAssistX -> Code Inspection (beta) -> Enable code inspection. 

Then you can either click on detected issues underlined in blue, use the Quick Refactoring Menu (Shift + Alt + Q) while on the highlighted issue, or use the code inspection dialogue to implement changes in batches.

WATCH: Code inspections in action

Visual Assist by Whole Tomato: Filling Gaps in Visual Studio for C/C++ and C# Developers

Visual Assist is a productivity extension for Visual Studio development work. It provides a set of intelligent refactoring, navigation, code highlighting and generation features for C, C++, and C# development. With its highly-efficient memory management and minimal UI, Visual Assist gives developers uninterrupted access to advanced features without slowing down the IDE.

Visual Assist is one of the definitive plugins that conceptualized and shaped most of the current features you see now in Visual Studio. And to this day it continues to develop user-centric design for maximum productivity and usability.

Download Visual Assist here.

Related posts
Tips and Tricks

The biggest challenges in writing C++ programs for finance and banking

Build Announcements

Visual Assist 2024.6 release post - Important fixes for Visual Studio

News

Catching up with VA: Our most recent performance updates

Build Announcements

Visual Assist 2024.4 release post - ARM Support

Leave a Reply

%d