Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use static functions if no class members are needed?

I have a member function that does not depend on any member variables of the class. (in my case the class is an ASP.Net Page)

The function is protected, I dont need it outside of this class. Its only purpose is to build an URL from an given object.

Should I make all my functions static if they dont depend on the class, even if they are not used outside of this class? Is there any reason like performance or maintainability to do so?

like image 658
magnattic Avatar asked Dec 12 '22 16:12

magnattic


2 Answers

Probably, but I'd take it a step further.

In these situations you really want to ask yourself if the method still belongs with the type at all. If this method has no dependence on the type's data, why is it part of that type? Is there a better or more appropriate type? Do you have several of these, perhaps scattered among a few different types, that could be logically grouped together?

like image 163
Joel Coehoorn Avatar answered Apr 12 '23 23:04

Joel Coehoorn


It is good practive to make functions that don't interact with member data static. It just helps describe how the function interacts with its environment. There should be no performance issues though.

like image 43
Nick Banks Avatar answered Apr 13 '23 00:04

Nick Banks