views:

189

answers:

8

I just realize some of my classes have no members. It has several public functions and maybe private functions and everything is passes through params. I realize functional programmers do this all the time but is this class considered special if it access nothing outside of the class and only uses its params for reading (except for out params and return values)?

I know this class can be static but static classes can modify outside variables. I want to know if this is a technique or maybe if a language may give additional benefits for writing it this way and etc.

-edit- This is looking like a wiki so lets make it one.

+2  A: 

If there are no members, then there is no need for a class other than having a namespace. If you were programming in Python, then you would just put those methods into a module and don't bother with a class. If you are working in Java, then a a class is a must. If you are working in C++, it's up to you - but maybe you should consider using just a namespace, instead of a class, to make it less confusing.

gruszczy
Not necessarily true. Sometimes you need polymorphic member functions even if you don't have any state. Look at the strategy pattern for instance.
Tom Dalling
What differentiates a stateless "strategy" class from an ordinary function? "Polymorphic" behaviour would be simply having a function signature instead of a base strategy class and passing the appropriate function pointer.
Max Shawabkeh
I still need private functions. In C++ (I am using C#) This would probably be easier in a class then in a namespace and using headers to decide if a function is visible or not. (thus i opt for class and private: functions)
acidzombie24
There's effectively no difference, but if the interface is already written you wouldn't go in and rewrite everything to take function pointers.
Tom Dalling
@acidzombie24 in c++ you would use an anonymous namespace for private stateless functions
jk
@jk is there such a thing as an anonymous namespace? I dont think i heard of it. Also how would you call functions within if you cant write nsName:: nor using namespace nsName;
acidzombie24
@MaxS: the difference is that if you take an object, some strategies can be stateless and others have state. Furthermore, suppose the strategy consists of multiple related functions. Even if every single implementation that will ever be written is stateless, then an object might be a convenient way to package the functions together into a single parameter. Of course another way to do that is to use a template parameter, which has its own pros and cons.
Steve Jessop
+4  A: 

There is nothing wrong with a class that has no members; controllers do this very frequently.

Yes, you could go static, but by not being static you allow for inheritance from your memberless class, which could add members along the way, if you so desired.

Randolpho
inheritance? Isn't this an example of abusing the concept of classes?
AraK
@AraK - I think your right. When an object has no state is it really an object?
ChaosPandion
And override existing ones. Although it becomes somewhat clumsy in using such a class - instead of simply referencing the static methods you have to lug a reference around. Or make a singleton, which then defeats the whole purpose again.
Vilx-
@AraK: possibly. It's a very slippery slope, I'll admit. Perhaps a stateless class is an indication of the possibility of code smell somewhere else, but it is not, in and of itself, bad. IMO.
Randolpho
If a class has functions and no data members but expects children to add data members, it's either an interface or an ABC.
Max Shawabkeh
@Vilx: keep in mind that referencing static methods tends to be difficult to test. Although given that this is a stateless class, that's not that big a deal. Unless the stateless class happens to modify state elsewhere... like say, a DAL.
Randolpho
@Max S.: Excellent point.
Randolpho
A stateless class is often an indicator for OO gone wrong. Check whether the functions actually do belong together (in the sense that the methods of an object belong together). In a language like Java you're probably stuck with the class paradigm for what is really a library (look at Math and such), in others you may not be. If it really is an object and it COULD have properties eventually, it's A-OK. Otherwise you might consider refactoring if the language permits it (e.g. modules / packages / libraries).
Alan
+3  A: 

Yes, this makes the class completely thread safe and thus no locking is required. To me, that is a fantastic attribute.

ChaosPandion
Or a poor attribute if it turns out the function depends on global variables. :)
KennyTM
Yes but i already said it only uses params for data and writes only to out params or return values >: -) (it doesnt even use ref). Except you have a point if that is a case.
acidzombie24
+7  A: 

It is just called "stateless". Nothing really special about it.

Dmitry
+2  A: 

Sane programming languages allow you to define functions outside of classes when the functions do not require any persistent data - if you can define it in an appropriate namespace, all the better. If your language does not allow that, at least define them in a static class so it's clear that no state is accessed or mutated. Overall, though, this reminds me of an excellent article on the illogical abuse of classes in the name of "pure OOP".

Max Shawabkeh
Nice way of saying "Java sucks", though I agree with your points.
Alan
I believe it's only nice way of saying "Java not having normal functions sucks" ;-) Let's not jump into conclusions ;-)
gruszczy
+1  A: 

I'd make the class static. I don't see what advantage it would give to keep it non-static. But then again - I'm a .NET developer, not Java. The languages are close, but there are many subtle differences I'm not aware of.

Vilx-
"static class" is a concept of .NET that doesn't exist as such in Java (and as far as I know it isn't common in other languages either).
Joachim Sauer
I am decided if i should or shouldnt make it static. I think i will but only because i see no reason to use the new() keyword.
acidzombie24
A: 

its simple class there is no specialty in it.

But i dont understand what is the use of having public or private methods when there in no member in it. because member methods are those which acts on particular instance's state.

Yes you can have static methods in it.

GK
some functions return useless data when used alone and has a proper implementation that should be use instead and equally powerful (ex i have a function that returns part of a name to build a longer string. But is used twice thus is called in a function instead of copy/pasted)
acidzombie24
+1  A: 

Remember that all methods of your class (even stateless) have one special variable -- pointer/reference to object - this (self, whatever) for which they are applied to.

Thus it is perfect sense in such stateless class if its methods could be overridden: in this case you have to have class to provide dispatching by this. (Of course it's just emulating of first-class functions, so if your language already has ones it's no sense in this technique.)

At the moment I can't imagine why would you need stateless class without any virtual method. Unless you want to have nice auto-complete in your IDE by typing objectName and dot :)

Alexander Poluektov
What if it is a static class?
acidzombie24
Well, I'm not a.NET developer, but as far as I understand static class is not "real class" in .NET -- just bunch of functions in one place. So my original post not really covers static classes.
Alexander Poluektov