Clean Code Tip: Keep the parameters in a consistent order
Following a coherent standard, even for parameters order, helps developers when writing and, even more, reading code. How to do that?

I’m a Principal Backend Developer with more than 10 years of professional experience. I’ve been working with Microsoft platforms and frameworks since 2014.
I’ve worked with many tools and frameworks, such as Azure, MongoDB, Docker, and, of course, .NET and C#. I love learning new things, and I think that the best way to learn is to share: that’s why I started my journey as a content creator and conference speaker.
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 AddSectionToContext and 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
Conclusion
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!
🐧





