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

前端 未结 5 889
慢半拍i
慢半拍i 2020-12-15 16:15

What is better to do?

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

or

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

And what in the following

5条回答
  •  一生所求
    2020-12-15 16:46

    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 structure, and well... you don't see that with functions that much, 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 to send mail and rockets to the moon at the same time, and leave a trailing comma when calling to send the 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 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:

      • It implies 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 does return 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.
      • Even if you don't care of the coding principles, and ignore the above, how often would this be done? If the answer is often, that's another poor, unstable design implementation. And if it's not very often (well it pretty much answers it by itself that it shouldn't be there, and even if you think it should, I think your eyes can bear another extra line in the version control diff so that you can avoid causing extra confusion within the ranks of the people who also share your project.

    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

提交回复
热议问题