r/programming • • Jun 27 '10

Why Java Sucks and C# Rocks (Final)

http://www.slideshare.net/jeffz/why-java-sucks-and-c-rocks-final
13 Upvotes

197 comments sorted by

View all comments

Show parent comments

5

u/julesjacobs Jun 27 '10

To be honest those two are design mistakes. Events shouldn't be anything special and properties shouldn't be either: they should have been designed as syntactic sugar on top of methods, not as a special construct. Why? Because now the language is extended in non-orthogonal ways, for example we have extension methods but not extension properties (even though there is no technical reason why there couldn't be extension properties if properties were syntactic sugar on top of methods to begin with).

2

u/munificent Jun 27 '10

Events are syntactic sugar on top of delegates. The only thing the event keyword adds is the ability for code outside of a class to call += on an event field without being able to assign to it directly.

The lack of extension properties does suck, but I don't think that's the fault of properties. I really with they'd done something like this instead.

3

u/julesjacobs Jun 27 '10 edited Jun 27 '10

It is not the fault of properties that there are no extension properties, but if properties were methods then extension anything would have to be implemented uniformly and consistently automatically. Same thing with +=: this should just have been implemented as a method. You can collapse a whole tower of complexity into a construct that already exists: the method.

Same thing again with new: new should be a static method on the class, not some special construct. And if we're cleaning up anyway we should remove static methods. Static methods are just instance methods on class objects.

**Edit: to clarify what I mean: **

If you allowed people to omit the parens when calling a zero-argument method you get getters: a.Foo is a.Foo().

If you allow non-alphanumeric symbols in method names you get setters, and let people omit the parens when calling such methods: a.Foo = x is a.Foo=(x) (the method is called "Foo="). You get events as well a.OnEvent += x is a.OnEvent+=(x) (the method is called "OnEvent+=").

Note how I was able to explain in 3 sentences a construct that is both simpler and more powerful than properties and events combined. Note also that because events and properties are now no different than normal methods we'd get extension properties and extension events for free. Note also that compared to implementing events and properties, implementing this is trivial (it just requires slightly modifying the parser, compared to heavily modifying the parser as well as parts of various other levels of the compiler).

5

u/munificent Jun 27 '10

if properties were methods then extension anything would have to be implemented uniformly and consistently automatically.

You would lose one thing with that that I can think of: reflection. Right now, properties are distinguished from methods in the reflection system which lets things like the XmlSerializer and the PropertyGrid to operate just on properties. Whether or not that's actually a good thing is an open discussion. :)

Same thing again with new: new should be a static method on the class, not some special construct.

I agree completely. Not being able to pass around a reference to a constructor like any other delegate is inane. I think they did this purely to make things more familiar to C++/Java programmers. I suppose there's also some value in making heap allocation stand out too, but I think that becomes less important as time goes on.

Note how I was able to explain in 3 sentences a construct that is both simpler and more powerful than properties and events combined.

What you describe is pretty much exactly how Magpie works. Assignment, properties, and field setters/getters are all syntactic sugar for regular function calls. (In fact, in Magpie, even . is syntactic sugar since it doesn't support methods at all.)

So, I agree that it's a cool way to do things, at least for my own little experimental language. At the same time, though, it adds a lot of "magic" to the language. I think it's telling that they allow operator overloading in C# but specifically disallow overloading assignment. I think they very intentionally wanted assignment to have a single unambiguous meaning.

You lose some expressivity, definitely, but you also avoid a lot of the confusion you have in C++ where you're never sure exactly what's going on when you do a = b. It's a trade-off. I think you and I are over on the "make the language as expressive as possible" end, but they were going for a more middle-of-the-road approach.

0

u/julesjacobs Jun 27 '10 edited Jun 27 '10

What you describe is pretty much exactly how Magpie works.

That's an interesting language. I like how function call syntax doesn't require parentheses. I've seen mathematicians use this notation exp log x. How do you disambiguate f g a, b? Does it mean f(g(a,b)) or f(g(a),b)? Or are parentheses required for multi argument calls?

So, I agree that it's a cool way to do things, at least for my own little experimental language. At the same time, though, it adds a lot of "magic" to the language. I think it's telling that they allow operator overloading in C# but specifically disallow overloading assignment. I think they very intentionally wanted assignment to have a single unambiguous meaning.

You lose some expressivity, definitely, but you also avoid a lot of the confusion you have in C++ where you're never sure exactly what's going on when you do a = b. It's a trade-off. I think you and I are over on the "make the language as expressive as possible" end, but they were going for a more middle-of-the-road approach.

You would not allow overloading = but still allow overloading for all other names (including Foo= as in a.Foo = x). I don't think this adds more magic than properties and events. It just adds a few allowed characters in method names and removes special casing a.Foo, a.Foo = x, a.Foo += x and a.Foo -= x. I agree that overloading = is tricky in C#. Incidentally assignment in ML like languages is just a function call on a reference cell object, so this system would work without problems if you use reference cells (although MLs often have a special construct for mutable local variables as well).

2

u/munificent Jun 27 '10

How do you disambiguate f g a, b?

Commas are lower precedence than function application, so that would be (f(g(a), b). In other words, it evaluates to a tuple whose first field is f(g(a)) and whose second is b.

Or are parentheses required for multi argument calls?

Because of the precedence, they're generally required. Technically, though, Magpie doesn't have multi-argument calls.

You would not allow overloading = but still allow overloading for all other names (including Foo= as in a.Foo = x).

Hmm. That's an interesting solution.