5 Things I Hate about my Favorite Programming Language: C#
October 26, 2010 1 Comment
In episode 73 of the StackOverflow podcast , Jeff Atwood mentions one of his favorite questions to ask developers, “What are 5 things that you hate about your favorite programming language?”
It got me thinking. I definitely like some of the language features of C#, especially when developing within Visual Studio. Some of my favorites are Generics, Intellisense, and Short-Circuited conditionals. However, there are other pains that I encounter repeatedly when it comes to language syntax.
- In C#, there is the nifty syntax of following a variable with “??” allowing the developer to specify a replacement value in the case of the variable being null. In theory this is a great feature. However, I never end up getting to use this and I imagine that is the case for most developers. I find I usually end up using the “?” and “:” syntax because I typically want to use a member of the null object like “user.Identity.ToString()” but I have to check if “user” is null or I will get an object reference error.
- Why do I always have to lookup how to format a DateTime object in code without the minutes?
- If “ToString()” is a member of every object, why should I have to explicitly call it when setting the value of a string variable from another non-string variable? As an example, why can’t a string value be inferred from my int variable by just calling “ToString()” when there would otherwise be a type issue?
int i = 0;
string j = String.Empty;
j = i;
- The compiler considers the use of “=” valid within an if condition. This can be confusing and often causes accidental issues where “==” is the intended code. As a solution, I use a coding standard of typing the constant value to be compared first because it cannot be assigned to (i.e. “if (0 == count)”).
- Nullable types seem like a great idea but they are not always intuitive. In the example below, the code seems like it should work. However, I receive a compiler error on the second line.
int? i = null;
i = (sender != null) ? sender.GetHashCode() : null;
Most of the above complaints were compiled off the top of my head. If you have found a better way to leverage the C# language so that you do not run into these issues, please share them with me in the comments. Perhaps it is my mis-use of the language features that has caused my agony.
Curiously, that’s my favorite question, and maybe Jeff saw it in my Stackoverflow post from 2008. I actually started thinking about this when I was writing Mastering Perl in 2005, and it shows up in the introduction for that book. The trick from jumping from apprentice to master requires that level of thinking. I also mentioned it on use.Perl, in response to someone trying to push Ruby onto me, as I recall.