Featured image of post Transitioning from PHP to C#

Transitioning from PHP to C#

My love-hate relationship with C# as a PHP developer.

As I explore new technologies, I got a job that deals with C# instead of my usual arsenal of PHP and JavaScript.

Yes, some programmers would find it disgusting that my two main languages are PHP and JavaScript. People complain that PHP is horrible but base their complaints on old versions. Let me tell you something, this ain’t the PHP you used to know. He grew up and moved on.

When you say that PHP and JavaScript are horrible languages, that’s your right. You have the right to be wrong. That doesn’t make you right, just arrogantly judgemental.

But we’re not here to discuss that, that’s for another blog post. We’re here to discuss my transition from being a PHP Software Engineer to a C#-based one.

TL;DR - I’m hating the experience but I look forward to the knowledge.

Disclaimer: I know that there may be other ways to do the things I’m ranting about below, and I’ll probably appreciate it more when I get more used to things. I’m new to C#, cut me some slack.

C# is such a diva

I guess this just comes from my roots as a PHP dev, a dynamically-typed language by nature. But come on:

List<int> myNumbers = new List<int>();

Apparently, var myNumbers = new List<int>(); also works. That’s cool, I guess. I just find it funny that there’s a way to declare a variable where you need to declare the data types twice.

Strong typing is great, and I’m glad C# has the second way as an option. It’s just so weird to me to even see the first one.

Creating JSON was confusing

There’s the JsonSerializer class, then there’s also Json.NET by Newtonsoft. Initially, I didn’t know about JsonSerializer, most of the tutorials were using Json.NET, which I guess has it’s advantages:

  1. LINQ to JSON
  2. The JsonSerializer for quickly converting your .NET objects to JSON and back again
  3. Json.NET can optionally produce well formatted, indented JSON for debugging or display
  4. Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
  5. Ability to convert JSON to and from XML
  6. Supports multiple platforms: .NET, Silverlight and the Compact Framework

The list above was taken from this StackOverflow answer, which I find weird. It feels like for JSON to be a first-class citizen (which it should be in modern apps), you need a third-party plugin.

In PHP, it’s as simple as:

<?php

$arr = ['key' => 'value'];
$json = json_encode($arr);

I’m sorry if I’m comparing everything to PHP. It’s like my native dialect, so it’ll be my point of reference for a lot of these.

URLs are ugly as f—

This is the most annoying pet peeve I have with C#/MVC 5/.NET Core. URLs are not in lowercase form and it depends on your method name.

Imagine having a URL like http://mydomain.com/Videos/AddVideo instead of a proper REST-compliant URL like http://mydomain.com/videos (via POST).

Yes, I know you can change this in RouteConfig. However, this being the default makes people think it’s okay to just make URLs like that. It’s not. It’s annoying to the eyes.

Using LINQ is both a blessing and a curse

I still don’t know how I feel about LINQ. On one hand, it makes queries easy. On the other hand:

var query = from person in people
    join pet in pets on person equals pet.Owner into gj
    from subpet in gj.DefaultIfEmpty()
    select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };

Apparently, that’s how you do left outer joins. To a C# beginner like me, it’s confusing because it’s SQL-like but it references objects from within C#.

In Laravel, this exact query looks like:

$query = People::leftJoin('pets', 'people.id', '=', 'pet.owner')
    ->select('person.first_name, pet.name as pet_name')
    ->get();

Would you look at that: I could read it even if I knew nothing about PHP/Laravel.

Visual Studio is… well, Visual Studio

For an IDE that’s not written in Java it sure is sluggish as hell.

However, I really like the debugging tools, so I guess there’s not much to complain about here.

I would appreciate it though, if Visual Studio didn’t randomly complain about something only to be fixed by restarting Visual Studio.

What I like about C#

It’s not all bad. I do like a lot of things, and I’ve even learned a few things.

I really do appreciate a lot of the other stuff, like:

  • Strong typing - as having handled both backend and frontend projects, I hated the times where PHP throws either a string or a null type and JavaScript complains because it expects something different.
  • Compiled - a compiled language will almost always outperform a scripting language. Granted, improvements to PHP’s engine and process management makes it catch up but there’s nothing like a good ol' compiled software running there as a daemon to get things going.
  • Respected - I don’t know why people still hate PHP, but I’m glad I can add another language to my resume where I won’t be judged prematurely just because I code in PHP

With those being said, I still go back to PHP every now and then, especially when working with Linux-based microservices. A project I finish in a week using C# is a project I’ll probably be finished with in 3 days with PHP.

Licensed under CC BY-NC-ND 4.0
Last updated on October 18, 2020 11:33 +0800
comments powered by Disqus