monterail / guidelines

[DEPRECATED] We are Ruby on Rails experts from Poland. Think hussars. Solid & winged. These are our guidelines.
71 stars 17 forks source link

Try to avoid calling self explicitly on reads #143

Closed sheerun closed 11 years ago

sheerun commented 11 years ago

I understand sometimes using self is necessary:

def balance=(balance)
  self.balance = balance
end

but for reads it usually can be omitted, what makes code more readable.

An example:

def is_pay_with_balance?
  self.cooleaf_payment? && self.balance > 0 &&  self.payment_applied > 0
end

vs

def is_pay_with_balance?
  cooleaf_payment? && balance > 0 && payment_applied > 0
end
szajbus commented 11 years ago

:+1:

jcieslar commented 11 years ago

for me is less readable, but I know what is your point.

I prefer use self to indicate what is tmp variable and what is not. e.g

def balance?
  old_balance = ... # some calculations
  new_balance = ... # some calculations
  self.amount > old_balance
  ....
end
jandudulski commented 11 years ago

Omit self when it is absolutely not needed and when it would make code less readable.

I think @jcieslar's exmaple is a good example when self improves readability (apart of the fact, that I would move old_balance and new_balance to separate methods ;))

teamon commented 11 years ago

@sheerun PR