How to avoid large function arity / exposure of underlying implementation

Currently reading Clean Code, and the authors seem to imply that any function with an arity greater than 3 should be avoided at all costs.

I am unfortunately running into an issue where I am faced with a class method with an arity of 4 (5 if you count self)

 def start_job(self, operation, name, external_id, format):
    ...

All of the parameters can vary, and are necessary to start the job. However, if I think about encapsulating all of those parameters in some sort of class

class Job(object):
    def __init__(self, operation, name, external_id, format):

but then all of those parameters would be necessary for the __init__, and would still need to expose the underlying structure of the object to the start_job function. At this point, it would seem like feature envy as well and would make sense to change the start_job function over to the Job class and just call it start.

Any ideas on how to avoid this smell?

1

This really isn’t answerable without knowing the exact case you are trying to solve. The reason they call them “code smells” is not because the “smell” itself is bad but rather because it indicates that there is something else wrong, a metaphorical pile of crap underneath generating that smell.

As such, the answer generally is not to directly attack the smell itself. That’s no better than spraying the crap with lysol. The answer is to try to figure out what in the underlying design pushed you to use a method with four parameters. Maybe examining the smell will bring this out, or maybe you’ll decide that it was just a whiff of compost and maybe not so bad.

In this case, four parameters isn’t much of a stink. It’s when you reach ten or fifteen that things stink to high heaven.

One way to see if this is a real issue is to look at the code inside the function. Is it a set of elif blocks, basically a case statement controlled by one of your parameters? If so, maybe the method should be broken up along those lines.

Are you passing the same set of parameters to a set of methods? Then maybe you should have a class controlling those values with members for each of the original methods.

Are some of the parameters combined in particular ways inside the method to make a single value? Maybe it could be pulled up out of the function. (For instance, are you creating a filename that looks like name + ‘.’ + extension where extension depends on format?

Does a parameter almost always take a particular value? Then make it a default. While not reducing the “arity”, it does reduce the cognitive load required to use the function.

1

If all of these parameters are always required (never optional), vary independently, have no sensible sub groups, and have widely varying values then it will be tough to do better than you’ve already done. Named Arguments can make the long list less confusing to read.

If that’s not exactly the case then there may be room for improvement. Ask yourself:

Are all the parameters required or are some optional? If optional they should have a default. Make attributes and use a property or setter to change the default. Only keep required ones in __init___.

Are any of the parameters related? If I know two of them can I do anything useful? If so make an object for them that does this useful thing and see if it can also help you do your thing.

If I know one of them can I learn what the other is? If so you’re probably providing redundant information. Figure out what you can live without.

Do any of the parameters have a limited number of states? For example are there only 5 things operation can be? Maybe 5 methods would be a better idea, or even 5 classes.

There are a lot of patterns that can help with this problem. The GoF construction patterns, in particular builder and parameter object. Also the python version of Josh Bloch’s builder might help. Whether it’s 4 or 14 parameters you should always look for a way to simplify how your code is used.

Do the members of Job logically cohere into an object? Yes.
What can you do with a Job? Start it, stop it, query its state.

Sounds like you’ve got a bona fide object there with start(), stop(), query() instance methods.

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