File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes DiceGame Code - which is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DiceGame Code - which is better?" Watch "DiceGame Code - which is better?" New topic
Author

DiceGame Code - which is better?

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
They are quite alike, but i believe one is better writte.
Which should i use, and can i improve on it?

code 1



code 2:

[/size]


Stephen Foy - Microsoft Application Development Consultant
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Anything please??
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

Stephen,

Honestly, your code still needs a bit of work to make it clearer and easier to follow. As I mentioned in another thread, you should write code in such a way that it reads the way it is intended to work. Each method should have a single responsibility and consistent level of abstraction and detail. Assign methods names that reflect what they actually do. Your rollDice() method does not actually just roll the dice; it does almost everything.

My first-cut implementation of rollDice() would probably be something like:



But then again, this code is very procedural. If I really wanted OO code, I might try something like:



Notice that these first methods do not have much detail but they give you a high-level view of what happens in the program. If you need more detail, you would drill down to other methods. Those methods would have a lower level of abstraction and more detail. The trick is getting the right balance of detail and abstraction.


Junilu - [How to Ask Questions] [How to Answer Questions]
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Thanks im gonna give it another bash, i think my problem is we havnt started OO programming yet, the ways i ave used are the ways we have been shown so far. Only been in java for 2-3 months. My problem is, linking methods, like you have there methods called in other methods.

How?

Thanks for the advice.
 
Don't get me started about those stupid light bulbs.
 
subject: DiceGame Code - which is better?