5

Why is not it a common practice for languages to have method arguments passed inside method names?

For instance, wouldn't something like: shop.Sell(15)Notebooks (although looking pretty unusual) be more readable than shop.SellNotebooks(15)?

Also, this could simplify fluent APIs: instead of implementing additional interfaces for things like money.Add(20).Dollars() we could simply use methods: money.Add(20)Dollars and so on.

5

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?


Execution in the Kingdom of Nouns

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.

  • Constructing fluent interface could occasionaly increase the complexity of the supporting code. For instance, if I would like to have something like Sizer.FromImage(...).ToLocation(...) the FromImage method would have to return an additional interface which would then have an extension method added over it (in case of C#). And while this is still worth the effort, wouldn't in-method arguments eliminate these steps at all, still providing almost-identical API? – bashis Nov 30 '16 at 16:17
  • @bashis: Of course. Well, not in-method arguments. The language construct you propose would be quite difficult for a language compiler to parse. My point is that object.Verb() notation is easy to parse, both for the programmer and the compiler. – Robert Harvey Nov 30 '16 at 16:18
  • 2
    @bashis: What increases readability is consistency, not variety. The more different ways you make to represent something, the more time it takes to mentally figure out which way is in play. – Robert Harvey Nov 30 '16 at 16:57
  • 1
    "Because object-oriented notation is noun-verb" -- except that the term "object oriented" was originally coined to describe Smalltalk, which has almost exactly the kind of argument-within-method-name notation described in the question, so this clearly isn't a valid argument. – Periata Breatta Nov 30 '16 at 18:07
  • 2
    Reducing OOP to a noun-verb relationship is silly. I dislike seeing this linguistic approach being pulled out of thin air, since it has no basis in either OOP or programming language ergonomics. There is no standard OOP grammar. Yes, the single-dispatch OOP languages descending from C and C++ generally use noun.verb(arg), but there are plenty of languages that use verb noun arg or something entirely else. Likening methods to verbs is OK when explaining OOP, but taking that similarity too literally is misleading. – amon Dec 3 '16 at 11:56
3

There are quite a number of languages that have "keyword selectors". In those languages, arguments are passed after a colon inside the message selector. E.g., the following is valid syntax in (at least) Smalltalk, Self, Us, Korz, Fancy, Slate, Newspeak, Objective-C, Objective-C++, Objective-Modula-2, and possibly others:

anArray at: 3 put: "Hello".

This puts the string "Hello" at index 3 of anArray.

It is not quite what you want, though, since it only allows arguments after selector parts, not before.

I know I have seen a language that allows what you want somewhere, but I can't for the life of me remember the name.


The answers to the related question mentioned in the comments above list a couple of languages that support your exact syntax: Agda, Inform7, TeX, Gherkin, robotframework scripting language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.