Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I add a trailing comma after the last argument in a function call?

What is better to do?

self.call(1, True, "hi") 

or

self.call(1, True, "hi",) 

And what in the following cases:

self.call(     1,     True,     "hi" ) 

or

self.call(     1,     True,     "hi", ) 

?

Reasons for adding a trailing comma in data structures are familiar to me, but what about function calls?

like image 945
taper Avatar asked Aug 23 '12 08:08

taper


People also ask

Should we use trailing commas?

In general, you should make use of trailing commas when you frequently copy/paste properties or add new items to the end of a list. You can also take advantage of them to produce cleaner diff outputs.

What is a trailing comma?

Trailing commas refers to a comma at the end of a series of values in an array or array like object, leaving an essentially empty slot. e.g., [1, 2, 3, ] I kind of like them when I work on Ruby and Python projects.

Can trailing commas be used in objects and arrays JSON?

They are optional. There is no reason at all commas need to be present to parse a JSON-like document.

What does a trailing comma do in Python?

It helps to eliminate a certain kind of bug. It's sometimes clearer to write lists on multiple lines. But in, later maintenace you may want to rearrange the items. But if you allow trailing commas, and use them, you can easily rearrange the lines without introducing an error.


2 Answers

I think there's no technical reason to avoid trailing commas in function calls, but some people probably do find them distracting. Some may stop and say:

"Hmmm, I wonder if that's really supposed to be there?"

I hesitate to call this a benefit, but one effect of using trailing commas in conjunction with an indented style is to make version control diffs look a little bit cleaner when adding an argument.

For example, a function like this:

def my_fun(a, b, c=None):     ... 

...called like this:

my_fun(     a='abc',     b=123 ) 

...then changed to this:

my_fun(     a='abc',     b=123,     c='def' ) 

produces this diff in git:

$ git diff ...  my_fun(      a='abc', -    b=123 +    b=123, +    c='def'  ) 

Whereas,

my_fun(     a='abc',     b=123, ) 

changed to...

my_fun(     a='abc',     b=123,     c='def', ) 

produces this diff in git:

$ git diff ...  my_fun(      a='abc',      b=123, +    c='def',  ) 
like image 128
Jacob Wan Avatar answered Sep 22 '22 22:09

Jacob Wan


I'll also leave my 2 cents here, even if this thread has been here for quite some time, it might benefit somebody :)

PEP8 actually does say when to use trailing commas and if you follow their recommendations (not mandatory, but definitely recommended) then the second one-liner example of yours can be ruled out, i.e:

No:

self.call(1, True, "hi",) 

Yes:

self.call(     1,     True,     "hi", ) 

Usages in function calls (use sparingly to never) and here's why:


  • One of the coding principles is that a function should do one thing, and one thing only. So, seeing a trailing comma there, a lot of questions may arise, when they really shouldn't. Normally when you see a trailing comma somewhere, you expect that thing to change over time. So if it's a list, tuple, dict, etc. it usually indicates that whoever designed it, did it with the intention of physically adding or removing or switching lines in that data structure, and well... you don't see that with functions very often, or at least you shouldn't, and if you do, a better design should be considered.

  • As aforementioned, a function should also be very predictable, you don't design a function that sends mail and rockets to the moon at the same time, and leave a trailing comma when calling it to send mail because who knows when you might send rockets and you want less cluttered diff (it will only cause more confusion and its a poor design).

  • An arbitrary number of parameters (or even a fixed, but large number of parameters) usually indicates that either you are either instantiating an object there, or you are doing multiple things, when really what you should be doing is split your design into creating multiple smaller functions, that each do one thing, hence, no trailing comma needed.

  • Also consider, even if you did have a function, which you would be able to call with multiple number of parameters, think about how often would you do that and what it implies? Well that would imply something like:

    • Refactoring the code at the call place (which may be in another function, or in a module or somewhere) because usually the result from functions is stored in variables, and you need to use that new result, so that would imply refactoring.
    • If it doesn't imply refactoring then it means the function doesn't return anything, or it returns the same thing, in which case it means that it does multiple things with the extra arguments passed in the call, which, as previously said, is not very ideal.

Actual usages


  • Trailing commas make sense, like you said in data structures, data structures that are expected to change physically over time, because if you change it at run-time, well it wouldn't make any sense for anything that's in the editor, except for the definition which might as well be an empty structure [] xD

  • Where data structures (lists, dicts, sets, tuples, etc) are expected to change (physically, the source code) then trailing commas are actually recommended and actually useful (see the full PEP link, it has use cases and recommendations)

Conclusion:


  • Recommended in multi-line data structures that are expected to physically change
  • Rarely to never in function calls
  • Never in function definitions
like image 37
Marius Mucenicu Avatar answered Sep 18 '22 22:09

Marius Mucenicu