Skip to content

Instantly share code, notes, and snippets.

@jamesbebbington
Created August 19, 2009 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesbebbington/170388 to your computer and use it in GitHub Desktop.
Save jamesbebbington/170388 to your computer and use it in GitHub Desktop.
module ApplicationHelper
# Why is this a helper - should be method in User model
def get_name(first_name, last_name, middle_name)
full_name = "" # Why's this here? It's never used
if middle_name != "" && last_name != "" && first_name != "" then # Doesn't handle nils
return first_name + " " + middle_name + " " + last_name
# Ruby intrinsically returns, idomatic ruby coder wouldn't bother with return
end
if last_name != "" && first_name != "" then
# Conditional statments both overly verbose, fragile (doesn't handle all cases - what if validation changes and we don't require a last_name - method then returns nothing useful)
return first_name + " " + last_name
end
# Not DRY, if we change f.e. first_name to be forename, then four seperate lines are affected.
# What if we were to want to include a 'title' atribute i.e. 'Mr.'? The structure of the method is very inflexible
end
end
# Also anoyingly verbose to call:
# get_name(@user.first_name, @user.last_name, @user.middle_name)
# Better version:
class User < ActiveRecord::Base
def full_name
[first_name, middle_name, last_name].delete_if{ |n| n.blank? }.join(' ')
end
end
# This is called by:
# @user.full_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment