Measuring the complexity of SQL statements

Common measures of software complexity include Cyclomatic Complexity (a measure of how complicated the control flow is) and Halstead complexity (a measure of complex the arithmetic is). The “control flow” in a SQL query is best related to “and” and “or” operators in query. The “computational complexity” is best related to operators such as SUM … Read more

How to reduce cyclomatic complexity?

An easy way is to promote the check into a separate method: private String getAppendString(String value, String appendString) { if (value == null || value.isEmpty()) { return “”; } return appendString; } And then you can use this method instead of the if blocks: sb.append(getAppendString(request.getStreet(), “street,”); This will reduce complexity from 28 down to 3. … Read more

Code Metrics Calculation in Visual Studio

The theoretically optimal values are: Maintainability index: 100. Higher values indicate better maintainability. Cyclomatic complexity: 1. The number of different paths that code can take. Depth of inheritance: 1. The number of class definitions above this one in the inheritance tree, not including interfaces. Class coupling: 0. Number of other entities this entity is dependent … Read more

Calculate Cyclomatic Complexity for Javascript [closed]

I helped write a tool to perform software complexity analysis on JavaScript projects: complexity-report It reports a bunch of different complexity metrics: lines of code, number of parameters, cyclomatic complexity, cyclomatic density, Halstead complexity measures, the maintainability index, first-order density, change cost and core size. It is released under the MIT license and built using … Read more

Do you find cyclomatic complexity a useful measure?

We refactor mercilessly, and use Cyclomatic complexity as one of the metrics that gets code on our ‘hit list’. 1-6 we don’t flag for complexity (although it could get questioned for other reasons), 7-9 is questionable, and any method over 10 is assumed to be bad unless proven otherwise. The worst we’ve seen was 87 … Read more

In a switch vs dictionary for a value of Func, which is faster and why?

The short answer is that the switch statement executes linearly, while the dictionary executes logarithmically. At the IL level, a small switch statement is usually implemented as a series of if-elseif statements comparing equality of the switched variable and each case. So, this statement will execute in a time linearly proportional to the number of … Read more

Conditional logging with minimal cyclomatic complexity

With current logging frameworks, the question is moot Current logging frameworks like slf4j or log4j 2 don’t require guard statements in most cases. They use a parameterized log statement so that an event can be logged unconditionally, but message formatting only occurs if the event is enabled. Message construction is performed as needed by the … Read more

What is Cyclomatic Complexity?

I’m not aware of a deeper concept. I believe it’s generally considered in the context of a maintainability index. The more branches there are within a particular method, the more difficult it is to maintain a mental model of that method’s operation (generally). Methods with higher cyclomatic complexity are also more difficult to obtain full … Read more

How can I analyze Python code to identify problematic areas?

For measuring cyclomatic complexity, there’s a nice tool available at traceback.org. The page also gives a good overview of how to interpret the results. +1 for pylint. It is great at verifying adherence to coding standards (be it PEP8 or your own organization’s variant), which can in the end help to reduce cyclomatic complexity.

tech