Menu

C# and top-level statements

March 31, 2022 - C#, Programming

C# 9 introduced a new language feature. It was called “Top-level statements”. Basically, it allows you to write a program eschewing all the boilerplate stuff like a class and a main routine, and just directly write code in the source file and execute it.

I think this is a terrible feature. Making it default? Even worse. Frankly, it has no place in a language in 1998 let alone 2022. It’s one of the worst and most ill-thought features ever added to C#, in my opinion.

What is a top-level statement, Anyway?

The feature is called “top-level statements”. But what is a top-level statement, anyway? Now, for the most part, supporters praise the feature as being better for beginners; often referencing how it makes C# more like BASIC, a language designed for beginners. However, this incorrectly, I feel, assumes BASIC has top-level statements as part of it’s design as a beginner’s language, but the most likely reason is simply because, well, that’s pretty much how all programming languages worked, and, particularly, the biggest professional languages- COBOL and FORTRAN, worked that way.

And why did COBOL and FORTRAN work that way? Because they were designed for punch cards.

So “Top level statements” is really “Punch-card format”. I’m going to start calling it that conversationally because it will really annoy the people who think top-level statements in C# are a great new feature.

Anachronisms of a forgotten past

So Top-level statements are fundamentally an ancient “language feature”, largely an artifact of the original media used to program computers than any inherent design based on it’s beneficial traits from the perspective of a programmer. So the question becomes, why was such an anachronistic feature added to C# in 2020 with the introduction of C# 9? It’s difficult, of course, to know specifics why, but I can guess.

The feature, in official descriptions of it’s initiatives, as being intended partly for “beginning” programming and to allow for creating simple programs more easily. This suggests the designers of the feature may be looking back on the “punch-card era” with some rose-tinted goggles; I’ve seen comparisons drawn by some veterans about how easy it was to start programming BASIC when they were 7 on say a commodore 64, and therefore to work better for beginners, C# should emulate that style. This is as I described previously something of a misapprehension, I think, about that design to BASIC; BASIC doesn’t work that way because it is better for beginners but simply because it’s basically how all programming languages worked at the time. Emulating that behaviour now is not better for beginners simply because programming languages seldom work that way now.

If I’ve never seen it, it must be new!

Newer developers- those who have no experience with the aforementioned “punch-card style” languages, see this feature as brand new. It’s absent from almost every major programming language, making the feature seem completely new and even unique. The positioning as being better for beginners seems accurate from that perspective because it “removes the annoying boilerplate”, allowing programmers to learn by writing code instead of being told to “ignore this for now”.

I’m not convinced that makes it better for beginner programmers, though. the C# language feature is “syntax sugar” in the sense that it basically compiles to an implied main routine anyway, since one is in fact required. This differs from those older language designs because in those the “top level statements” were a first-class, required design. The use of entry-points was actually mimicked by creating a Main() sub or function and calling it at the top-level for a number of years, to adhere to the newer programming style which eschewed the relics of punch card programming for the Entry-point concept that was pushed by the new structured programming style.

Of course, I’m not a beginner anymore so it’s difficult to judge whether this does or does not actually make the language easier to learn. Thing is, that would seem to apply to all the people involved in it’s conception and implementation. It seems there is a good chance this is one of those cases where experienced developers make somewhat uncharitable assumptions about beginner programmers, and then implement features to allow those imagined beginner programmers to learn more easily. It’s the skill equivalent of the Arrested Development joke about “How much could a banana cost, $10?”. We are rich in skill and experience enough that we no longer have any idea what actually helps a developer learn. This feature feels like some older developers thought back to how they learned programming, recalled things like C64 BASIC, and decided that “top-level statements” were the language feature that allowed them to learn programming and wanted to add that to C#. Younger devs typically love to follow and adapt to “new stuff” without thinking too much about whether it is actually a good feature, which is probably why some of the counter-criticism I have received due to disliking this feature is of the classic form “You just don’t like it because it’s new and different!”, even though the reason I dislike it is because it is neither.

Have something to say about this post? Comment!