internetsistemas / guides

A guide for programming gracefully.
25 stars 13 forks source link

Default parameters value are toxic for methods #24

Open felipefontoura opened 9 years ago

felipefontoura commented 9 years ago

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 commented 9 years ago

It's amazing @felipefontoura :dancer: 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 commented 9 years ago

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... :v:

felipefontoura commented 8 years ago

Hey guys! Can I close or implement it?

brunoocasali commented 8 years ago

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