File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DiceGame Code - which is better?" Watch "DiceGame Code - which is better?" New topic

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:


Stephen Foy - Microsoft Application Development Consultant
Stephen Foy
Ranch Hand

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

Joined: Feb 26, 2001
Posts: 6529


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.


Thanks for the advice.
I agree. Here's the link:
subject: DiceGame Code - which is better?
jQuery in Action, 3rd edition