JamesDevJim / game-zulu

First game of the coolest project
0 stars 1 forks source link

Prefer keyword arguments as a general rule #10

Open shuttle1987 opened 4 years ago

shuttle1987 commented 4 years ago

Take for example this line:

https://github.com/JamesDevJim/game-zulu/blob/4b46243499803992d0ef07bb856fb49bb8f72240/whiskey.py#L56

This can be rewritten to use keyword arguments as follows:

 light.strip(animation='A=303', duration='D=2000', color='C=0x00FF00',palette='P=0')

This was you don't have to be thinking about what order these arguments are in. Seeing as these arguments are distinct and re-ordering them arbitrarily makes no sense it's best to not use semantics that depend on the positions of the arguments.

If you wan to force the calling sites to use keyword arguments only you can change:

https://github.com/JamesDevJim/game-zulu/blob/2689e3baa95e1bcc8c3f203fe3f701a94191ed72/test_light_strip.py#L27

To this:

 def strip(*, animation, brightness=None, duration=None , color=None, palette=None): 

The * as the first argument has special meaning here and effectively disables the use of positional (non-named) arguments

shuttle1987 commented 4 years ago

For educational reasons I think it's worth noting that function calls in Python are slow and the keyword arguments is not something that is particularly slower than positional arguments. As a result the extra readability and reduced possibilities for bugs/defects that this allows doesn't come at the expense of much of a tradeoff in other areas.