Bad variable names can increase costs

CostSoftware AgilityEmbedded Documentation

Bad variable names can increase costs

This code insight counts one violation each time the name do not complies with:

Constants

(([A-Z_][A-Z0-9_]*)|(__.*__))

  • underscore allowed
  • upercase
  • anything delimited with double underscore

Note : a constant is an identifier that is assigned at only one time in a function, method or global scope, with a literal value (number, string or list)

 

Variables

 

[a-z_][a-z0-9_]

 

  • underscore allowed
  • lowercase

 

Note : a variable is an identifier that is assigned at least one time in a function, method or global scope.

 

Examples
PI = 3.14                   # (constant because assigned only one time in root scope)
CONST1 = [item1, item2]     # (constant because initialized with a literal list
CONST2 = "this is a string" # (constant because initialized with a literal string
my_var = 10                 # (variable because assigned later in same scope)
class toto:
   def meth() :
       CONST3 = 100    # (constant because assigned only one time in meth() scope)
       var = 10        # (variable because assigned later)
       print 'yo'
       var = 5
       my_var2 = var   # (variable because not initialized with a literal
       PI=5            # (variable because assigned two times in "meth()" scope)
       print(PI)
       PI = PI_0
my_var = 5

5362

Why you should care

The name of the variable or constant does match the naming convention. This error is a stylistic warning. The code will execute. But you can improve the readability of the code by renaming the variable or constant to match the following regular expression:

  • Variables: [a-z_][a-z0-9_]$
  • Constants: (([A-Z_][A-Z0-9_]*)|(__.*__))$

Business Impacts

Cost

CAST recommendations

References

https://www.quantifiedcode.com/knowledge-base/maintainability/Avoid%20using%20%22non-Pythonic%22%20variable%20names/4m6i5Dpz

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

See featuresHow it works