Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate Class vs Method

Quick design question.

ClassA has a method called DoSomething(args)

In DoSomething(), before it can actually do something, it needs to do some preparatory work with args. I believe this should be encapsulated within ClassA, (as opposed to doing the prep work outside and passing it in) as nothing else needs to know that this prep work is required to DoSomething.

However, it's where the actual preparatory work code belongs that is making me think.

The preparatory work in my particular example is to create a list of items, which satisfy a certain condition, from args.

My hunch is that I should create a new class, ListOfStuff, which takes args in its constructor and put this preparatory work here.

From a TDD-perspective, I think this is the right choice. We can then unit test ListOfStuff til our heart is content. If we had put the preparatory work in a private method of ClassA, we'd have only been able to indirectly test it through testing DoSomething().

But is this overkill? Since adopting the TDD and DI approach, I've seen the number of classes that I write multiply - should I be worried?

Ta.

like image 722
Duncan Avatar asked Dec 19 '08 23:12

Duncan


People also ask

Why use a class instead of a method?

By using classes, you're ensuring that methods are only used on one set of data. This adds to the security of the code because you're less likely to use functions where they don't belong.

When should you separate classes?

If there is a class that handles two or more things, split that into multiple classes. So check your classes now, is there any class that does a lot of things, like getting user input, doing caluclations, and printing the result? If that's the case you probably want to split that into multiple classes.

What is the difference between class () and typeof () functions in R?

class is an attribute of an object that can be assigned regardless of its internal storage mode, while "typeof determines the (R internal) type or storage mode of any object." One describes a logical characteristic while the other is a physical characteristic of an object.

What is the difference between class method and instance method?

Instance methods need a class instance and can access the instance through self . Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls .


3 Answers

There are a couple of heuristics here.

  1. Is there state on this class that survives from invocation to invocation? Does this prep work get done every time you need to doSomething(), or is it done and saved? If so, that argues for the class.
  2. Does this computation need to happen in more than once place? If so, that argues for a class.
  3. Can the details of the implementation of the doSomething() method, or the preparation work for it, change without affecting the enclosing class? If so, that argues for a class.

Well, three heuristics. No one expects the Spanish Inquisition.

like image 142
Charlie Martin Avatar answered Oct 19 '22 23:10

Charlie Martin


What is the simplest thing that could possibly work? That's the TDD mantra. Don't try to think too far ahead. If the time comes to create the helper class, you'll know it because you'll be doing all sorts of related work in multiple methods in other classes. Until then, do the work in your method. If it makes the method too long or cumbersome to read, extract the work to its own method. This method can also be tested to your heart's content without the need for another class.

like image 20
tvanfosson Avatar answered Oct 19 '22 23:10

tvanfosson


Definately put it in a new class. Its called separation of concerns. You don't want to overload a class and make it do all sorts of other stuff. This is because your class will not be able to be used anywhere else as its so specific to one thing.

Put it in class, then use that class elsewhere. Otherwise you'd have to write this again and again in the future.

To make it extensible, and be able to pass in all sorts of different algorithms, the design pattern your after here is the Strategy pattern. But that is for the future...

like image 20
Simon Hughes Avatar answered Oct 20 '22 00:10

Simon Hughes