Created
October 25, 2011 21:08
-
-
Save davelnewton/1314284 to your computer and use it in GitHub Desktop.
Quick voting refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def vote_yes | |
vote(:up_vote) | |
end | |
def vote_no | |
vote(:down_vote) | |
end | |
private | |
def vote(up_or_down) | |
@project = Project.find(params[:id]) | |
current_user.send(up_or_down, @project) | |
flash[:success] = 'Thanks for voting!' | |
rescue MakeVoteable::Exceptions::AlreadyVotedError | |
flash[:error] = 'You already voted!' | |
end | |
redirect_to @project |
Author
davelnewton
commented
Oct 25, 2011
via email
No problem :)
In general, whenever there's a bunch of code that looks or acts the same,
there's an opportunity to clean things up.
It also makes testing an understanding things easier: with this refactoring
it's immediately obvious that there's no difference between how yes and no
votes are handled. In the original version anyone reading the code would
need to study both methods to see if they differed, like redirected to
different pages, used different messages, etc.
Dave
On Oct 25, 2011 6:26 PM, "Raphael Caldas" <
[email protected]>
wrote:
… Amazing! Thanks a lot! Still so much to learn, but thankfully, with people
like you, the journey is much smoother :)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1314284
I see that... If it's not too much too ask, would you recommend any books from which I could get this kind of insight? Not properly about Rails or Ruby (methods, syntax, etc.), but essentially how to write beautiful code?
There are a few, but in general:
Clean Code by Robert Martin: Gave copies to all my devs for Christmas last
year (purely selfish motivations--better code makes my life easier ;) I
really like this book.
http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
Refactoring by Martin Fowler: I'm not sure how necessary this one is, but
when I read it what seems like so long ago, it changed the way I approached
coding.
http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672
Beyond that it kind of depends what you're interested in.
One thing that really matters, particularly in Ruby/Rails (and in dynamic
languages in general) is "metaprogramming", writing programs that write
programs, often at runtime. This is how Rails does all its magic, like how
"belongs_do :user" adds methods to ActiveRecord models.
The ability to think in terms of metaprogramming, and high-level
abstractions (like design patterns) is one of the things I think separates a
great developer from an average one. One book that's both practical, and
really demonstrates the power of "putting things in the right place" is
Rails AntiPatterns.
http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814
There are a bunch of others, Metaprogramming Ruby comes to mind.
There's no end to it!
Dave
…On Tue, Oct 25, 2011 at 7:06 PM, Raphael Caldas < ***@***.***>wrote:
I see that... If it's not too much too ask, would you recommend any books
from which I could get this kind of insight? Not properly about Rails or
Ruby (methods, syntax, etc.), but essentially how to write beautiful code?
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1314284
You're an amazing guy. Thanks a lot for putting this effort in helping out a stranger. Whenever I can do anything for you, please just ask.
I'm just a guy that enjoys programming, and helping other people enjoy it :)
Dave
…On Tue, Oct 25, 2011 at 7:42 PM, Raphael Caldas < ***@***.***>wrote:
You're an amazing guy. Thanks a lot for putting this effort in helping out
a stranger. Whenever I can do anything for you, please just ask.
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1314284
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment