Domanda

Wow, that title sounds confusing. Hopefully this clears it up. I have two tasks that I can preform, foo and bar. Currently, I preform them like this:

public void foo() {
  foovar = true;
}
public void bar() {
  barvar = true;
}

now I need a method to preform both of these things. As i see it, I have two choices:

1:

public void foobar() {
  foovar = true;
  barvar = true;
}

2:

public void foobar() {
  foo();
  bar();
}

What should I do? In the context of Java, what is more efficient? Other languages?

È stato utile?

Soluzione

The answer depends on a lot of factors. If you are literally doing nothing but setting a single field to a value, and if your class is an internal part of a cohesive block of code, then there's not much difference between the approaches.

If you are instead just showing an example, but the actual methods do more than shown here, or if the class is exposed as public API towards other modules/parts of your wider picture, then you should definitely prefer reusing the code by calling the two methods.

With respect to efficiency, you should never worry about it unless you care whether the method invocation overhead amounts to 0.2 ns or as much as 0.4 ns.

Altri suggerimenti

The second option is best by far.

Suppose you would like to change the behaviour of the function? With the first option you would now have to change two pieces of code. Using the second option you only have to change the method.

Suppose you would like another method doing foo() and bar() and baz(). Now you would have even more code duplication when choosing for option one:

public void foobarbaz() {
    foovar = true;
    barvar = true;
    bazvar = true;
}

While

public void foobarbaz() {
    foobar();
    baz();
}

Would give you way more flexibility and less code duplication.

Though both are doing the same thing, I'l go with

public void foobar() {
  foo();
  bar();
}

Since later if I want to update only barvar and not foovar, then I need to write a new method.

And I prefer to write the smallest methods I can to make my code into units.

In your example it doesn't really matter. From the performance POV method 1. should be a tiny bit faster. If your foo() and bar() do more than that, it's worth to follow the 2nd approach

I would favour maintainability and readability over performance until performance is a known issue. In your example above it wouldn't be an issue and as such I would go for your second option.

I think it doesn't matter in your case.

In their efficiency they are pretty much the same. If you would have more and more complex operations in your method you should split these in logical pieces.

Use:

public void foobar() {
    foo();
    bar();
}

Because when foo() or bar() changes you don't need to also change foobar() which you would need to do in your other implementation. These are simple functions but if you would have more complex functions control and debugging of your code will become harder.

You imply that efficiency is the only consideration. The chances that this choice, or this kind of choice, will affect your application' performance are very low.

Your major problems are (almost) always to write code that works now and that keeps working after change. Duplicating a piece of logic such as

barvar = true;

is bad, because now you have two places to change when that logic changes. Generally Do Not Repeat Yourself.

I'd go for second one, since they are loosely coupled and it helps in the long run since both are kept seperate and each can evolve irrespective of the other. In first option, it is like you mix donkey and monkey in the same method, where are in the second one we can keep it separate and it really helps well when you want to give something only to monkey, not to donkey ;)

public void foobar() {
  foo();
  bar();
}

But, this is of course a test method which does nothing than assigning boolean values.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top