View Single Post
Partial
Stallion
 
Join Date: Feb 2006
Location: Milwaukee
 
2009-10-04, 20:29

Code:
if ( foo ) { bar; }
This is a best practice. On a small project like yours, it probably doesn't matter, but it is much better to get into best practices when you're learning.

Trust me. I'm working on this awful, awful project with one 7000 line class, and probably a total of 50k lines of shitty code. It's awful. The worst part is in they rarely open or close ifs with brackets and then they don't do any indenting, so it's very difficult to follow whats going on.

Worst. Project. Ever.

One thing you could do to get into some really good habits is to look into the java coding standards and install the CheckStyle plugin into Eclipse.

Also, you should pass most of those values into your method instead of accessing as local variables that may or may not be set correctly at they time they're being called. Dependency injection is another practice I'm big on.

Not saying it's how you have to do it, but this is how I'd probably write the below code. I'm definitely making some assumptions about the requirements.

Code:
public int runEvent(double predators, double competitors, double food) { final int PREDATOR_VALUE = 0; final int COMPETITOR_VALUE = 1; final int FOOD_VALUE = 2; final double TOTAL = calcTotal( predators, competitors, food ); final double P_THRESHOLD = calcPredatorThreshold( predators, total ); final double C_THRESHOLD = calcCompetitorThreshold( P_THRESHOLD, TOTAL, competitors ); final double EVENT_SEED = Math.random(); if ( EVENT_SEED <= P_THRESHOLD){ return PREDATOR_VALUE; } else if ( EVENT_SEED <= C_THRESHOLD ){ return COMPETITOR_VALUE; } else{ return FOOD_VALUE; } }

...and calling/e-mailing/texting ex-girlfriends on the off-chance they'll invite you over for some "old time's sake" no-strings couch gymnastics...

Last edited by Partial : 2009-10-04 at 20:45.
  quote