I have just finished one method and I was not happy with the code. Then I put a comment on top of the method “Far from perfect….”.
Then I read the class – it was ugly.
here is the before
def validate return if is_email == false found = InternetComunicatorType.find(:first, :conditions => { :is_email => true, }) return unless found if found.id != id errors.add(:is_email, "We have already email type") return end end
here is the after
def validate if is_email found = InternetComunicatorType.find(:first, :conditions => { :is_email => true, }) if found and found.id != id errors.add(:is_email, "We have already email type") end end end
The result is much far readable