简体   繁体   中英

Should all methods that do not use instance variables be marked static

Suppose I have a class like this:

public class Car {

    private double distanceDriven;

    public void drive(double miles){
        distanceDriven += miles;
    }

    public void driveInCanada(double kilometer){
        distanceDriven += convertToMiles(kilometer);
    }

    private double convertToMiles(double km){
        return km*0.621371192;
    }   
}

You can see that convertToMiles is:

  • not using any instance variables
  • is only used inside the class

Should it be declared as static? This does not change the functionality of the the function at all (see above). I think that it may affect:

  • readability
  • performance
  • other?

Should the convertToMiles function look like:

    private double convertToMiles(double km){

or

    private static double convertToMiles(double km){

For maximum stylistic hygiene, yes, private methods that don't use any object state, but only make sense inside the object, should be static.

That's the clearest (and strictest) way of indicating how they operate, and it will helpfully force you to be careful about your design around method-boundaries, and to think twice if you decide to go change one of them later to use object data.

FWIW, I don't suspect there's a relevant performance impact here (in theory the static is easier to call due to no implicit this reference). Also, you could go nuts being strict about this in your codebase, but it's certainly a reasonable goal to have.

NB Public methods require more consideration before marking them static; those can't change down the road without impact to callers, so "defaulting to tightness" isn't always the right choice.

If you're asking yourself this, they your design is already shaky. You should rip all those "static" functions out of the class and put them in a generic, reusable algorithm container static class.

Look at your code, what does convertToMiles have to do with a car? That's a generic algorithm that can be reused in multiple functions.

Using static might make a performance difference, however this is less likely if it is inlined as it won't be called as much.

static is useful as it makes it clear you are not accessing any member fields. This has picked up some bugs for me in the past when I marked a method as static but this produced an error (because it shouldn't have been using a member field)

You can get creative with the design and add layers and complexity which might be useful one day, but I would go with the YANGI principle and say it is unlikely you are going to want to change how kilo-meters are converted to miles, or if you do change it you are unlikely to want more than one way of doing it.

A definitive NO for ALL such methods.

For example it is perfect legal that such a method calculates an result (return value) only on its arguments, and the author would like to allow others to change the calculation in a subclass. (This is some kind of Template Method pattern.) -- And overriding a class can be only done if they are ..not.. static.

Btw: if you change your question and ask only for private methods, then I could not argue this way. But you asked for all kind of methods.

yes. Use static methods when you can.

private static double convertToMiles(double km){}

This will be the right one for your program code as convertToMiles() method has nothing to do with the instance variable.

But keep in mind this method is not reusable in other class by non-static members, if yes then the very purpose of static wont serve, as static avoids multiple object creation and memory wastage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM