Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I know that static methods/variables are associated with the class and not the objects of the class and are useful in situations when we need to keep count of, say the number of objects of the class that were created. Non-static members on the other hand may need to work on the specific object (i.e. to use the variables initialized by the constructor)

My question what should we do when we need neither of the functionalities?

Say I just need a utility function that accepts value(s) and returns a value based solely on the values passed. I want to know whether such methods should be static or not. How is programming efficiency affected and which is a better coding practice/convention and why.

PS: I don't want to spark off a debate, I just want a subjective answer and/or references.

share|improve this question
3  
    
see also: Can't I just use all static methods? –  gnat Jul 14 '13 at 20:13
add comment

4 Answers 4

Methods which are not attached to any specific class like these are called "free functions", and as you have adequately noted, OO does not provide for them, and some languages also do not provide the facilities for them.

In a language like C++ you can just place the function in a namespace. But if you're working in Java or C# you must pointlessly place them in a class and make them a static member for no good reason at all.

share|improve this answer
1  
nope, no memory or speed issues the only effect it may have is it may make the compiler ever so slightly simpler to write if functions are always in a class –  jk. Jul 4 '12 at 9:18
7  
The Internet is a very negative place... If you don't like a design decision, that doesn't give you an excuse to slander the designers. Have you sat down and had a long conversation with the designers of C#? Until you have, you have no room to say their decisions were pointless or they were incompetent. If you disagree, do it respectfully. –  Phil Jul 4 '12 at 11:55
1  
@Phil: Sorry. I guess that when they came to design their Standard library and somebody said "What do we do about sin", it's perfectly OK for them to have just completely ignored it and pretended that it did belong in a class. No problem at all. It's not blatantly obvious whatsoever that Math is just a namespace. –  DeadMG Jul 4 '12 at 14:34
1  
2  
@DeadMG I know that all good programmers should have something of an ego, maybe even a superiority complex - but calling people such as Anders Hejlsberg 'hardcore incompetent' is just taking it to the extreme. –  MattDavey Jul 5 '12 at 15:39
show 3 more comments

It's pretty standard in Java to make such methods static and put them in "classes" with other static methods. You certainly wouldn't want to make the methods non-static--you'd confuse everybody, including the optimizers. I suppose the designers could have thought of a name besides "class" ("namespace"?) to hold these methods/functions, but most of us have enough to remember as is. I'll admit, having to add a private constructor to a "static" class seems a bit silly, but it gets the job done without adding new rules.

C# is a bit more explicit, with a static class (no double quotes around static), no need for a private constructor, and enforcing of the static-methods-only rule.

(Java has a "static class", but it's something completely different, primarily intended, no doubt, to confuse discussions like this one.)

share|improve this answer
add comment

If those utility functions are relatively small and not similar to others then you can use static functions. Also, the most important thing, they should not be related with the abstraction of the object (the class) and also you should not abuse them; they do not represent a OOPish way of programming.

I mentioned about size, because if they are large then you should use objects in order to obtain a better cohesion (share some data between functions by instance variables). I mentioned about similar functions because you might want to use the power of polymorphism.

Regarding efficiency static methods might be a little faster, but you would not gain any considerable performance gain from this.

share|improve this answer
add comment

Continuing with the example of System.Math in C#, some languages such as Nemerle allow you to add a using statement which effectively lets you use a classes behaviour as free functions, as well as direct access to its constants & other static members.

using System.Math // using statement at class level

public class Example
{
    public TestMethod() : void
    {
        def radians = Pi; // direct access to Math.Pi

        def x = Sin(radians); // direct access to Math.Sin & Math.Cos
        def y = Cos(radians);
    }
}

In essence this allows you to treat a class as a namespace, which as DeadMG pointed out is basically what System.Math is...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.