• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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()*
}
------------------------------------------------
0
Dawkins
Asked:
Dawkins
  • 4
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now