When should an class not be a class.
Monkey Archive Forums/Monkey Discussion/When should an class not be a class.| 
 | ||
| was looking at some old code and found this. Have no clue what I was thinking when I wrote this, not even sure what I was planning to do with it, but based on all I had done I have to ask myself if that's it, then why did I make it a class, why is not a local or global. So Monkey coders, when is a class not a class ? when its really a global in disguise. 
class OP
  field value:float
  
  method new()
  end method
  method SetOP(_op)
    value=_op
  end method
  method GetOP()
    return _value
  end method
 | 
| 
 | ||
| Were you going to have multiple instances of OP? The class itself looks fine and very much a POJO (Plain Old Java Object) or Bean. | 
| 
 | ||
| no idea mate, I often sit at 2 or 3am and have this amazing idea, I block in a little code before I cave and goto bed, when I get up in the morning I almost ALWAYS have no clue what the hell I was thinking, some times I come up with little gems that my sleep deprived brain just plucks out of god knows where, but most of the time its nonsense, like the above. | 
| 
 | ||
| Was it some kind of boxing effort?  Like you wanted to pass a float around as an object? More likely though it was intended to be some more complicated class that you did not finish. Maybe for doing some kind of math. | 
| 
 | ||
| You know what, it might have been something to do with boxing, still no clue what it was but I do recall messing with boxing a little. I normally comment the hell out of my code but some where between Midnight and 1am I seem to stop commenting, and anything I write after 1 in the morning is always a complete mystery to me lol, it's like there is some little switch in my head that just toggles off or onto auto pilot mode as soon as it passes Midnight. | 
| 
 | ||
| Well, for me I implement a class in I need an object to have several different sub-values, that cannot be contained in a single value. In your example you could have it as a global variable, but what happens if you add a value2, value3 and value 4. That would then mean that you would have 4 global variables. If you would have made it into a class you would still have one global called for example "op". You would then be able to called op.value1, op.value2, op.value3 and op.value4. Of course this is the simply answer. | 
| 
 | ||
| I've taken to sometimes using an int array when I'm likely to have a few pieces of data but I'm not sure how many.  Sort of like a union in C, but not to save memory so much as to save field names.  E.g. an item class in a roguelike which might have several parameters depending on the item type. |