• Post Reply Bookmark Topic Watch Topic
  • New Topic

object vs method  RSS feed

 
M Marsden
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Starting to program a dice roller that will handle polydice and just wondering what the implications are of putting number of sides and times rolled in methods versus in different constructors... thanks!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well what do you think they are?
How are you modelling your program?

do you want the concept of dieRoller.roll( numberOfSides, numberOfTimes )
or

Die die = new Die(numberOfSides)
die.roll(numberOfTimes).

If you wanted to roll a twenty sided dice five times would you write it as:
dieRoller.roll(20, 5);

Die d20 = new Die(20);
d20.roll(5)

It comes down to personal preference and how your program is going to be used.



 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you needed to save all the old rolls then it could make sense to make each roll an instance that you keep. Otherwise, nah.
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
It comes down to personal preference and how your program is going to be used.

I know it may seem like a strange point of view but I like to look at it as what the code wants to do. Kind of like how one sculptor would take a block of marble and try to find the figure inside it. His job wasn't to make the block of marble into a figure but rather to chip away bits of marble so that the figure already inside could be revealed.

With program code, my way of telling whether what I've written is what the code wants to do is to read it out loud and see if it makes sense. If it doesn't make sense, what I've written isn't really what the code wants to do. And "making sense" means that the code is easy to read and understand and easy to work with.

For example, this makes perfect sense:

However, this

not so much. I look at that and say, "Nah, that's not what the code wants to do." This makes more sense:

When the code does this, doing this makes sense, too:

With code, "chipping away unwanted marble" is called Refactoring. I don't know, maybe all that time spent pair programming with my buddy the Go Gopher has made me a little weird that way.
 
M Marsden
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I started with putting sides in the constructors thinking all these dice are different objects, but ran into some compiler problem (probably unrelated, possibly a missing bracket, who remembers at this point) so I rewrote it as a method which worked quite well, but now I've decided to rewrite as an external class file and I wondered if I should retry the original approach. Programatically, I don't foresee any issues with the approach but I wondered if there might be something I was overlooking. The issue of saving the die rolls certainly fits, I hadn't even considered that yet. Not sure why that would require using separate objects though, couldn't results just be stored in an array?
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Marsden wrote:I've decided to rewrite as an external class file

Not sure what you mean by "an external class file"

M Marsden wrote:
couldn't results just be stored in an array?

Makes sense to me. See the last code snippet in my last reply.
 
M Marsden
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By external file I just meant separating the dice.java to its own file rather than glommed in with the game file that I was working on.

Anyway, it did occur to me that if the dice were ever to appear graphically I might want to instantiate the sides. Didn't really plan on doing that before but once the idea came to me, it sounded sort of interesting.

And also that if the dice needed to be serialized (must be what the earlier comment meant, sometimes I'm daft.)
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Marsden wrote:By external file I just meant separating the dice.java to its own file rather than glommed in with the game file that I was working on.

[pedantic] You meant to say separate source file then. When you say "class file", people will take that to mean the .class file created when you successfully compile a Java source program (the .java file). [/pedantic]


Anyway, it did occur to me that if the dice were ever to appear graphically I might want to instantiate the sides. Didn't really plan on doing that before but once the idea came to me, it sounded sort of interesting.

And also that if the dice needed to be serialized (must be what the earlier comment meant, sometimes I'm daft.)

You could give it a shot but I think you're overthinking it. I don't think that rendering a graphical representation of a die requires separate objects for each face. If anything, I would think you'd need something like a DieRenderer class that calculates all the shapes and angles you need to draw a representation of an N-sided solid or if you want to start simpler, an N-sided polygon to represent the face that's up.

And why would you need to serialize Dice? You could just send over the number of sides or the side that's up and either one would be an Integer, which is already Serializable.

As a programmer, you should always ask yourself "What is the simplest thing that could possibly work?" Take this to heart because it can save you and others a lot of pain and suffering.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!