Should domain logic reside inside or outside associated classes?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật