A Critical Component of Your Software Code Quality Strategy
Welcome to the third installment of our blog series on code quality! In our previous posts (which can be found here, and here), we unraveled the intricacies of code complexity, exploring its definitions and why it’s crucial to measure. Today, we embark on a journey to discover how to measure code complexity effectively.
In the realm of software development, objectivity is the North Star guiding decisions and strategies. Without accurate, reliable metrics, you’re navigating blindfolded, relying on intuition and guesswork. Objective measurement of code complexity is crucial for several reasons:
Code complexity metrics are typically measured at the function or file level. Still, their true significance emerges when you consider them at the component or project level. It’s entirely plausible for specific files or functions to have high complexity figures, especially when they implement complex functionality. However, it should never be the case that all your files and functions exhibit high complexity. If that’s the scenario, it’s a clear indication of low overall code quality, and improvements are urgently needed.
Choosing the right code complexity metric is akin to selecting the right tool for the job. It’s imperative that the chosen metric resonates with all stakeholders within the development organization and is measurable. Using a single agreed-upon Code Complexity metric helps align the entire development organization; it fosters transparent decision-making. Whether it’s prioritizing refactoring efforts, assessing code quality during code reviews, or making architectural decisions, a consistent metric provides clear data to support these choices.
However, it’s essential to acknowledge that different projects and organizations have different needs and priorities. While some projects may prioritize maintainability and readability, others might prioritize high performance and efficiency. Therefore, the choice of metric should align with your project’s specific goals and the preferences of your development team.
Among the various code complexity metrics available, we, at TIOBE Software, have found McCabe Cyclomatic Complexity to be the metric that best aligns with our philosophy of measuring and managing code complexity. Here’s why it has secured its place as our metric of choice:
Ease of Calculation: McCabe Cyclomatic Complexity is relatively straightforward to calculate, thanks to readily available tools. This simplicity ensures accessibility for all team members, making it easier for development organizations to adopt and apply consistently.
Widespread Acceptance: McCabe Cyclomatic Complexity enjoys widespread acceptance within the software development community. It’s a well-established metric that has stood the test of time. This recognition means that developers, architects, QA managers, and other stakeholders are more likely to embrace and work with this metric.
Reflection of Code Complexity: While there are more complex metrics like n-path complexity, we believe that McCabe Cyclomatic Complexity strikes the right balance between simplicity and depth. It effectively reflects the intricacy and complexity of code without overwhelming teams with intricate calculations.
Testing Perspective: Code complexity is not only about development; it also has implications for testing. Higher complexity often translates to more test cases needed to achieve comprehensive coverage. McCabe Cyclomatic Complexity provides insights into the testability of your code, helping QA teams allocate resources effectively.
While we see value in other complexity metrics like Cognitive complexity, we’ve encountered challenges in finding tools that calculate this metric in a consistent and reliable manner. As such, McCabe Cyclomatic Complexity emerges as the pragmatic choice that balances accuracy, simplicity, and widespread adoption.
In today’s diverse software development landscape, companies often have multiple software products that use different technologies. Therefore, it’s crucial to select a tool that has support for all the languages and technologies your organization employs. Keep in mind that many tools have support for only a limited set of technologies, potentially complicating your efforts to standardize code quality across your projects.
A wealth of tools is available for measuring code complexity, catering to various needs and budgets. Complexity metrics are often part of other kinds of metric measurements, like Static Analysis for measuring Coding Standards or even Code Coverage measurements of your automated tests.
Additionally, Code Complexity metrics are fundamental metrics measured by Code Quality Metric frameworks like our own TiCS Framework, SonarQube and CAST, as well as more advanced Code Analysis tools like Coverity and CodeSonar.
Here’s a breakdown of some of the notable options that offer multi-language support:
At TIOBE, we’ve walked the path of complexity measurement and encountered various solutions, but many failed to measure McCabe Cyclomatic Complexity correctly or consistently. Commercial options often came with prohibitive pricing, limiting accessibility. Drawing on our expertise in compiler technology, we decided to create our own Code Complexity tool – TICSCyclox.
TICSCyclox supports all languages within our Code Quality framework TiCS and we extend the tool’s capabilities as new technologies emerge. It’s designed to be accessible and accurate, ensuring that organizations can measure and control code complexity effectively. Moreover, it’s free for all our customers, democratizing access to this critical metric.
In our next blog, we’ll explore what constitutes good values for Code Complexity metrics. We’ll delve into the crucial aspects of trending, baselining and benchmarking, providing you with actionable insights to keep code complexity under control.
Controlling code complexity is a linchpin in the journey to software excellence. With the right tools, metrics, and practices in place, you can ensure your codebase remains not just functional but also robust, resilient, and future-ready. Stay tuned for our next blog, and let’s continue raising the bar in software development together!