I have a question on where to put the logic in the following hypothetical:
There’s this class that, at it’s basis, has an int[]. There’s a method that takes the int[]s of two of these classes and returns a third int[].
My question is: where should this logic reside, and why?
Option 1
The first option would be inside the class:
public class SomeClass{
public int[] publicData;
// additional contextual data here
SomeClass PerformCalculation(SomeClass otherClass){
// accesses publicData from both classes and returns a new SomeClass-object
}
}
Which means you get the following implementation:
var firstClass = new SomeClass();
var secondClass = new SomeClass();
var result = firstClass.PerformCalculation(SecondClass);
// or: secondClass.PerformCalculation(firstClass)
Option 2
The second option would be outside the class:
public class SomeClass{
public int[] publicData;
// additional contextual data here
}
public class SomeCalculator{
public SomeClass PerformCalculation(SomeClass firstClass, SomeClass secondClass){
// accesses publicData from both classes
// and returns a new SomeClass-object
}
}
giving the following implementation:
var firstClass = new SomeClass();
var secondClass = new SomeClass();
var result = _calculator.PerformCalculation(firstClass, secondClass); // _calculator is created via DI
Option 3
Finally, there is a third option, where only the int[] is used:
public class SomeCalculator{
public int[] PerformCalculation(int[] firstData, int[] secondData){
// return only the calculated int[]
}
}
var firstData = new int[]{1,2,3};
var secondData = new int[]{4,5,6};
var result = _calculator.PerformCalculation(firstData, secondData); // _calculator is created via DI
Context
The first option is more OOP, the second more procedural (if I’m correct in my terminology).
In all cases, the operations performed on the int[]s in PerformCalculation is core domain logic; it’s not an arbitrary function that ends up in a helper class.
SomeClass is also part of this domain, giving rise to option 1. Here, the creation of a new SomeClass-object is also non-arbitrary and would warrant the inclusion of PerformCalculation into the class.
At the same time, because the core operations in PerformCalculation is only done on two int[]s, without any required context, options 2 and 3 are also possible.
My question is: which option is best and under what circumstances?
3
In this question, you are pretty much asserting that the appropriate location of a method should be decided purely on the basis of the method’s input/output parameter types, not the purpose of the method; which is just not a good way to approach code organization.
Any coincidental overlap you might find in some cases (where a method that belongs in a class does have the class for its input/output type) does not make a case for this as a general guideline to be applied above all else.
The question is so vague it’s not meaningfully answerable. This is the equivalent of saying:
Person A rendered person B unconscious and then stabbed him with a knife. What should we do with person A?
The answers can range from “imprison that murderer” to “pay that surgeon his salary”, because it’s lacking in context and nuance.
Any answer I give you here is going to be broad and based on assumed scenarios, which means that you shouldn’t take it as irrefutably proven to be correct. Different contexts can lead to different conclusions and justifications.
The first option is more OOP, the second more procedural (if I’m correct in my terminology).
There’s a grain of correctness here in that OOP tends to bundle logic more in a class whereas procedural/FP is more inclined to separate things out; but the more important consideration here is that you can’t just say that one is better than the other, not even when you establish whether you’re using OOP or not.
You still are lacking the context to actually judge what makes sense in that context.
For example, if the class in question is a rectangle, and the goal here is to build a new rectangle using the largest dimensions found in two other rectangles (e.g. a 6x4
and a 2x8
create a new 6x8
rectangle), that’s not the kind of logic you’d put in a rectangle class, because that’s being done for wholly different reasons. Nothing about this logic matches anything about the definition about what a rectangle is, it’s an arbitrary algorithm done for some ulterior reason.
“Ulterior” is the key word there. If the design of that PerformCalculation
is done for reasons unrelated to the definition of SomeClass
, then it doesn’t belong in the SomeClass
class definition.
Your job is to look at the context and figure out if this is the case or not. There is no universal answer to such a vague question.