0

Which is faster and/or less resources consuming:

class Foo()
{
    public int value;
}

This way?

public int doSomeStuff(Foo f)
{
    return (f.value + 1);
}

public int doOtherStuff()
{
    ...
    Foo f = new Foo();
    int x = doSomeStuff(f);
    ...
)

or this way?

public int doSomeStuff(int v)
{
    return (v + 1);
}

public int doOtherStuff()
{
    ...
    Foo f = new Foo();
    int x = doSomeStuff(f.value);
    ...
)

In both cases, "doSomeStuff" will not change nothing in foo class. It just needs to know the "value".

1
  • According to the java naming convention, your class should be named Foo and not foo. It will make your code much more readable to fellow programmers. Commented Feb 15, 2012 at 12:09

7 Answers 7

3

They both perform the same, the same sequence of operations occurs. Your main concern is maintainability and sensible design here. Think carefully about which methods need which data and design it properly.

If you do have issues, you can optimise later. But you should always optimise last.

Sign up to request clarification or add additional context in comments.

1 Comment

"But you should always optimise last." +1
1

In terms of resource consuming, it is exactly the same. But the second option is clearly better in terms of programming because if doSomeStuff only needs value, then there is no point to passing f.

Comments

0

I don't think there is any performance difference at all. And Java compiler will optimize to the best one anyway...

Comments

0

Depends how often you're going to call doSomeStuff without calling doOtherStuff, but generally performance difference is negligible and if you only call doOtherStuff then they'll be equally performant.

Comments

0

Probably even better:

Decalre doSomeStuff() as a method of foo, and invoke: f.doSomeStuff()

It is much more readable and will be easier to maintain doing it so, since if you have a sub class of foo: Bar, and you want to calculate things a bit different - all you have to do is override doSomeStuff() in Bar

You should prefer readability over micro optimizations - let the compiler take care of those for you.

code snap:

class foo() {
    public int value; 
    public int doSomeStuff() {
       return value + 1;
    }
}

and:

public int doOtherStuff() {
    ...
    foo f = new foo();
    int x = f.doSomeStuff();
    ...
}

Comments

0

The difference between doing:

object.intvariable + 1

and

int + 1

is so negligible as to be irrelevant for real world apps. It's probably one or two more JVM opcodes to look up foo and find its value variable which is not worth mentioning. You'd never notice that unless you were trying to create a pseudo real-time app in Java (which is all but an exercise in futility).

However, that said, the way you are doing it is very bad. You should not be exposing value directly, but be using proper data encapsulation via getter and setter methods.

Comments

0

It does not matter from performance perspective. The recommendation is: do not think about pre-mature optimization. Think about correctness and good design of your code.

For example your code

  1. Does not follow naming conventions: class names must start with capital letter
  2. Contains public fields. It is forbidden. Use bean notation (getters and setters).
  3. Cannot be compiled (there is no type integer. Choose among int and Integer

1 Comment

1) OK, I always do this. In this case, I wrote this example pretty fast, not concering about those things... 2) OK, noted. 3) Again, it for illustration only. Thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.