org.sonar.l10n.py.rules.python.S117.html Maven / Gradle / Ivy
Local variables and function parameters should be named consistently to communicate intent and improve maintainability. Rename your local variable
or function parameter to follow your project’s naming convention to address this issue.
Why is this an issue?
A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes.
Local
variables and function parameters hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily
recognizable pattern.
Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to
maintain and debug. It also ensures consistency in the code, especially when multiple developers are working on the same project.
This rule checks that local variable and function parameter names match a provided regular expression.
What is the potential impact?
Inconsistent naming of local variables and function parameters can lead to several issues in your code:
- Reduced Readability: Inconsistent local variable and function parameter names make the code harder to read and understand;
consequently, it is more difficult to identify the purpose of each variable, spot errors, or comprehend the logic.
- Difficulty in Identifying Variables: The local variables and function parameters that don’t adhere to a standard naming
convention are challenging to identify; thus, the coding process slows down, especially when dealing with a large codebase.
- Increased Risk of Errors: Inconsistent or unclear local variable and function parameter names lead to misunderstandings about
what the variable represents. This ambiguity leads to incorrect assumptions and, consequently, bugs in the code.
- Collaboration Difficulties: In a team setting, inconsistent naming conventions lead to confusion and miscommunication among
team members.
- Difficulty in Code Maintenance: Inconsistent naming leads to an inconsistent codebase. The code is difficult to understand,
and making changes feels like refactoring constantly, as you face different naming methods. Ultimately, it makes the codebase harder to maintain.
In summary, not adhering to a naming convention for local variables and function parameters can lead to confusion, errors, and inefficiencies,
making the code harder to read, understand, and maintain.
Exceptions
Loop counters of one letter are ignored by this rule.
for i in range(limit): # Compliant
print(i)
Local variables matching regular expression ^[_A-Z][A-Z0-9_]*$
are considered as constant and ignored by this rule.
Function
parameters are not concerned by this exception.
def print_something(important_param):
LOCAL_VARIABLE = "" # Compliant
print(important_param + LOCAL_VARIABLE)
How to fix it
First, familiarize yourself with the particular naming convention of the project in question. Then, update the name to match the convention, as
well as all usages of the name. For many IDEs, you can use built-in renaming and refactoring features to update all usages at once.
Code examples
Noncompliant code example
With the default regular expression ^[_a-z][a-z0-9_]*$
:
def print_something(IMPORTANT_PARAM): # Noncompliant
localVariable = "" # Noncompliant
print(IMPORTANT_PARAM + localVariable)
Compliant solution
def print_something(important_param):
local_variable = ""
print(important_param + local_variable)
Resources
Documentation
- Python Enhancement Proposals - PEP8 - Naming Conventions
- Wikipedia - Naming Convention (programming)
Related rules
- {rule:python:S100} - Method names should comply with a naming convention
- {rule:python:S101} - Class names should comply with a naming convention
- {rule:python:S116} - Field names should comply with a naming convention
- {rule:python:S1542} - Function names should comply with a naming convention
- {rule:python:S1578} - Module names should comply with a naming convention
- {rule:python:S2710} - The first argument to class methods should follow the naming convention