I'm loving .NET Core
I’ve been a web programmer all my professional life. Though I learned Java in school, I see now that I’ve always worked with languages which have the lowest level of entry.
This means I’m really good with Javascript and PHP, as they have arguably been the easiest languages to use for web development over the last decade and a half.
For developing anything server-side, PHP has always suited my needs. Great packages can be found all over Composer’s repository like the ones by the PHP league and, especially, the elegant Symfony components.
There are also powerful web frameworks written in PHP. I definitely feel Laravel stands out as one of the best across the whole programming landscape in that regard.
I never felt like I was missing out on anything and generally thought I was happy with my experiences. That was until recently when I did my first .NET Core application.
Now I don’t want to be a PHP developer at all.
C# and .NET Core
Fear not, I’m not turning this into a tirade of PHP criticism. I still think PHP definitely can do web development well.
What I am trying to express here is that .NET Core and C# are raising my general enjoyment when working on code for any given similar application. They, directly and indirectly, make my life easier as a developer. Once you get a taste for that level of comfort, it’s hard to go back.
Right away when working on my first .NET Core project I was confronted by a couple of things I could never have in PHP:
C#: a consistent, typed language
Coming from a loosely typed language, you may think typed languages can get tiresome. Even newer languages like Ruby and Python are loosely typed. I didn’t really think you’d need it at all before.
Certainly, when writing C# code, object typing can feel quite verbose. The thing is, in the context of work, when you have the pressure of quickly figuring out and permanently fixing bugs without necessarily knowing the whole application context, types are a true gift to the programmer.
As an added bonus, I’ve noticed that being forced to use strong typing naturally improves the code I write. It’s not that I couldn’t have been able to write the same thing in PHP, it’s that I probably wouldn’t have.
For instance, there would likely have been logical decisions done by comparing strings in a PHP approach instead of doing an isolation similar to Enums. String comparison is often a flimsy way of building application-central logic upon, but you don’t really have simple alternatives in PHP 7.
Pair that with Generics and parameter overloads and you can write some really pretty code.
Consistency
Native PHP methods are not consistent. Even after all these years, I never seem to remember them perfectly when using array_filter($input, $callback)
versus array_map($callback, $input)
or strpos($haystack, $needle)
versus array_search($needle, $haystack)
.
You shouldn’t have to remember all these quirks when programming. They distract you from your application’s code. There are none of these variations within the .NET Framework in C#.
Oh, I’m not kidding myself, I’m sure there are consistency issues in .NET Framework as well, but at least you have to dig a little more to find them.
The type methods are also correctly packaged under a meaningful group. I guess it helped to have access to namespaces all along. No need to prefix global methods with array_
when the variable’s type infers it. This cleans up the language API quite a bit.
Better native objects, like List<>
In comparison, I also think there are missing core object types PHP. Arrays are used to represent all types of things, but there are not semantically meaningful.
As an example, Laravel and Symfony return custom-made collection objects around database query results to allow re-filtering and ease any further data management. You may argue that these list types with value-added should have been made available by now by default in PHP, even without type enforcement inside the object.
We’d likely see a performance gain if, for instance, Laravel’s implementation of collect()
was running in C rather than in the interpreted PHP version.
There are multiple kinds of enumeration types in C#. Additionally, the IEnumerable
interface grants the use of Linq on these lists. It’s a descriptive way of doing operations on list objects that I think help express developer intent as opposed to a foreach
loop on an Array.
This snippet from the official Linq documentation performs two sorts one after the other.
The tools
Not many changes for me
I know I would rather use Linux servers over Windows and use Postgres or MySql over Microsoft’s SQL Server.
Unlike the previous Windows-only version of the .NET Framework (ASPNET), Core can be configured to connect to anything and runs on most OSes. It is in fact encouraged by how easy it is to configure how to start up a project.
That’s an important factor because I am not interested in changing everything I do.
IDEs
PHPStorm and a correctly configured Visual Studio Code can act as a good IDEs for developing PHP. You get decent code completion in most cases. You can quickly see it never gets completely integrated though.
You need a helper library and custom code changes in your application to get autocompletion in a Laravel project. It doesn’t feel right. Shouldn’t my editor be able to figure out a lot more by itself now that we have been sending robots on Mars for a while now?
I would think that not having to pre-compile all your code prevents the editor from finding out all the project’s namespaces and classes as Roselin does for C#. Compiling probably grants better code reflexion for the editor and that is harder to crawl in PHP.
Though I still enjoy coding in Visual Studio Code better (because of window management and shortcuts mainly), Visual Studio proper is a beast of an IDE. The refactors it can do and the things it knows about your application is downright impressive when you have not been exposed to that beforehand.
The C# debugger is also very powerful, allowing you to jump back an forth in execution unlike anything I have experienced in XDebug.
Not everyone’s happy
I work with a cranky developer that has been using the classic .NET Framework implementation throughout all his career. He knows historical facts and has been through many language debates over the years.
He’s quite disappointed with how the .NET Core implementation has begun and mentions it absolutely does not compare with the Windows’ only version of .NET Framework.
Apart from how the project was managed at the beginning, his main gripes seem to revolve around having to explicitly configure how things talk to each other in Core. IIS and the .NET Framework seem to have been really tightly integrated inthe past and the lines where they talked to each other seem to blur when I listen to him describe it. There’s none of that plug and play architecture in the cross platform .NET Core implementation.