5 ms·
I am suspicious of names containing "and". I agree about clarity over everything else, but then why even make a function if all it does is calling 2 functions
by ajuc 11y ago
I am suspicious of names containing "and".
I agree about clarity over everything else, but then why even make a function if all it does is calling 2 functions in sequence, and you can't name it on higher level of abstraction?
In this case wouldn't
[x+1 for x in input_list.reversed()]
be even clearer?
True, functions allow you to change code in one place and affect many places (so they prevent "forgot to update one place" errors), but when function is named from its implementation instead of its purpose - it doesn't make much sense to change implementation anyway. The absolute worst thing is
def reverseListAndAddOneToEachElement(input_list):
#requirements changed
return [x+2 for x in input_list.reversed()]
So either I repeat myself by copy-pasting code, or I repeat myself by saying the same thing in implementation and in the name. It's the hardest problem in naming for me.
- Kiro 11y ago> but then why even make a function if all it does is calling 2 functions in sequence How else would you execute the functions? What initiates them?
- ajuc 11y agoThe "and you can't name it on higher level of abstraction" part is important.
- jacquesm 11y agoFunction composition is by far the better solution, it is future proof and requires less memorization.
- eru 11y agoIt's syntax is just somewhat ugly in Python. (Compared to, say, Haskell.)