I know this sounds like a subjective answer, but I will try to make the question as objective as possible, because an objective answer to the question would be the most helpful.
I recently had a code reviewer point out that I have a habit of including prepositions at the end of my methods. Here's a recent method I wrote as an extension method to the Point
class:
var rectangle = new Rectangle(0, 0, 2, 2);
var point = new Point(3, 1);
var result = point.DistanceTo(rectangle);
My code reviewer mentioned that the method should be point.Distance(rectangle)
. I've always considered this subjective and a matter of style. However, I have noticed more .NET API design going in this direction. For example, with NUnit's Fluent Interface, you have:
Assert.That(result, Is.EqualTo(1.0));
I have also seen this with Linq:
list.CopyTo(anotherList);
list.IndexOf(item);
list.RemoveAt(0);
Is there any settled or consistent way that .NET and/or Third Party API designers use prepositions at the end of methods? Or is it just a matter of style, and subjective? Has the API design in the .NET framework itself evolved with this policy, or has it always been in place?
Prepositions are fine, as long as the object of the preposition is the corresponding argument (usually the first, never the this
argument of an extension method).
An example where it might be an argument later than the first:
array.CopyCountedTo(count, destination);
To answer your question about whether this has evolved with .NET, no it hasn't.
Prepositions in function names are far more widespread than Microsoft (e.g. Java has string.charAt
and string.indexOf
), also .NET has been using it a lot longer than LINQ (e.g. ArrayList.IndexOf
in .NET 1.0).
Agreed, it is subjective but I go with the premise that I don't want to have to "Go To Definition" to work out what a method call does. The name shouldn't expand into a novel but being descriptive certainly doesn't hurt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With