After years of cowboy coding, I decided to pick up a book on how to write good quality code. I'm reading Clean Code by Robert Cecil Martin. In chapter 3 (functions) there is a section on dyadic functions. Here is an excerpt from the book.
Even obvious dyadic functions like
assertEquals(expected, actual)
are problematic. How many times have you put the actual where the expected should be? The two arguments have no natural ordering. The expected, actual ordering is a convention that requires practice to learn.
The author makes a compelling point. I work in machine learning and come across this all the time. For example, all of the metric functions in the sklearn library (probably the most used python library in the field) require you to be careful of the order of the inputs. As an example sklearn.metrics.homogeneity_score takes as inputs labels_true
and labels_pred
. What this function does isn't too relevant, what is relevant is that if you switch the order of the inputs no error will be thrown. In fact switching the inputs is equivalent to using another function in the library.
However the book does not go on to say a sensible fix for functions such as assertEquals
. I cannot think of a fix for assertEquals
or for functions I often come across like the one described above. What are good practices to solve this issue?