• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which of two approaches is the most effective?

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is near on philosophical.

In my project here I have a lot of classes depicting objects moving about in a 2D environment.
One class X has (for example) three attributes, a, b and c. If I change one attribute the other two are also influenced.

Here are two possibile approaches - which is the right one?

1. caller takes responsibility


and the caller does this :


!!! BUT !!! the caller must also determine how b and c
are modified by the change in a.
It is the caller's responsibility to make the appropriate changes.

------------------------ OR ---------------------------

2. the object being called takes responsibility


this way if the caller does :

then he is not given responsibility forall that calculating

!!! BUT !!!
if the caller does :


then a lot of code is being performed for nothing, namely
: make the appropriate changes in b and c
: make the appropriate changes in a and c

each way has advantages and disadvantages.
However, what is the real cool programmer direction?

Thanks muchly.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd go for the second option, but with a few additional methods for bulk settings:
Your set of calls can then be replaced by setAandB(10, 10).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Rob.

The biggest problem with the first approach is that this leaves it up to the user of class X to keep the state of an X object consistent. That is a very brittle and error-prone design: it is far too easy to make a mistake for users of class X so that you get objects that are in an inconsistent state. A class should be responsible for keeping its instances in a consistent state.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, why should the three attributes influence each other anyway? You should strive to make fields independent from each other.

Can you explain what the three attributes are supposed to signify?
 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One class is a MyVector class. It has a lot of attributes.

it has a source point,
an end point,
a direction(resembling a point..two doubles)
and a scalar magnitude.

if I drag the object by the source then the magnitude and direction remain fixed
but I must change the coordinates of both source AND end point

if I drag the end point, I change the direction and magnitude as well as the end point--the source remains fixed.

This is why

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need to store all that?

If you have a start point, direction, and magnitude, you can calculate the end point. Or, if you have the start and end point, you can calculate the direction and magnitude.

I would argue that you shouldn't store anything you can calculate from existing data - it's a waste of space, it's prone to generating inconsistent data, and can force the user to do unnecessary work.

 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting point.

You have reduced the list by one with your suggestion. Just one. Is it really worth that?

I was getting tired of writing "is the cursor in a position : magnitude*direction-vector.getX(), magnitude*direction-vector.getY() added to source point components X and Y".

Isn't saving all this calculating a factor in speeding up the application as well as making the classes more useful?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO, much of software design requires you to strike a balance between many variables - speed, memory usage, clean code, etc. Without knowing much (ok, anything) about your application or requirements, I would strive for cleaner code over speed. You would have to have rather compelling evidence showing me that speed is an issue when we calculate these things. In other words, I doubt that the performance hit will be noticeable. I don't think this is a very complicated calculation, and I doubt you need to calculate it that often. If you save a thousand users a microsecond a thousand times a day, you save about 6 minutes a year of user time. Odds are rather high that you will spend more than 6 minutes maintaining that code at some point, so the simpler it is, the better.

If you feel you must store all that data (and again, there could be a reason why), it does make sense to update the calculated values yourself - you can't rely on someone else to maintain the data integrity. I would agree with Rob's suggestion of providing a way to update more than one at once, but you should limit it. I would NOT give them a way to update the end-point AND the magnitude/direction, as this could lead to some kind of conflict.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether you choose to use an extra field to avoid doing calculations or not (I strongly suggest you don't), you should never let the internal state of a class be the responsibility of another class, unless that class is internal itself.
 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to all for your feedback.
It has helped.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic