Skip to content

Instantly share code, notes, and snippets.

@jonahoffline
Last active December 11, 2015 01:59
Show Gist options
  • Save jonahoffline/4527823 to your computer and use it in GitHub Desktop.
Save jonahoffline/4527823 to your computer and use it in GitHub Desktop.
Refactoring examples using code taken from revXbot.
#Original
# If you were down with this you are a code martyr waiting for a stab.
# Ruby was born so that code could be drier than your Grandma.
# The first any? method is as useless as a PHP Senior Developer so I ain't worried!
# The @offenders hash always get saved using the store method so cut that bitch off!
if @offenders.any? && @offenders.key?(nick)
@offenders.store(nick, @offenders.fetch(nick).next)
else
@offenders.store(nick, 1)
m.reply "#{nick} arranca, pal carajo a otro canal a hablar de esa mierda mamabicho."
end
@offenders.each do |_, count|
if count >= 5
m.channel.kick(m.user.nick, "Get the fuck out bitch!")
m.reply "Goddamn fool had it coming. I warned him #{count} times"
else
m.reply "Warning: ##{count}. (Just #{5 - count} more and you'll feel my wrath bitch.)"
end
end
#Refactor #1
# Less code is always a plus but there's something wrong with that else return.
# It looks lonely and ugly; just like the girl that nobody wants to fuck.
result = if @offenders.has_key?(nick)
@offenders.fetch(nick).next
else
1
end
@offenders.store(nick, result)
m.reply "#{nick} arranca, pal carajo a otro canal a hablar de esa mierda mamabicho."
#Refactor #2
# I could live with this version but this could read better and not kill my code boner.
# So, anybody resistin' can goddamn my ass kissin!
# Dear Ternary, you ain't tender anymore so get the fuck off my code.
new_count = @offenders.has_key?(nick) ? @offenders.fetch(nick).next : 1
@offenders.store(nick, new_count)
m.reply "#{nick} arranca, pal carajo a otro canal a hablar de esa mierda mamabicho."
# We'll also take care of this little clusterfuck that followed (in the original version):
@offenders.each do |_, count|
if count >= 5
m.channel.kick(m.user.nick, "Get the fuck out bitch!")
m.reply "Goddamn fool had it coming. I warned him #{count} times"
else
m.reply "Warning: ##{count}. (Just #{5 - count} more and you'll feel my wrath bitch.)"
end
end
#Refactor #3
# Drier than a nun. This bitch is flowin' straight from the survival scrolls!
# Removed the has_key? method and replaced it with some fly, pimped-ass code inspired by the power of the Holy Ghost.
# By who power? The || operator motherfucker.
# Meet the control flow hitman. Got any shady method returning nil? Child-threads gone wild? Promiscuous instance variables?
# Hire this mac-daddy; it will put a cap on nil's ass and provide you with the goods.
new_count = @offenders.fetch(nick).next || 1
@offenders.store(nick, new_count)
m.reply "#{nick} arranca, pal carajo a otro canal a hablar de esa mierda mamabicho."
# While you may not agree with this change (and just to let you know: I don't give a fuck)
# Your thoughts ain't my thoughts so gem install hatorade to your wetware (the brain...)
# Alright now, let's stay focused, let's stay dry. Scroll down because this shit is about to go down.
@offenders.each do |_, count|
reason_msg = if (count >= 5)
m.channel.kick(nick, "Get the fuck out bitch!")
"Goddamn fool had it coming. I warned him #{count} times"
else
"Warning: ##{count}. (Just #{5 - count} more and you'll feel my wrath bitch.)"
end
m.reply reason_msg
end
#Refactor #4
# Coding is not a competition, it's all about cooperation of humans and machines. Stupid-ass bitch!
# So we got rid of the each method, and what's the result? code runs faster than a nigerian chicken!
reason_msg = if (count = @offenders.fetch(nick).to_i >= 5)
m.channel.kick(nick, "Get the fuck out bitch!")
"Goddamn fool had it coming. I warned him #{count} times"
else
"Warning: ##{count}. (Just #{5 - count} more and you'll feel my wrath bitch.)"
end
m.reply reason_msg
#Refactor #5 (March 24th)
# This little bitch had a nasty bug. Paranthesis needed to close after 'to_i' method
reason_msg = if (count = @offenders.fetch(nick).to_i) >= 5
m.channel.kick(nick, "Get the fuck out bitch!")
"Goddamn fool had it coming. I warned him #{count} times"
else
"Warning: ##{count}. (Just #{5 - count} more and you'll feel my wrath bitch.)"
end
m.reply reason_msg
@whatAboutJohn
Copy link

Awesome, aewesome to the max

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment