Clean Code Tip: Keep the parameters in a consistent order

Related Articles

If you have a set of related functions, always use a coherent order of parameters.

take it Bad Example:

IEnumerable<Section> GetSections(Context context);

void AddSectionToContext(Context context, Section newSection);

void AddSectionsToContext(IEnumerable<Section> newSections, Context context);

Note the order of the parameters transferred to it AddSectionToContext and AddSectionsToContext: They are replaced!

Pretty confusing, no?

For sure, the code is more difficult to understand, since the order parameters are not what the reader expects from it.

But, worse, this problem can lead to bugs that are hard to find, especially when the 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 go wrong?!?

We have two ways to avoid possible problems:

  1. Use a coherent order: for example, type Is always the first parameter
  2. Move objects instead: You will add some more code, but you will avoid these problems

To read more about the smell of this code, check it out This article By Maxi Kuntiri!


In conclusion, always pay attention to the order of the parameters!

  • Always keep them in the same order
  • Use an easy-to-understand order (remember the smallest surprise principle?)
  • Use objects instead, if necessary.

👉 Let’s discuss it On Twitter Or in the comments section below!





Please enter your comment!
Please enter your name here

Popular Articles