Functional vs object-oriented style in C#

I’m learning functional programming and face the following confusion when applying it to my C# projects:

  • I begin by writing pure, static functions and use function composition.
  • After the code reaches a certain size, I realize some of these functions tend to “cluster” about some types. For example, there exist >5 functions having the same type as parameter.

  • Next step, my object-oriented background takes over and I move these functions to this type (class) as methods. As a result, their signatures simplify and they often become more cohesive. However, I lose the benefits of functional style.

I’ve grown very fond of functional approach, but I cannot convince myself that my initial code was better above.
Which approach would you prefer in this situation (in C# or another mixed language)?

Related: Design in “mixed” languages: object oriented design or functional programming?

class MyType { }

class Functions
{
    public static void X(MyType t) { }
    public static void Y(MyType t) { }
    public static void Z(MyType t) { }
    public static void T(MyType t) { }
    public static void U(MyType t) { }
}

class Class1
{
    public MyType T { get; private set; }
    public Class1(MyType t) { T = t; }
    public void X() { }
    public void Y() { }
    public void Z() { }
    public void T() { }
    public void U() { }
}

21

However, I lose the benefits of functional style.

If you always lose the benefits of functional style code when converting bundles of functions to objects, you’re doing it wrong (or have an usual idea of what the benefits of functional style are).

After all, instance functions are the same thing as static functions but simply have an additional implicit parameter. If you’re making these functions impure, that’s your problem, not a problem inherent to OO code. If you make your classes mutable, then yes, you’re going to lose functional style benefits.

But if you keep your classes immutable (except for collections/DTOs), I find that you can maintain many of the benefits of functional style coding while getting some OO goodness as well.

9

I don’t think that simply using static methods makes it Functional

I don’t think simply moving your previously static methods to a class makes it OO

If you are doing OO, then your classes should have properties and the methods should change those properties. This makes the ‘data object’ mutable and functional programming goes out the window

While Static does stop you using properties from the same class, you can still reference other static data or databases etc. Functional programming relies on your methods returning the same result for the same parameters every time.

Going from your Example code:

ive followed your pattern but added a real method

public class MyType
{
    public int ANumber { get; set; }
}

public class StaticFunctions
{
    public static int AddOne(MyType x)
    {
        return x.ANumber + 1;
    }
}

so you convert your static method to OO via this kind of pattern:

public class MyOOClass
{
    public MyType data { get; private set; }
    public MyOOClass(MyType data)
    { this.data = data; }

    public int AddOne()
    {
        return this.data.ANumber + 1;
    }
}

But really this is nothing more than a namespace for your static functions. it arguably makes them harder to use as you have to initiate an extra MyOOClass for every MyType.

If you wanted to make it OO I would change it to something like this:

public class OOClass
{
    public int ANumber { get; private set; }
    public OOClass(int n)
    {
        this.ANumber = n;
    }

    public void AddOne()
    {
        this.ANumber= this.ANumber+1;
    }
}

if you wanted to make it functional i would change it to something like this:

public Func<int, int> AddOne = (x) => x + 1;

My Personal fave though would be ADM

public class AdditionService()
{
    public MyType AddOne(MyType a)
    {
        return new MyType() {ANumber=a.ANumber+1};
    }
}

7

You can write functions even when using classes.

You said you created static functions, so there is no “state”. When you move those methods to their respective classes, of course they can’t continue to be static, they need to be regular instance methods.

But if you don’t modify any of the parameters, even the receiver of the message (which is the same as the container of the method), it continues to be functional style. Just don’t modify any of the parameters.

The real advantage of the functional style is that the functional style can be functionally executed in a way that is really simple: replace the called code with the body of the ode being called. You can repeat this many times. And therefore you can be sure it produces the desired output.

1

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