Hacker News new | ask | show | jobs
by mattmanser 3504 days ago
What magic are you talking about? Various .Net frameworks have silly amounts of magic, but C# itself?
2 comments

When I started going into C# from Java a bit, there were two things slightly annoying me:

- Class extensions: I saw code examples online that just wouldn't work for me because the compiler told me a certain method of a built-in class wouldn't exist. After I while I found out that the author of that snippet had used class extensions and not bothered to mention.

- The var keyword: While this is sometimes nice for quick scripting or hacking in the debugger console, it opens the door for hard-to-read code.

Those are not necessarily "magic", but potential obscurities that can't happen in Java, simply because Java lacks these features.

> The var keyword: While this is sometimes nice for quick scripting or hacking in the debugger console, it opens the door for hard-to-read code.

I find type inference to be the opposite, generally, because it encourages good naming, and it reduces noise/boilerplate. It also makes writing and refactoring faster (don't have to think about the return types, just the code flow).

The only situation I can even think of where it is a problem is when you are passing something to an overloaded method. For example:

    void DoSomething(MySpecialType value);
    void DoSomething(string value);

    ////

    var result = GetData();
    DoSomething(result);
  
You can't tell which one is going to be called by just looking at that code. However:

* GetData() is probably badly named in this situation

* If DoSomething() does something very different depending on the type passed, it should have different names, not be overloaded

On top of that, this code can also have the same problem without type inference:

    DoSomething(GetData())
> It also makes writing and refactoring faster (don't have to think about the return types, just the code flow).

I see what you mean, but isn't this one of the advantages of explicitly declaring types? If you refactor a method to return a different (incompatible) type, you'll have to touch all affected code parts, possibly revealing uninteded consequences of the change.

Also, I think remembering that autocompletion in VS had some trouble with correct type inference in some cases. But it's been a couple of years, maybe things are different now.

If you change the return type of a method without using var you would still have the exact same problems. That's not an actual problem.

I've used var since it came out years ago and never had this autocompletion problem you talk about.

You sometimes had to explicitly declare a type in foreach loops, but that had more to do with shitty legacy APIs that used abstract classes as return types. Wasn't really var's fault though.

Consider the following scenario:

You have a Toilet object with a method flush.

  var toilet = house.getToilet();
  toilet.flush();
Now you find out that updating Toilet objects is somehow expensive and decide to wrap it into a Cache<Toilet>. Unfortunately, Cache also has a flush method, so there will be no compile time error, but functionality is broken now.

I agree this problem would also occur when using chained calls (house.getToiled().flush()), but explicit types could cover at least some of the cases.

> Now you find out that updating Toilet objects is somehow expensive and decide to wrap it into a Cache<Toilet>. Unfortunately, Cache also has a flush method, so there will be no compile time error, but functionality is broken now.

This has happened to me exactly 0 times since type inference was introduced in 2007/2008.

> If you refactor a method to return a different (incompatible) type, you'll have to touch all affected code parts, possibly revealing uninteded consequences of the change.

If you can refactor your code such that you change the return type of a method such that it returns an object of a different type, which nonetheless implements all of the methods used by clients of your type that accept and return types which in turn accept and return types that line up with the expectations of their clients, etc, etc, without having to look at or touch any of those clients, and end up with something that happily compiles but gives an unexpected result, you need to have a long and hard look at how you're using the language's type system, and why it isn't encoding your assumptions in a sane way.

Your code shouldn't be compatible with a different type unless it makes sense for your code to be compatible with that type. That's what the type system is for.

One single common method is enough if that's the only one used in said context.

Sure, different types shouldn't use the same method name for different functionality, but it happens. And when such a scenario happens, I imagine it will be quite nasty to debug.

The var syntax definitely doesn't lead to hard to read code. It's one of those assumptions people who haven't actually used it make.

99% of the time it's completely obvious what the variable is because you can just look at the right side of the equal sign.

Also, you can just hover over it.

As for the extension methods, they pretty much fixed that in VS 2015, it will now tell you which using statement you need to include.

Extension methods are a good solution to a specific problem.

Do you really think something like this:

Dictionary<string, Tuple<int, List<string>>> foo = new Dictionary<string, Tuple<int, List<string>>>();

Is prefferable to:

var foo = new Dictionary<string, Tuple<int, List<string>>>();

What do you get from the former that makes the eye-pain worth it?

Also, the var keyword is necessary for anonymous types if you want to hold a reference to one. And this is a language feature I sorely miss in Java-land.

Since Java 7 you can use the "diamond operator" in such cases:

Dictionary<string, Tuple<int, List<string>>> foo = new Dictionary<>();

Lombok includes the "val" keyword, which infers the type from the initializer expression: https://projectlombok.org/features/val.html
If you have visual studio it tells you what the var is.
I noticed my post was down voted. I am just sharing information. I don't understand why that merits a down vote.
So, where do I get a version fo Visual Studio that’s open source, and works on Linux? And not just the little text editor VS Code, a real IDE?

For most devs, Visual Studio isn’t a realistic option.

It's called Monodevelop, and it would have taken you 5 seconds to find using Google.
I have a lot of respect for the Monodevelop team, because writing IDEs is really hard, but it is really not an awesome tool for writing C#. (The XS modifications were never great, either. I'd rather write code in Sublime Text because Sublime won't randomly lose lines in its text widget.)
Well they just released a preview for VS on macOS.

Project Rider by Jetbrains is a cross platform C# IDE.

VS Code has some tooling.

etc...

VS on macOS is Xamarin Studio rebranded, which Xamarin has never wanted to port for Linux (despite being a fork of MonoDevelop). I don't see MS changing that.

Project Rider is still extremely green and is not FOSS, nor free as in free beer.

VS Code is extremely limited and the little support it has is for .NET Core only.

I'm right now working on an MVC 5 project on Linux and I have to jump around MonoDevelop and Rider to have a workable dev environment; with the occasional jump to a Windows VM and Visual Studio to make sure everything works over there (it often doesn't).

.NET IS NOT a viable stack for developing on Linux and it's an inferior one in macOS. If you want to do real work with it you have to eat, breathe and live Windows and VS.

If I didn't absolutely have to work with .NET in this particular situation I would've laughed all my way back to a real cross-platform stack.

> .NET IS NOT a viable stack for developing on Linux and it's an inferior one in macOS.

.NET isn't, and wasn't officially meant to be. That's your problem.

I've been working on a .net core web app on my mac exclusively now for the last 8 weeks and have had no problems with it. I do check to make sure it works in full VS/Windows occasionally and every single time its worked without fail, and without a single change. Just pull the code and go. I haven't needed to touch windows but its nice to know it still works over there anyway.

Using VS Code as my main editor. Tried Rider a little yesterday and I'll probably switch to that once its more stable as it has a few more tools for refactoring etc.

So .NET core is clearly what's intended to be used cross platform, and it works great.

Most developers use Windows, where VS Professional (under the badge of VS Community) is free. The set of developers who use Linux and OS X (and I mean, I am one) is not representative of developers of a whole.
You mean most .NET developers use Windows?
I mean most developers, period, use Windows, on a worldwide basis. Most web developers use Windows. Most C/C++ developers use Windows. Most Android developers use Windows. Almost all .NET developers use Windows--but most developers, period, use Windows.

Windows is the de facto standard in most places that aren't chasing the leading edge.

> Class extensions: I saw code examples online that just wouldn't work for me because the compiler told me a certain method of a built-in class wouldn't exist.

One thing that VS can now do (as of VS 2015) is tell you where exactly the "missing" extension method is, and offer to add a corresponding `using` declaration for you.

Not sure if VSCode or VS/Mac support that, though. But they are all built on the same code analysis engine, so there's no reason why they couldn't.

Interesting. I wonder what would be your opinion on languages with even more magic, like Swift.
I'm not saying it's a bad thing, I actually like both. It just takes more time to get up to speed and there is more surface of attack for devs to screw things up. That's also what I don't like e.g. about JS: It's so unrestrictive you can do a lot of wild things with it - which is sometimes good but often bad.

Idk about Swift, never used it yet.

I guess you are out of touch with Java.

Extensions methods can be approached via default methods.

Java 9, latest by 10, will get var. It is already on the approved roadmap.

both of these are great tools, but as with many you have tradeoffs. Extension methods give you a way to produce very clean reusable code to extend behaviour of classes. Downside is you do need to bring in the extension method class (not a big deal).

Java is a shitty language exactly because they don't put in things like these out of fear that people don't know how to use them.

Java has default methods on interfaces instead. It's their take on the same issue, in a way. They considered the fact that with C#'s extension methods types not owned by the developer can be extended (as well as platform types) a problem and opted instead for a solution where someone who owns a type also controls its extensions. Default methods in Java are purely a way of adding stuff to interfaces in your API without breaking existing implementations (but also allowing future implementations to override those methods). C# on the other hand is merely a bit of syntactic sugar which can be confusing at times. (I have to admit not to be a fan of various libraries that add extension methods to integers and similar. It's not a good way of exposing an API in my eyes.)

TL;DR: Java has something similar, it just came from a different view of how the problem should be solved. Both approaches have advantages and drawbacks.

Silly amounts of magic? Ever looked at a Java Spring project?!
THIS!