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

Optional arguments #110

Open
GatorQue opened this issue Jan 26, 2018 · 2 comments
Open

Optional arguments #110

GatorQue opened this issue Jan 26, 2018 · 2 comments

Comments

@GatorQue
Copy link
Contributor

When a function/method has arguments with a default value assigned all other callers of the function/method should explicitly specify the optional argument name.

def foo(bar=None, zoo=None):
pass

Bad:
foo(1, "lion")

Good:
foo(bar=1, zoo="lion")

Rationale:
Additional optional arguments will likely be added to the function/method in the future and the callers won't need to be hunted down and updated to account for the new optional argument added.

Example:
def foo(flag=True, bar=None, zoo=None):
pass

Bad:
foo(1, "lion") # unexpected result since flag is now assigned 1 and bar is now assigned "lion"

Good:
foo(bar=1, zoo="lion") # No change necessary - behaves the same as before

@adewes
Copy link
Member

adewes commented Jan 29, 2018

I partially agree, as naming variables explicitly makes the code clearer for functions that have many optional arguments. Would you be able to write an anti-pattern for this? Could be a great addition to the "maintainability" section, you can use e.g. this file here as a template:

https://github.com/quantifiedcode/python-anti-patterns/blob/master/docs/maintainability/using_single_letter_as_variable_name.rst

Looking forward to reviewing that PR (and don't worry about grammar / presentation, we can fix that), thanks for contributing!

GatorQue added a commit to GatorQue/python-anti-patterns that referenced this issue Feb 10, 2018
@GatorQue
Copy link
Contributor Author

Sorry it took me so long to put this up. I hope it fits your anti-pattern format and explains the concept clearly enough.

adewes added a commit that referenced this issue Feb 28, 2020
add name optional arguments maintainability anti-pattern. Fixes issue #110
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants