Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing class methods: Are they "evil" in this use case?
    primarykey
    data
    text
    <p>Oftentimes I have the following scenario: I implement an algorithm that just gets some input and returns an output. The standard OO way would probably be to create an object that is initialized with the input and then call a method on it that returns the result (maybe with the intermediate step of 'running' the algorithm):</p> <pre class="lang-rb prettyprint-override"><code>algo = MyAlgorithm.new(some_input) algo.run algo.result </code></pre> <p>There are a few things that I don't like with this:</p> <ul> <li><p>Every time I want use this class, I have to write down all the steps, which is tedious, obviously.</p></li> <li><p>My object has (at least) 2 different states: before and after running the algorithm. Even though I'm only interested in the second state, I would have to cover the behavior in the first state in my specs. What if I call <code>#result</code> in the first state? Is <code>#run</code> idempotent?</p></li> <li><p>I'm actually violating Tell Don't Ask here: I ask the class to give me an object which I use to obtain the solution to my problem. Why don't I just ask the class for the solution directly?</p></li> <li><p>The most naive implementation would be to just make one method call. Of course, pure imperative programming is not ideal here as there are benefits to using an object: we can isolate the data used during the algorithm computation. But isn't this just an implementation detail, and if yes, shouldn't we hide that information?</p></li> </ul> <p>I wonder why not just do something like this:</p> <pre class="lang-rb prettyprint-override"><code>class MyAlgorithm def self.solve(input) new(input).instance_eval { run; result } end def initialize(input) @input = input end private def run # do some computation (modifying the object's state) end def result # return the result end end </code></pre> <p>This way, I obtain the only thing I really want (the result) with a single method call on the class object, without losing the benefits of object orientation. I don't have any public instance methods, so I don't have to worry about different states of my objects. Moreover, I could even make <code>.new</code> a private method, if I wanted to.</p> <p>(Note that, if I'm interested in reporting like runtime, number of iterations, etc., accessing the object directly for makes sense, but here I'm just interested in the result of a computation).</p> <p>Now I here people saying "class methods are evil" (<a href="http://nicksda.apotomo.de/2011/07/are-class-methods-evil/" rel="nofollow">here</a> and <a href="http://andrzejonsoftware.blogspot.de/2011/07/yes-nick-class-methods-are-evil.html" rel="nofollow">here</a>, for example). But I think for my use case, a class method is exactly the right thing, isn't it? On the other hand, I wonder why I don't see other people use this style. Maybe I'm missing something. Are there any drawbacks to this design?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload