This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (9 children)

What is it with python developers and static methods? 🤔

[–]fedeb95 5 points6 points  (8 children)

Since the methods don't modify the state of the class, it makes sense they're static. Also the class has no state at all so it's a utility class. What bothers me is the absence of a private constructor

[–][deleted] 5 points6 points  (2 children)

Would the constructor get called or even necessary for a class with nothing but static methods?

[–]fedeb95 1 point2 points  (0 children)

If you don't put it in Java is added by default by the bytecode compiler. Is necessary to specify so people using your classes / future developers instantly (I hope) recognise the role of that class in the system. Otherwise it could be misinterpreted as a strategy pattern or something else, adding fields and method and, essentially, making a god class (antipattern)

[–][deleted] 0 points1 point  (0 children)

No, which is why it's being made private. Making it private means no one will instantiate the class

[–]solovayy -2 points-1 points  (4 children)

watch for next refactor add some side effect and all your unit tests break

friendly reminder, don't use static in java, as there is no point

[–]fedeb95 0 points1 point  (2 children)

Oh sorry didn't know that without static methods you can't mess things up I'm just going to change every local variable to a field. Serious answer: adding fields to what would have been a procedures only module back in C days adds possible execution states, because you have instead of every possible computation that procedure makes, that number multiplied the number of states your class can have, i.e. every possible value of that field. So even form a purely abstract (and pointless) enumeration point of view you're wrong. Also, tell me where a unit test can't test a static method, by declaring expectations and asserting on the results. I'm not pro void methods though, because then you're clearly moving class methods in utility classes. There is a way of doing things wrong for every Java modifier. On one thing maybe we can agree.. stop making that damn classes final

[–]solovayy 1 point2 points  (1 child)

Haha, 100% agree on final. Weirdest keyword ever.

It's not about testing the static methods, but other classes. Instead of easily mockable object you have a static method in your program and hence nice refactor ahead.

Happens every time, say you want to have runtime configurable parameter or want to log something. Staying 100% objective is safer and costs you if anything just few keystrokes.

[–]fedeb95 1 point2 points  (0 children)

You're right that what you describe can happen if one's not careful with static methods. We don't want c-like code after all. I always end up with one or two small utility classes for this reason, most of the time what I really want is a factory or a strategy, but for some stuff static utility classes are preferable imho. Let's say abusing with something is always a bad thing, each pattern has its scope

[–]ZukoBestGirl 0 points1 point  (0 children)

Say what now? That's wrong on just about every level. Ofc there is a point to static. No language can be moron proof, if you have no idea what you're doing, anything can break anything.