Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid local variables that are only used in return statements #471

Closed
sobolevn opened this issue Jan 31, 2019 · 8 comments · Fixed by #551
Closed

Forbid local variables that are only used in return statements #471

sobolevn opened this issue Jan 31, 2019 · 8 comments · Fixed by #551
Labels
help wanted Extra attention is needed level:starter Good for newcomers pr-available pr-merged rule request Adding a new rule

Comments

@sobolevn
Copy link
Member

Rule request

Thesis

Sometimes I see code like this:

def function():
     ...
     some_value = ...
     return some_value

What the point in creating this intermediate variable? Why do we need it?

Reasoning

This is done for consistency and more readable source code.

@sobolevn sobolevn added help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule labels Jan 31, 2019
@loucadufault
Copy link

Can I start working on this ? To clarify the fix would be to remove all these intermediate variables in all function returns?

@sobolevn
Copy link
Member Author

sobolevn commented Feb 8, 2019

@loucadufault you are welcome!

The fix would be to return a direct value.

Wrong:

def some_function():
     some_value = 1
     return some_value

Correct:

def some_function():
     return 1

@Chinmaygoyal
Copy link
Contributor

Can I work on this issue?

@SizovIgor
Copy link
Contributor

@Chinmaygoyal I am on finish line to complete this issue

@sobolevn
Copy link
Member Author

@Chinmaygoyal feel free to take #545

@DmytroLitvinov
Copy link
Contributor

DmytroLitvinov commented Apr 16, 2019

Hello @sobolevn
The immediate value is good in the next case:

def function(a):
     result = None
     if a:
         ...  # some actions
        result = 123

     return result

As for me, it is also understandable from the naming of value what we will return after calling of function.

@sobolevn
Copy link
Member Author

sobolevn commented Apr 16, 2019

@DmytroLitvinov yes, this code is valid.

What do you mean by:

As for me, it is also understandable from the naming of value what we will return after calling of function.

@SizovIgor SizovIgor mentioned this issue Apr 21, 2019
sobolevn pushed a commit that referenced this issue Apr 21, 2019
* Update noqa.py

* Add files via upload

* Delete out.log

* Update CHANGELOG.md

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Fix minor things

Fix function return type 
Change dict.setdefault() on collections.defaultdict(list)

* Fix minor things

* Add files via upload
@DmytroLitvinov
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed level:starter Good for newcomers pr-available pr-merged rule request Adding a new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants