0

I am designing one application in which I have to read one file given by a user and calculate the total number of operations. There are two ways I can think of achieving this:

  1. Creating static variable that holds total operations. The benefit of this approach is that any where I need to perform some calculation based on total operations, I don't need to call function again and again which will read all the records from file and calculate number of operations.

  2. Create function which returns number of operations by looping through file.

Option 1 is better performance wise, but static variable are against principle of OOP. Kindly let me know if there is any other approach to this problem and if my understanding is correct.

Thanks in advance.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Addict
  • 803
  • 2
  • 9
  • 16
  • 2
    Any reason why you can't use an instance variable? – Sujith Surendranathan Nov 14 '12 at 19:52
  • If I use non static instance variable, even then i will have to call function that calculates/assign value to that instance variable every where i am using class. – Addict Nov 14 '12 at 19:57
  • Not really... Yould could calculate/assign the value once then set it to an instance variable. The next time you want the value, it will already be calculated. – jahroy Nov 14 '12 at 20:01
  • I'm not convinced we have enough information to help. My first reaction is that whatever is doing the looping and/or operation would simply increment an instance variable. – Dave Newton Nov 14 '12 at 20:02
  • 1
    Also... Where did you hear that static variables are against the principles of OOP. That seems kinda silly. There are definitely situations where static variables are quite effective. – jahroy Nov 14 '12 at 20:02
  • It seems to me the choice is between caching the "total number of operations" -- whatever that may be -- and computing them every time they are needed. – Miserable Variable Nov 14 '12 at 20:05
  • If I have three classes where i need total number of operations, then i will have to create objects in all the three classes and call the function which sets total number of operations to instance variable, so that is why i decided to make variable as static so that all the classes can directly use that value once calculated. To best to my knowledge static variables are global variables so they should be avoided in OOP – Addict Nov 14 '12 at 20:11
  • I think your solution sounds fine: use a static variable. This is a perfectly acceptable use of a static variable. Somebody told you something about static variables that was either incorrect or misinterpretted. – jahroy Nov 14 '12 at 20:18
  • http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – Addict Nov 14 '12 at 20:21
  • I believe you are misinterpretting that question. Read the answer by _irreputable_. Like with all things, static variables can be abused. That doesn't mean they should never be used at all. In my opinion, your question demonstrates the exact intended purpose of a static variable. – jahroy Nov 14 '12 at 20:24
  • That being said, there are probably many other optinos to achieve what you want. It would help if you share some code. It's very hard for us to help when we have no idea what your code looks like. – jahroy Nov 14 '12 at 20:29

3 Answers3

0

You may count the number of operations in a method of the instance _operationCounter of type OperationCounter which have an attribute of type int. This instance is itself an attribute of any other instance of any class (possibly several). To make sure you count only once, OperationCounter propose two operations : void makeCount( File in ) and int getCount(). A bad design is to count into int getCount().

Aubin
  • 14,617
  • 9
  • 61
  • 84
0

May be you should put together the suggestion given by user "crnlx" and your second option.

Have an instance variable and set its value to the value returned by your function.

So if your method signature is public void int getNumOfOperations(File fileName);

You would do the following.

int numOfOperations = getNumOfOperations();

This way you can reuse the variable again in your class without having to call the function again.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Susie
  • 5,038
  • 10
  • 53
  • 74
0

This is shading into a "functional programming" technique, but I would suggest that you write functions with the signatures:

int numberOfOperations(String line)
List<String> linesForFile(File file)

and then something like:

int totalOperations = 0;
for(String line : linesForFile(file)){
  totalOperations += numberOfOperations(line);
}
return totalOperations;

That way, you have easy-to-test individual functions, you aren't coupling the logical domain of "counting operations" with the specific source (a file), etc.

Larry OBrien
  • 8,484
  • 1
  • 41
  • 75