Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does including prepositions at the end of method names follow or detract from normal C# API design?

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?

like image 717
Michael Hedgpeth Avatar asked Nov 23 '10 12:11

Michael Hedgpeth


2 Answers

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).

like image 153
Ben Voigt Avatar answered Oct 13 '22 01:10

Ben Voigt


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.

like image 28
Lazarus Avatar answered Oct 13 '22 01:10

Lazarus