4 ms·
The article states: These rules are constraints on how to create your code and are only intended to be applied when doing katas or exercises. Thes
by munificent 9y ago
The article states:
These rules are constraints on how to create your code
and are only intended to be applied when doing katas or
exercises.
These rules are an exercise to kick you out of an OOP mindset, not best practices for writing real code.
By that same token, I think it is a good rule when practicing OOP to not allow bare functions. It helps train yourself to think "Is there a logical class that this method belongs to?" In real code, of course, the answer is often "no". But when first learning OOP, it's good to teach yourself to ask that question in the first place.
Your code isn't a good example of "the OOP way", though. A fluent OOP solution would be:
s = x.sin()
Another good rule when practicing OOP would be "No classes without state." That helps you avoid the habit of writing pointless "namespace" classes like the "Math" class you define here. Of course, again, sometimes it is useful to wrap a bunch of related behavior in a stateless class, but that should be the exception, not the default.
- michaelg7x 9y agoIn Java static methods are a particularly simple way making clear to the compiler and runtime that a given functions does not have side effects (to instances of the owning class). I can imagine that, as a pattern, they would assist escape analysis.
- maxxxxx 9y agoIn C# you can have static classes. I often use these to group simple functions together.
- lederhosen 9y agoA static method can of course mutate an object of the same class (give the object as an argument (direct or indirect) or use a static variable of the class) I can not see how they could be used to help escape analysis more than that you have one less reference (this).
- michaelg7x 9y agoYes, of course that's possible. I was trying to point out their usefulness to the programmer as well as potentially to the runtime/JIT. Next time I'll list the obvious exceptions!
- derefr 9y agoYou know, you're talking about a very specific kind of OOP here. Elixir, for example, is a language I'd call OOP—but it doesn't have classes. It just has "bare functions" in modules, where a module's functions will usually expect values of the type the module is named after. Also, to reverse that perspective, a "type" in Erlang/Elixir-lang is just any data formatted in a way that a given set of functions will work with it, rather than requiring an explicit tag. {1, 2} can be an instance of a type†, if you have a module Foo that spits that shape of data in response to Foo.new/0 and takes it for Foo.change/1. † An example of such a type is Erlang's http://erlang.org/doc/man/queue.html http://erlang.org/doc/man/queue.html . queue:new/0 just returns {[], []}.
- Veen 9y agoThe actor model that Elixir/Erlang is OOP in its "purest" form, but that's not what most people mean when they discuss OOP languages.
- CuriouslyC 9y agoI don't think that no classes without state is a good hard and fast rule. In some languages classes are the easiest way to get a new namespace, and thus in combination with static methods they are useful in producing fluent APIs (which I believe are a good design choice). You could of course achieve the same result with modules but I'd argue it's cleaner to have an inline class than a separate file when you're only adding a couple of functions.