Why Java developers hate .NET

I have been struggling with .NET. Actually, I have been fighting pitched battles with it.

All I want to do is take our existing Java client example code and write an equivalent in C#. Easy, right?

Trisha's Guide to Converting Java to C

Turns out writing the actual C# is relatively straightforward. Putting to one side the question of writing optimal code (these are very basic samples after all), to get the examples to compile and run was a simple process:

1. Find-and-replace the following (only you can't use Ctrl+R like I expect. Sigh.)

final = readonly //(but remove from method params)
System.out.printf = Console.WriteLine
Map = Dictionary
BigDecimal = decimal
Set...           //oh.  I have no idea.

2. When using callbacks, replace anonymous inner classes with delegates

Java:

something.doSomething(new SomethingRequest(),
  new SomethingCallBack()
  {
    public void onSuccess()
    {
      System.out.println(“Action successful”);
    }

    public void onFailure()
    {
      System.err.println(“Action failed”);
    }
  });

C#:

private void foo ()
{
  _something.DoSomething(new SomethingRequest(), 
                         SomethingSuccess, 
                         SomethingFailure);
}

private void SomethingSuccess()
{
  Console.WriteLine(“Action successful”);
}

private void SomethingFailure()
{
  Console.Error.WriteLine(“Action failed”);
}

I rather like this pattern actually. You can't really tell in the noddy example above, but the C# code is generally shorter and more reusable.

3. Replace getters and setters with properties

Java:

private class MyClass
{
  private BigDecimal myField = new BigDecimal("-1.0");

  public BigDecimal getMyField()
  {
    return myField;
  }

  public void setMyField(BigDecimal instructionId)
  {
    this.myField = myField
  }
}

C#:

internal class MyClass
{
  public decimal MyField{ get; set; }
}

What the... where did all my code go??

My Thoughts

I was pleasantly surprised with the language. In general, for what I was doing, the equivalent C# was a lot less code. The fact that the syntax is not wildly different from Java made the transition relatively easy, even if I don't get all the nuances.

I didn't really like the enums - I can see what purpose they serve, but I quite like the way the Java ones are pretty much classes in their own right with properties of their own - it allows you to encapsulate some of your simplest domain objects. But it's a minor point, not a deal-breaker.

The C# capitalisation makes me queasy though. I just can't get my head around it. In Java, if I say:

com.mechanitis.foo.Bar

I know the class is Bar and the rest is the package (or namespace, or whatever). This is more useful when you're using static methods and so forth:

com.mechanitis.foo.Bar.thatThingIWantToDo()

In C#, I know the thing at the end is a method and the thing before that is a class, but it doesn't jump out at me:

Com.Mechanitis.Foo.Bar.ThatThingIWantToDo();

And if you're using a property:

Com.Mechanitis.Foo.Bar.MyProperty;

The whole thing makes me dizzy.

You could argue that all this is redundant with modern tools and IDEs doing all the heavy lifting for you - nice colourisation etc.

Which brings me to The Rant.

Oh My Dear God What Is Wrong With Visual Studio?

C# needs to be a shorter, more succinct language because it takes three billion times longer to do anything in Visual bloody Studio.

I'm coming at this from a Java, IntelliJ point of view, so there's always the possibility it might be lack of familiarity with the tool, rather than the tool itself, which is the problem. It's a long time since I used VS, and that was back in the 90s when I was doing ASP and COM (shhh, don't tell anyone).

But things shouldn't be this hard. I was ready to accept, due to my newbie status, the IDE not helping me. I didn't expect it to actively hinder me.

For example: it took hours of my life that I will never get back to discover that you can't simply run a class that has a main method by right-clicking and selecting "run" (note: I'm not even trying Ctrl+F10). No. I have to select, at a Solution level, which Projects are runnable. Then I have to select at the Project level the class with the main method I actually want to run. Then, it opens up a terminal window and runs it in there, which promptly disappears when the program errors or finishes. What's wrong with outputting in the output window of the IDE? Is that not what it's for?

Finally I worked out how to run the cursed program (seriously, like that's not the first thing everyone wants to do? How do people write "Hello World"?). Now, where are the command line arguments? Of course, they're at the project properties level too, because each project only has one entry point. I seriously had to Google that too because I couldn't work it out from the IDE alone.

The next day, my ReSharper licence had expired. I decided I should attempt to limp on without it, after all hundreds of developers must be surviving with just Visual Studio. How bad could it be?

Very bad, it turns out.

All those helpful little squiggles I was leaning heavily on to convert my Java to C#?

  • The red to tell you you're utterly wrong.
  • The orange to tell you could be using less code.
  • The blue to remind you to stop thinking in Java and helpfully offer corrected naming.
  • The green to suggest stuff that C# could do differently.

Yeah, all gone.

How do people code like this? Do they really just do a build to check if it's all wrong or not?

Next, I try to find a class. I actually have no idea how to do this, because I can't use Ctrl+N. So I Ctrl

I realise this is a waste of time anyway, because one thing that really annoys me about Visual Studio is that I can't find a way to sync the project tree to the class file I'm looking at. I can't get it to jump to highlight where I am. When I'm using IntelliJ, I find this dead useful when I want to see other stuff in the same package.

Less than ten minutes after attempting to use Visual Studio without ReSharper, I've abandoned the fight and tracked down a licence and installed it.

Documentation isn't a standard function of .NET?

What sort of message does this give developers? Documentation isn't important?

I always thought Javadoc was pretty ugly and clunky. In addition, now our IDEs generate so much, it's frequently meaningless. But it is generated by standard Java tools, and HTML is a standard format that can be read on pretty much any computer with any operating system.

I could not believe how hard it was to get the XML comments out of the C# into something the user can actually read. Thank goodness, some enterprising member of the team had already done that for us. All I needed to do was hack/crowbar the tutorial I'd been working on into the generated documentation, so it ended up in the Windows help files in some fashion.

I know there's a way to get HTML/XML files into the end result using Sandcastle, but hours of Googling only told me it was possible, not how to do it. I still have no idea what the correct question is to ask to find the solution.

Right now, this is an unsolved mystery. Our .NET client users will have to read the plain HTML I'm afraid.

In Conclusion

Are we lowly Java developers spoilt with our shiny IDEs?

Or is there such a fundamentally different approach to development for .NET people that all the functionality is there, I just can't find it?

I'm disappointed if I'm honest. I'm sure the .NET camp used to tout their tools as their superiority. I ended up feeling sorry for the poor NET people. Is there anything they can use that isn't Visual Studio?

Despite the nasty capitalisation I found myself surprisingly taken with C#. But until they can give me a proper development environment, I won't be tempted by the dark side any time soon.

Author

  • Trisha Gee

    Trisha is a software engineer, Java Champion and author. Trisha has developed Java applications for finance, manufacturing and non-profit organisations, and she's a lead developer advocate at Gradle.