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

keyword arguments for boolean arguments #509

Merged
merged 1 commit into from
Sep 15, 2018

Conversation

ruba-ruba
Copy link
Contributor

I found that i often see methods which consume a boolean argument and depends on it method define some logic. However i every time have to check the source because ...

some dummy example

def load_posts(include_unpublished = false)
  if include_unpublished?
    MyDataStorage.load_all
  else
    MyDataStorage.published
  end
end

# in code method invoked as
1) load_posts(false) 
2) load_posts
3) load_posts(true)
# and it is not meaningful for me

# however if we define method like => def load_posts(include_unpublished: false)
4) load_posts(include_unpublished: true)
# now i know what will happen without reading source code of method

i also have the same thoughts about numbers as arguments, and keyword arguments is a nice thing to use.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 2, 2017

I think a much more general point can be made - keyword arguments usually give the best readability. Now that Ruby 1.9 is not supported anymore and few people still use it I'd wouldn't suggest the usage of option hashes.

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

Successfully merging this pull request may close these issues.

2 participants