When should method overloads be refactored?

When should code that looks like:

DoThing(string foo, string bar);

DoThing(string foo, string bar, int baz, bool qux);

...

DoThing(string foo, string bar, int baz, bool qux, string more, string andMore);

Be refactored into something that can be called like so:

var doThing = new DoThing(foo, bar);

doThing.more = value;
doThing.andMore = otherValue;

doThing.Go();

Or should it be refactored into something else entirely?

In the particular case that inspired this question, it’s a public interface for an XSLT templating DLL where we’ve had to add various flags (of various types) that can’t be embedded into the string XML input.

1

I believe the common approach is to encapsulate some (or all) of the parameters into a class and have that as a parameter:

DoThing(string foo, string bar, DoThingParameters parameters = null);

If you use C#, you can then write those parameters directly inline when calling the method:

DoThing(foo, bar, new DoThingParameters { More = value, AndMore = otherValue });

Both work, though I personally dislike assigning public members or properties like that if these members or properties are used directly for one purpose.

In other words, if you have a logger class, you shouldn’t have to assign “logger.message” and then call “log()”, since message is used strictly for the log method, hence it should be passed.

Understandably, I can see the need for doing such things in order to avoid refactoring code. Here are a couple things you could do instead:

  1. Consider passing non-specific parameters in the constructor rather than the method itself. What I mean by non-specific parameters are parameters which aren’t specific to a particular method but control behavior that isn’t likely to change in the instance. Returning to the logger class example, such a parameter might be the directory where the log file is saved.
  2. Consider passing a key-value map containing all parameters. This is the preferred method for passing non-descript parameters to third party libraries anyway. If you find yourself dealing with large amounts of parameters to handle, then this might be preferable. A good example of this might be instantiating a database connection using many of the database-specific parameters rather than having to have a static method for each database. The method can then pull whatever parameters it finds that it can use. However, I recommend that if you do it this way that you consider eliminating the other method signatures and forcing all calls to use this key-value system for consistency.

I hope that helps!

1

What you are facing here is the telescoping constructor anti-pattern.

IMHO you should refactor to public members only if it’s a simple class used to store data. Otherwise, your class looks like a good candidate for the Builder pattern. You can use it to:

  • encapsulate default member-values logic – set those values from inside the builder instead of setting them for each object you construct using it’s members public access

  • gain the ability to respond to inappropriate initialization – when the object is constructed from the builder you determine inside the builder what to do when not all required values were set

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