Do not modify function parameter

Metadata

ID: go-best-practices/modify-parameter

Language: Go

Severity: Info

Category: Best Practices

Description

Assigning new values to function parameters exhibits several bad coding practices and should be avoided for several reasons:

  1. Redefining parameters: The code redefines the param parameter within the function body by assigning a new value 41 to it. This is considered a bad practice because it can lead to confusion and make the code harder to understand. Modifying a function parameter in this manner breaks the expected behavior and can cause unexpected side effects. It is generally best to treat function parameters as read-only values and avoid reassigning them.
  2. Shadowing variables: The code further exacerbates the issue by using the short variable declaration := to define a new variable param within the function body. This shadows the original param parameter, making it inaccessible within the function. Shadowing variables can cause confusion and make the code harder to reason about. It is better to use distinct variable names to maintain clarity and avoid any unintended side effects.
  3. Inconsistent variable references: The code references the variable a using both the increment (++) and decrement (--) operators, but it is unclear what a refers to. This inconsistency can cause confusion and make the code more error-prone. It is important to use clear and consistent variable naming conventions to improve code readability and maintainability.

To write more maintainable and understandable code, it is advisable to adhere to the following practices:

  • Avoid redefining function parameters.
  • Use descriptive and unambiguous variable names.
  • Avoid shadowing variables.
  • Maintain consistency in variable references.

By following these best practices, the code becomes more readable and easier to manage and avoids introducing unnecessary complexity and confusion.

Non-Compliant Code Examples

func myfunction1(param int) {
	param, b := 1, 2
    param = 41
    param := 51
	param++
    param--
}

func (r *Type) myfunction2(param int) {
	param, b := 1, 2
    param = 41
    param := 51
	param++
    param--
}

Compliant Code Examples

func (r *Type) myfunction1(ctx context.Context) {
	span, ctx := tracer.StartSpanFromContext(ctx, "myfunction")
}

func myfunction2(ctx context.Context) {
	span, ctx := tracer.StartSpanFromContext(ctx, "myfunction")
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis