Few days back Aleš Roubíček wrote an interesting article about, basically, needless code. You can read it here, though in Czech.
In one part he points to the coalescing operator
?? as being helpful while trying to write succinct code. And I agree. But also very often I need to not only coalesce the value itself, but, because we live with objects, also access some member on the object itself. Small example.
var d = DateTimeOrNull();
x = ((DateTime)d).Minute;
x = null;
This pattern I find having in my code very often. But it’s hard to write it succinctly. Yes, I can use ternary operator
?:, but I still need to use intermediate variable. Boring. Having a language construct for this case would be interesting. Actually I wrote myself simple extension method for that, but that’s not the same as having it baked into the language.
I can imagine something like this (just shooting some syntax).
int? x = DateTimeOrNull() ? x => x.Minute ?? null;
Or am I only one thinking/bothering about this?
Last week I was doing fairly simple change in a project I’m currently working on daily. Added a controller, model, few DTOs and orchestrated all this together using some methods from “library”. I had clean focus what to do and some pieces of new methods I already had in existing methods. So I partly also did small refactoring. While typing the code I noticed the red squiggles etc. are behaving weird already. When I was done with change I invoked build process and suddenly everything went wrong.
quickly followed by
Well, let’s try some basic healing steps. Restarting Visual Studio (2012); no luck. Deleting all
obj etc. and restarting Visual Studio; no luck either. OK, time to use big guns. Restarting complete machine; no luck.
Looking at the detailed build log I found (among tens of other errors below):
CSC : error CS0583: Internal Compiler Error (0xc0000005 at address 00FE7AFB): likely culprit is 'BIND'
Little bit search on internet didn’t brought anything helpful. So I decided to reproduce it. If it was working five minutes ago, it’s not going to be hard to pinpoint what caused it. So I created copy of project’s current state and reverted back. Then I started slowly adding pieces back and/or copying new files back. Surprisingly (and luckily) the problem went away.
Probably some bad coincidence, order of files, …, who knows. Thus if you face same or seemingly same error, try to redo your steps from working state. It’s probably going to work. And if not, report it, you have a reproducible test case, what’s better, isn’t it?