Hacker News new | ask | show | jobs
by gameswithgo 2047 days ago
C#s uses these heavily, and most of my professional career has been using C#. I understand the convenience they offer but I do not think they are worth the complexity they add to the language. There is a whole zoo of syntax around them, as the author states arbitrary computation can sneak up on you when you don't expect it, and ultimately all you are doing is creating some shorthand for functions.

If you like, make some shorthand for creating a getter/setter pair that link with some field, that would be fine. But keep them functions, so that it is clear that they are functions to consumers.

1 comments

I think there is a simple way around the complexity: Make the field "foo.bar" indistinguishable from the outside from the getter / setter pair bar() / set_bar(). This has the disadvantage of losing the ability to distinguish method calls and references by the presence of parens, but I think overall it should pay off.

I have written up some more details here: https://stefan-haustein.com/simplifying-properties

This is also known as the uniform access principle: https://en.wikipedia.org/wiki/Uniform_access_principle

I believe Ruby and Smalltalk more or less work that way.

Thanks a lot for the pointer! I was initially distracted by listing C#, JS and Python as examples, but the article clearly states:

"Many programming languages do not strictly support the UAP but do support forms of it. Properties, which are provided in a number of programming languages, address the problem Meyer was addressing with his UAP in a different way. Instead of providing a single uniform notation, properties provide a way to invoke a method of an object while using the same notation as is used for attribute access. The separate method invocation syntax is still available."

Eiffel was one of the first languages to introduce it that way.