Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is void method in interface a code smell?

I mean design smell like temporal coupling. Void method has no type safe strict description in its signature about cases why and when it should be called. So it's only up to documentation. And using it is based on faith in correctness of side effects.

So should we try get rid of void methods in interfaces?

like image 738
SerG Avatar asked Sep 08 '25 13:09

SerG


1 Answers

Void methods are not design smells.

Object-orientation (unlike functional programming) allows side-effects, for example changing the internals of the object the method is called on. This is normal.

Whether that is good or not is another question. Functional programming is certainly more powerful in terms of expressiveness. It is possible to write code that is almost impossible to misuse (when it compiles it works).

Also, void methods don't cause temporal coupling in general. Temporal coupling would mean, that there must be another method that must be called before or after the method, otherwise the method call does not make sense.

Of course you have to know the semantics of the method call, in other words what it means. That is to be expected, and would be the same in functional programming also, for functions with the same signature.

like image 184
Robert Bräutigam Avatar answered Sep 10 '25 06:09

Robert Bräutigam