If you have a set of related functions, use always a coherent order of parameters.
Take this bad example:
IEnumerable<Section> GetSections(Context context); void AddSectionToContext(Context context, Section newSection); void AddSectionsToContext(IEnumerable<Section> newSections, Context context);
Notice the order of the parameters passed to
AddSectionsToContext: they are swapped!
Quite confusing, isn't it?
For sure, the code is harder to understand, since the order of the parameters is not what the reader expects it to be.
But, even worse, this issue may lead to hard-to-find bugs, especially when parameters are of the same type.
Think of this example:
IEnumerable<Item> GetPhotos(string type, string country); IEnumerable<Item> GetVideos(string country, string type);
Well, what could possibly go wrong?!?
We have two ways to prevent possible issues:
- use coherent order: for instance,
typeis always the first parameter
- pass objects instead: you'll add a bit more code, but you'll prevent those issues
To read more about this code smell, check out this article by Maxi Contieri!
This article first appeared on Code4IT
To recap, always pay attention to the order of the parameters!
- keep them always in the same order
- use easy-to-understand order (remember the Principle of Least Surprise?)
- use objects instead, if necessary.
👉 Let's discuss it on Twitter or in the comment section below!