For instance, wouldn't something like: shop.Sell(15)Notebooks (although looking pretty unusual) be more readable than shop.SellNotebooks(15)?
No, it wouldn't.
Programmers don't read code as English; they read it as code. This:
shop.SellNotebooks(15);
is very straightforward; it means "Call the SellNotebooks
method on the shop
object, passing it 15 as a parameter." This:
shop.Sell(15)Notebooks
means... Well, what does it mean, exactly? It appears to call the Sell
method on the shop
object, but what's going on after that?
In most single-dispatch object-oriented languages in common use today (such as Java and C++), the usual notation is noun-verb. That is, you take an object (a noun), and you apply a verb to it (a method). Doing things this way provides a consistent, easily understandable notation.
There have been a number of attempts to make such constructs more English-like, the most notable of which is the fluent interface. When it works well, it is a joy:
var query = translations
.Where (t => t.Key.Contains("a"))
.OrderBy (t => t.Value.Length)
.Select (t => t.Value.ToUpper());
or
var sizer = Sizer.FromImage(inputImage)
.ToLocation(outputImage)
.ReduceByPercent(50)
.OutputImageFormat(ImageFormat.Jpeg);
When it doesn't, well, not so much:
money.Add(20).Dollars().And().TransferTo().MyCheckingAccount().In(3).Days();
How does this work under the hood, exactly? Fluent interfaces are just like many other great ideas in computing; you can have too much of a good thing.
However, everyone understands this:
object.Verb(data);
And you don't have to squint that hard to figure out what it does.
shop.Sell(15_Notebooks)
ormoney.Add(20_Dollars)
, with physical units integrated in the values. See Are units of measurement unique to F#? for some examples of languages with this feature. – mouviciel Nov 30 '16 at 16:34money.Add(20).Dollars()
is an abomination – CodesInChaos Nov 30 '16 at 16:36