When doing multiple operations on a variable, is it considered bad practice to reuse the same variable name?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/421073

  •  21-03-2021
  •  | 
  •  

Question

A commonly repeated best practice is to not reuse local variables. However, when doing multiple small operations on the same variable, I struggle both with coming up with good names for all the variables, and I find that the multiple similar names hurt the readability.

Alternative 1: always create new variables

def clean_text(self, text: str) -> str:
    text_without_double_quotes = self._replace_double_quotes_with_single_quotes(text)
    text_without_double_quotes_and_foo = self._replace_foo_with_bar(text)
    text_without_double_quotes_and_foo_with_formatted_html = self._format_html(text)
    return text_without_double_quotes_and_foo_with_formatted_html

Alternative 2: reuse the variable

def clean_text(self, text: str) -> str:
    text = self._replace_double_quotes_with_single_quotes(text)
    text = self._replace_foo_with_bar(text)
    text = self._format_html(text)
    return text

Alternative 1 does have the advantage that I can set a breakpoint at the end of the method and inspect how the text was transformed in each step, but I am not sure if the tradeoff in readability is worth it or not.

Was it helpful?

Solution

The only real rule with variable names is that it should always be obvious what they refer to. Reusing a variable name can cause issues in some situations if the reassignment fundamentally changes what the variable is representing, but in your case that isn't happening.

Compare what you wrote to what this might look if instead of applying a string to a function, you were calling a builder object:

def clean_text(self, text: str) -> str:
    return text
        ._replace_double_quotes_with_single_quotes(self)
        ._replace_foo_with_bar(self)
        ._format_html(self)

Or if python had function piping semantics

def clean_text(self, text: str) -> str:
    return text
        |> self._replace_double_quotes_with_single_quotes
        |> self._replace_foo_with_bar
        |> self._format_html

This is, semantically, the same thing.

In just about any other situation I would avoid it as there usually isn't any advantage, though avoiding the absurdly long variable names you have in Alternative 1 could be considered worthwhile. In the specific case you show where no other variable assignments happen in the entire function, there's no problem and the meaning is obvious and easy to read.

OTHER TIPS

Neither choice is “best practice” or not; there is very little difference. In your case the function names are very clear, so you are fine using “text” as the variable names. If it wasn’t obvious what the effect of the functions is, you would use the variable names to document it.

If I find myself in this position it is usually because I have neglected a Clean Code guideline.

If the variable can be reused, do you have two functions in one? Do you have repeated code?

99% of the time you will be able to refector your code into multiple functions or a function that can be called multiple times...and achieve the same thing more readably and usually in less (or simpler) code.

On the other hand, if you are coding something that needs every ounce of performance, variable reuse saves memory allocation time ... and large combined functions saves stack operation time.

So, it depends! Clean, maintainable code versus fast code. They are often (but not necessarily always) mutually exclusive.

Licensed under: CC-BY-SA with attribution
scroll top