When does it make sense to have only static methods and attributes in a class?

Asked

Viewed 924 times

13

Studying about static methods and attributes in OOP, I came across the following question: because in some codes we have ONLY classes with static attributes and methods?

Is there a design pattern that talks about it? In which cases is it an advisable practice?

1 answer

12


Methods

It is a matter of necessity. If you only have functions (another name of static method) that perform something alone or on top of some object that that function does not need to have more knowledge, has no reason to place these functions within an object that can be instantiated. They are functions that take an input (optional), perform a processing and release an output (optional) without depending on any information of a hidden object in its input.

By the question you must understand why you have static methods in a normal class. Think that there are cases that only they are necessary. There you can choose to create a normal class and let create a meaningless instance (there is even reason for this, for example in the Strategy pattern), but almost always restrict this is the best way and put a static there solves that question.

In some languages this confusion does not occur because they accept functions and other loose members. By marketing Java came up with this bizarre story that everything should be within a class to look like everything is object-oriented, even though static things are far from object-oriented. Then other languages copied this idea.

Attributes

Static state (attribute) usually tries to avoid a little since global state can be problematic. When you have been there is a good chance you are doing something wrong (but for everything there is an exception). Of course constant state does not cause any problem.

I used your term but the correct is field.

Where it is used

One of the best examples are the functions that act as utilitarian, They do their job, period, you have nothing else to worry about. And it makes perfect sense when it’s applied to something that doesn’t need an instance. It’s full of stuff like this in well-written application codes. In many cases it is possible to avoid repetition of code or complex relationships between classes.

Another good example is the Console. There is only one console to communicate, it does not need state, why create instance for this? Just because it gets more OOP? No, thank you very much, I need a better reason to choose another way.

Others related to the environment, universal information and directly to the operating system that there is only one, are also good examples.

Functions of calculating or converting numbers usually work better as something isolated, the interaction between the different types makes it difficult to put it as an instance, apart that it greatly swells the main class. And you will never have everything you need. You will always have new formulas to add, each with a different goal. There’s no point in putting that inside a int let alone create an object of a new type only to deal with a formula that can be easily applied to the original object. Think about it, being forced to create an instance of Math just to make a power of two simple numbers unrelated, seems exaggeration, no?

There are those who like to use a static class for object factories.

Not to mention the main class that obviously needs to be unique.

When to use

Some people disagree, but I always create a static class until I have a reason not to. It is true that almost always has a reason to use a "normal" class. If you don’t have a real need for an instance, if you don’t need inheritance, polymorphism, why complicate?

Some people like to make everything instance as a protective form. In general they ignore the YAGNI.

Some say they are bad because people do it wrong. So they better learn to do it right. Using the wrong technique because you don’t know how to use the right technique is not a good reason. It is not worth explaining here, but all the reasons that say that a static class is bad have solution, at least in some languages. Or else static class really isn’t a good idea.

Design pattern

Of course that’s a pattern, standards are everywhere. But it’s not a famous, cataloged one. It’s possible someone gave a name, but that’s little or nothing relevant. In fact this is of no importance. We should not cling to standards. They should serve us and not we serve them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.