Simple code efficiency question - should I limit function calls?

Hi all,

Which of these is better:

-------------Version 1-----------------------
public class GraphCanvas {

int xAxisMaximum;

public GraphCanvas(Graphable data) {
    this.xAxisMaximum = data.getLength();
}
... *loads of methods which perform calculations using xAxisMaximum*
}
------------------------------------------------

-------------Version 2-----------------------
public class GraphCanvas {

Graphable data;

public GraphCanvas(Graphable data) {
    this.data= data.getLength();
}
... *loads of methods which perform calculations using data.getLength()*
}
------------------------------------------------
DawkinsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tdisessaCommented:
This one, if you change it to this:

public class GraphCanvas {

Graphable data;

public GraphCanvas(Graphable data) {
    this.data= data;
}
... *loads of methods which perform calculations using data.getLength()*
}

that way if data.getLength() changes it value, your loads of methods will use the correct length.
0
sudhakar_koundinyaCommented:
if nothing to do with Graphable class then   this.xAxisMaximum = data.getLength();

is efficient because you need not to maintain the Graphable  object in  memory until  all the methods are executed

0
sudhakar_koundinyaCommented:
This model is perfect if you think the value of x that was set in MyObject  instance won't  be changed at runtime. Here you need to deal with only x but not My Object instance

publc class A
{
         int x;
              public someMethod(MyObject obj)
              {
                     x=obj.getX();
              }    
}



This model is perfect if you think there is possibilty of changing the value of x at runtime and at the same time if you want to deal with some other methods

publc class B
{
              MyObject obj;
              public someMethod(MyObject obj)
              {
                    this.obj=obj;
              }    
              public void someothermethod()
               {
                       int x=obj.getX();
                      int y=obj.gety(); //and so on
               }
}
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

tdisessaCommented:
Actually, I agree with sudhakar more than my initial comment.  My only comment now is
how OO is your program going to be?  Here are some questions to consider:

1.  Is "xAxisMaximum" (for your first example) valid state description for "GraphCanvas"?
2.  Should the Graphable object be the ultimate source for "getLength()"?
3.  What is the relationship between Graphable and GraphCanvas?
0
DawkinsAuthor Commented:
>  This model is perfect if you think there is possibilty of changing the value of x at runtime and at the same time if you want to deal with some other methods

What about if there isn't a possibility of changing the value of x at runtime, but I do want to deal with lots of other methods?  

Graphable is actually an interface which allows implementing objects to be turned into a time series style graph.  I suspect I'm not using interfaces correctly here but oh well! :/

public interface Graphable {
  int getLength();  // returns a length for the "time series"
  TimeSeriesData getData(int index);
  int getNumberOfLines();
}
0
sudhakar_koundinyaCommented:
OK  

Let me try to explain

Let you have a class something like this
class A
{
     int getX(){return 0;}
     int getY(){return 0;}
}

if you want to deal with all methods of A in class B then this is perfect solution though if there isn't a possibility of changing the values  at runtime

class B
{
    A someObj;
    void someMethod(A someObj)
     {
        this.someObj=someObj;
     }
    void someMethod1()
    {
               someObj.getX();
               someObj.getY();
     }
}



if you want to deal with only deal with method getX() and not others then this is perfect solution

class C
{
    int x;
    void someMethod(A someObj)
     {
        this.x=someObj.getX();
     }
    void someMethod1()
    {
             
              //do what ever u want here with x
     }
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
>> This model is perfect if you think there is possibilty of changing the value of x at runtime

This situation quite happens when u use classes in threads
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.