Drawback of using static method [duplicate]

For methods that never access instance variable or static variable and they act just like a function (name-spaced) and they are deterministic base on only the input arguments , I want to ask, are there any consideration if I change all of them to static method?

I keep hearing people saying it static method is poor, but I am looking for some real code example on my above case.

2

The problem with static methods is mockability and more generally substitutability. Although there are mocking frameworks which will allow you to mock static methods, there is broader support for mocking a non-static method, and it’s simple to do yourself by subclassing.

Say I’m testing class A which interacts with class B. If it calls only non-static members of B, then I can inject a replacement for B which behaves the way I need, for instance for testing. But if it calls a static member of B, it’s hard (depending on your mocking framework, sometimes impossible) to mock. So I have to use a real B – or the real B class – in my test of A; now I have a more complicated test, and one which is testing more than the unit (A) I’m interested in. It’s more fragile and harder to reason about than if I can just mock out the method.

I think that’s the best rationale for avoiding static methods.

4

I feel like static method’s encourage behavior like this. In this case we have a global class with a static function. We are hiding the fact that Foo depends on StaticWorker.DoWork().

Static Method

public class Foo
{
    public void Bar()
    {
        StaticWorker.DoWork();
    }
}

On the other hand, instance-level methods encourage passing the dependent object. Which is better than reaching into a global.

Instance Method

public class Foo
{
    Worker Worker;

    public Foo(Worker worker)
    {
        this.Worker = worker;
    }

    public void Bar()
    {
        this.Worker.DoWork();
    }
}

Now of course you could pass in a function, but I still like the abstraction I get with objects.

Another huge benefit of instance-level methods is that the class can implement an interface which you cannot do for a static method.

This is similar to the singleton argument. Singleton’s themselves don’t cause damage if used properly, but it makes it easier for people to call the singleton all over their code without passing it, once again creating invisible dependencies and global state.

There are exceptions to the rule. Sometimes you have a small, lightweight method. It will never change, and is not a dependency for anything else, I might make it static. You have to use your best judgement on a case by case basis, but I would say favor instances, especially if you have any doubts.

I feel like there are several benefits to using instance methods and all I have to do is instantiate an object. A small price to pay for the rewards.

Declaring a pure functions as static is correct in my opinion. It would be awkward to require creating an instance of a stateless object just to call a function.

There are cases however where you need them to be in an object instance. For example when you pass that function to another method, as you do with a Comparator in a sort method. More generally, when you need to substitute your function, or a set of functions by another one, you’ll need an instance.

The bad reputation of static methods comes from when they are used as a substitute for the Singleton pattern. But in this case, there is some state stored in static variables.

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

Drawback of using static method [duplicate]

For methods that never access instance variable or static variable and they act just like a function (name-spaced) and they are deterministic base on only the input arguments , I want to ask, are there any consideration if I change all of them to static method?

I keep hearing people saying it static method is poor, but I am looking for some real code example on my above case.

2

The problem with static methods is mockability and more generally substitutability. Although there are mocking frameworks which will allow you to mock static methods, there is broader support for mocking a non-static method, and it’s simple to do yourself by subclassing.

Say I’m testing class A which interacts with class B. If it calls only non-static members of B, then I can inject a replacement for B which behaves the way I need, for instance for testing. But if it calls a static member of B, it’s hard (depending on your mocking framework, sometimes impossible) to mock. So I have to use a real B – or the real B class – in my test of A; now I have a more complicated test, and one which is testing more than the unit (A) I’m interested in. It’s more fragile and harder to reason about than if I can just mock out the method.

I think that’s the best rationale for avoiding static methods.

4

I feel like static method’s encourage behavior like this. In this case we have a global class with a static function. We are hiding the fact that Foo depends on StaticWorker.DoWork().

Static Method

public class Foo
{
    public void Bar()
    {
        StaticWorker.DoWork();
    }
}

On the other hand, instance-level methods encourage passing the dependent object. Which is better than reaching into a global.

Instance Method

public class Foo
{
    Worker Worker;

    public Foo(Worker worker)
    {
        this.Worker = worker;
    }

    public void Bar()
    {
        this.Worker.DoWork();
    }
}

Now of course you could pass in a function, but I still like the abstraction I get with objects.

Another huge benefit of instance-level methods is that the class can implement an interface which you cannot do for a static method.

This is similar to the singleton argument. Singleton’s themselves don’t cause damage if used properly, but it makes it easier for people to call the singleton all over their code without passing it, once again creating invisible dependencies and global state.

There are exceptions to the rule. Sometimes you have a small, lightweight method. It will never change, and is not a dependency for anything else, I might make it static. You have to use your best judgement on a case by case basis, but I would say favor instances, especially if you have any doubts.

I feel like there are several benefits to using instance methods and all I have to do is instantiate an object. A small price to pay for the rewards.

Declaring a pure functions as static is correct in my opinion. It would be awkward to require creating an instance of a stateless object just to call a function.

There are cases however where you need them to be in an object instance. For example when you pass that function to another method, as you do with a Comparator in a sort method. More generally, when you need to substitute your function, or a set of functions by another one, you’ll need an instance.

The bad reputation of static methods comes from when they are used as a substitute for the Singleton pattern. But in this case, there is some state stored in static variables.

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