On Friday, 28 February 2014 at 12:28:22 UTC, John Colvin wrote:
Do you mean:

Chaining operations that can return null (or some other known-to-be-invalid state) is a code-smell.


That is the worse.

That's quite different to saying:

Chaining operations using . is a code-smell.

which is what deadalnix said.


Yes. That must not be understood as a hard rule, but something that is true most of the time. You want to look up "Law of Demeter". The problem when you chain is that you make a lot of code dependent on the structure of the project, which makes it hard to evolve or maintain the project, as any change in the structure will impact more code than it should.

Obviously, this idea may be in tension with other principle, and good judgement is always welcome. As a general rule, unless you have some really good reason to, avoid chaining.


Either way, a do-this-if-you-can pattern is quite reasonable IMO. However, I do question whether it's common enough to justify syntax sugar.

This pattern exist. That is called the maybe monad. To take previous example:
writeln(person ? person.name : "");

Can become
Maybe!Person person;

writeln(person.name.get(""));

If person is a Maybe!Person, the person.name is a Maybe!string (assuming name is a string) and the get method will provide a default value if nothing is present in the Maybe monad.

That ensure that null is checked consistently and provide what you want, a do-this-if-you-can pattern.

Reply via email to