Adding unnecessary inheritance is far worse than the original problem.
In my experience observing (pragmatically, never dogmatically) OO principles like SOLID reduces entropy. Thus are worth applying.
Personally I prefer polymorphism and null object pattern over conditionals to deal with edge cases, though if you have a leaky abstraction in the first place no principle or pattern is going to save you!
Every time I look at the code I have to 'recompile' it in my brain, just because it's doing such a simple thing in such a darn complex way.
class User
def post_created(post); end
end
class TwitterUser < User
def post_created(post)
post.send_to_feed(twitter)
end
end
class EmailUser < User
endCode should be simple and easy to understand. Observers add significant complexity and make your code vulnerable to unnecessary bugs because the code that acts on your objects is invisible to the normal control flow of the program and those who write or maintain it.
My stance is that things like sending email should be explicit calls so they are obvious. Sending email when registering a new user, for example, should be in the if user.save branch in your controllers or, if you have a more SOAish app, in the service that creates users.
def create user = User.create(params[:user])
if user.save
Emailer.new_user_email.deliver
render 'welcome'
else
render 'oh shit'
end
endOr, refactor that out a bit (ONLY IF NECESSARY!)
def create user = UserService.create_user_from(params[:user])
if user
render 'welcome'
else
render 'oh shit'
end
end
Honestly though, if I saw the not-so-good code, I'd leave it alone. It takes a pretty big justification to double the code for the same features.