Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i happen to be kinda picky when programming something big. I try to find the best way to do it it terms of speed and complexity. Since i've been learning Rails the previous 3 months, i try to find the best techniques for everything. I would like to ask you how you would go about writing some code like this :

   @defender = User.find_by_id(user_id)
   @attacker = current_user.clone
   @attacker_starting_attribs = current_user
   @defender_starting_attribs = @defender.clone
   @defenderWeapon = @defender.getEquippedWeapon
   @attackerWeapon = @attacker.getEquippedWeapon
 @combat = Combatant.fight(@attacker, @defender)

This code is about the battle outcome between two persons in a browser game. The code works well, but i've some problems in terms of good coding. In fact, i know that my code is bad here, that's why i ask you what a better version would be. Let me explain what happens in this code.

@defender is given by user_id, so i guess that this part is needed. Now, in @attacker i'm cloning the current_user. The reason is that Rails works on objects and current_user will be changed inside Combatant.fight. I need both the new hp and the old hp and that is why i'm cloning the object. The defender and attacker starting attribs illustrate that concept. Now, i get the weapons in instance variables, so that i can get their information inside the final view.

However, the weapons are needed inside the fight function and i execute the same getEquippedWeapon twice again inside fight(). I was not so comfortable with something like fight(@attacker, @defender, @attacker_weapon, @defender_weapon), but i don't also like the idea of repetition. So, i would like an opinion on that.

@combat is a hash containing the result of the combat. Fight happens and i get that hash back in the view.

I'm not pleased with my coding on that stage and i want your opinion. How would you do it ? Is there maybe a design pattern for that ? Please tell me your opinion.

Thanx :)

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Without creating a fighter class, I would just turn the two into hashes:

@defender[:attribs] = User.find_by_id(user_id)
@attacker[:attribs] = current_user.clone

@defender[:starting_attribs] = @defender.clone
@attacker[:starting_attribs] = current_user

@defender[:weapon] = @defender.getEquippedWeapon
@attacker[:weapon] = @attacker.getEquippedWeapon

And send all the data the same way:

@combat = Combatant.fight(@attacker, @defender)
share|improve this answer
1  
Use symbols for the keys, like :attribs and :weapon. –  Jeremy Heiler Jan 28 '11 at 15:45
    
Good point; haven't touched Rails in a while. –  Stuart Jan 28 '11 at 16:30
    
indeed, that seems a nice alternative. thanx ! –  user990 Jan 28 '11 at 18:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.