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

Default parameters value are toxic for methods #24

Open
felipefontoura opened this issue Jun 10, 2015 · 4 comments
Open

Default parameters value are toxic for methods #24

felipefontoura opened this issue Jun 10, 2015 · 4 comments
Labels

Comments

@felipefontoura
Copy link
Contributor

Hello Folks,

In our internal projects I see default value for the method's parameters used frequently, and I think that it can be a toxic for a best understanding, because normally it's means Business Rules but hidden.

For example:

class Installment < ActiveRecord::Base
  def self.overdue(date = DateTime.now)
    # codes...
  end
end

And we can call it using:

Installment.overdue

Ok, it's will run gracefully...

But the next Developer need to read the model's code to understand that overdue need to receive a date parameter, and if this date isn't .

In our method's guide (https://github.com/internetsistemas/guides/tree/master/style-guides/ruby#methods) we encourage devs to use named parameters for best understanding.

What do you think?

@brunoocasali
Copy link
Contributor

It's amazing @felipefontoura 💃
But for this example, we can't do anything using or no named parameters the call of method will be:

Installment.overdue

I thought it's a good ideia, standardize method names using named params, when it uses more than one param!

@felipefontoura
Copy link
Contributor Author

Yes @brunoocasali!

Named params need to be used with caution, but it really helps! Ruby On Rails use it every time like a boss!

In this case, I prefer:

Installment.overdues_for somedate

or:

Installment.overdues from: datetime

Wherever, but I have been used first option... ✌️

@felipefontoura
Copy link
Contributor Author

Hey guys! Can I close or implement it?

@brunoocasali
Copy link
Contributor

I wish we could implement the follow guideline:

When you need to use more than one parameter, please add named parameter pattern like:

Installment.overdues from: datetime

But, when you need to use just one parameter:

Installment.overdues somedate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants