I often find it hard to decide and am inconsistent. Are there some rules I could follow?
For example:
def remove_except(haystack, needle, exclude_value):
for key in hackstack:
if key in needle and haystack[key] != exclude_value:
hackstack.pop(key)
could easily be:
def remove_except(needle, exclude_value, haystack):
for key in hackstack:
if key in needle and haystack[key] != exclude_value:
hackstack.pop(key)
Not a perfect example because I tend to put the variable being modified as the first parameter, e.g. haystack, and that might even be a convention but apart from that I'm unsure what to do.
I'm interested for more languages than just Python.
The only rule is be consistent. That doesn't only mean be consistent between and within the APIs you define, but also be consistent with any APIs commonly used in the environment. Note that this may mean you do things differently in one environment than in another. Try to fit in.
Now, with Python and JavaScript, you can use object orientation to take at least the haystack out of the question, because the haystack will be (or at least, can be) an object you're acting on and so how you do that will be dictated by the language (e.g., delete haystack[needle];
[JavaScript for removing a property from an object] or del haystack[needle]
[Python for removing a dictionary entry]). C isn't object-oriented (C++, D, Objective-C, and several other derivatives are) and so you'll need to look for the convention there.
Subjectively speaking, in my experience, the haystack
normally comes first, but that may just be my experience. Similarly, my subjective opinion would be that the exclude_value
would be the last thing (so, haystack
, needle
, exclude_value
) because there would be other operations that would take haystack
, needle
, something_else
— but that's just an opinion.