1

I read Robert Martin's Clean Code: Function chapter. In this chapter, the best number of arguments of function is single argument. So, it is good to abstract that argument and function should be separated if it have various roles.

In practice, I wrote function that accepts single/plural arguments using Typescript like below.

Context

Our web server have various permissions to enable a specific functionality, thus client should separate each features per permission. To implement this, I wrote function which receives single/plural permission as argument and check that permission is permitted. At first, I wrote this code like below

const isPermitted = (permissions: Permission | Permission[]): boolean => {};

But, I have problem for naming argument name permissions because it could be single and plural. I considered between permission and permissions. Consequently, I concluded to change like below.

const isPermitted = (permissions: Permission[]): boolean => {};

Argument is single, but it always accepts array only. I thought it was good refactoring. In this context, is it proper solution?

5
  • 4
    I get the feeling you're trying to dogmatically follow scripture here, rather than apply good practice as a common sense argument. That's not the solution to your answer, but it is a frame challenge (or at least a frame nudge). There is no "perfect" clean code, or a binary yes/no adherence to clean coding principles. Additionally, any general blanket clean coding advice may not be compatible with concrete requirements you have, and the way you code cleanly should adapt based on the context of your requirements and development priorities (e.g. favoring speed over maintainability).
    – Flater
    Commented Mar 18, 2021 at 1:39
  • 1
    Also, from what I remember from Robert Martin's Clean Code, the advice isn't to reduce method parameters to having only a single one, it's to favor minimizing the amount of parameters. That's not the same as "it must be 1". Are you maybe conflating the concept of (a) minimizing method parameters and (b) having one responsibility?
    – Flater
    Commented Mar 18, 2021 at 1:42
  • @Flater I got ur point, but I didn't say that Bob said "it must be 1". Not MUST, BEST. Anyway, I agree with you that clean code depends on the context of specific environment. I thought there is general solution for my case. Also, I'm focusing on "minimizing method parameters", not SRP. Then, this kind of question is useless?
    – undefined
    Commented Mar 18, 2021 at 2:18
  • Not useless. What is your argument for this version of the code over any other arrangement of code that achieves the same purpose?
    – Kain0_0
    Commented Mar 18, 2021 at 3:46
  • @everyone Sorry for inconvenience, I added context and updated question. If you have any question or feedback, it would be good
    – undefined
    Commented Mar 18, 2021 at 7:01

1 Answer 1

7

That smells a bit like Cargo Cult (One of the main things BTW 'Uncle Bob' is fighting against).

Yes, the best number of arguments for a function that only needs one argument definitely is one argument. Easy to read and easy to follow. But jumping through hoops to reduce the number of function arguments blindly will even complicate your code.

Imagine the following function that adds two numbers:

// 1
int add (int a, int b) {
   return (a + b);
}

Looks straight and easy. But: Oh. too many arguments, let's reduce that to one

// 2
typedef struct addition {
   int a;
   int b;
} Addition;

int add (Addition x) {
   return x.a + x.b;
}

Much better, a single argument (really? To me it looks rather convoluted)

What Uncle Bob promotes is rather avoiding the following:

typedef enum {
   ADD = 0,
   SUB,
   MUL,
   DIV
} Operation;


int calculate (int a, int b, Operation o) {
   switch (o) {
      case ADD:
         return a + b;
         break;
      ...
   }
}

The rule "do one thing and do it properly" comes before (and is definitely more important than) the "single argument" rule in Uncle Bob's books.

What you seem to be trying to do in your example is to ignore "do one thing" over "single argument" by writing a function that can either take one or more permissions and apply one or more.

You should rather write a function for one single permission and another function for a list of permissions (which may in turn repeatedly call the previous one). The caller will know very well what she needs in what specific case when you name your functions properly (e.g. setSinglePermission (permission) and setListOfPermissions (list))

2
  • I would even go as far as saying you don't need both functions; setting one permission is functionally identical to setting a list with one element... Commented Mar 21, 2021 at 15:21
  • @KevinVanDyck That's exactly the point - The caller needs to provide a list, wich is (depending on the language) way more complicated as providing a scalar argument, thus bloated code.
    – tofro
    Commented Mar 22, 2021 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.