Top-Level Code Prototype: Scenario A

While most of my prototypes allow you to write more (new) kinds of code this prototype is all about writing less of what’s already in the language. The idea is simple:

  • You should be able to write a complete VB.NET program with exactly ONE line of code; no namespace/class/method declarations.
  • You should be able to write a complete VB.NET program that’s just a sequence of executable statements in a single file, if that’s all you need.
  • You should be able to declare methods and classes as needed in these simple programs in the same files without giving up the lightweight/low-ceremony syntax.
  • This syntax should integrate seamlessly with the rest of the VB.NET language rather than being an alternate dialect/project type/compilation-mode.

Here’s a quick 3 and a half-minute video showing off those ideas in action:

There are benefits for VB coders at every level of experience from first-time programmers to veteran developers:

Simple programs for learning concepts and trying out ideas

A program that’s only one statement is not usually* very useful outside of the fact that for a newcomer to the language to conveys in the most succinct way the smallest (often fundamental) useful thing you can do in that language. This is why “Hello, World!” programs matter. 20 years ago when I discovered QBasic on my Windows 95 upgrade CD I didn’t know how to do any programming at all. Then I read a tutorial and in one line I knew how to display output to the user (myself). Before that line I was not a programmer and couldn’t make a computer do anything that wasn’t already installed and after that line I was a programmer because I could make my computer do that one thing.

I didn’t know anything else but I knew that one thing. And then I learned a second thing, and a third thing, but the journey of a million miles that led me to being a language designer at Microsoft started with one step and ONE concept. Not “This is an import statement, let me explain what a namespace is, here’s a class/module, don’t worry about it, oh yeah, subs, can only write code that runs in here, entry points, gotta be called Main, now let me tell you about Console.WriteLine”.

VB lost that simplicity in the jump from QuickBasic, though it has its own flavor of “Hello, World” in the Command_Click handler demo but for learning language/programming fundamentals outside of GUIs the simplicity of a bunch of statements in a single file can’t be beaten. I can’t tell you the dozens of console applications I spin up and throw away during the course of working on a project to try out ideas, approaches, algorithms, etc.

Scripts (maybe?)

And by this I don’t mean scripting engines (a la VBA) but scripts a la VBScript, PowerShell, Batch files, etc. Even if you’re not learning but just automating some task it’s convenient to be able to do so with as little ceremony as possible. But if the language supports such simple programs natively it could be as straightforward as compile-and-run. It doesn’t even have to be interpreted.

I have little VB.NET programs I’ve written over the years for all sorts of tedious little chores on my computer like re-naming and/or re-tagging all my newly downloaded MP3s that could just as easily be ‘scripts’.

There are upsides, downsides, and concerns to this, of course (like security), but clearly shell scripting languages haven’t stopped being a thing so I’d love to hear your thoughts on whether you’d prefer scripting in VB.NET to PowerShell or Bash.

Better documentation and tools for everyone

Whenever I write a blog post or an article or documentation for something there’s always a tension between writing “all the code you need to copy, paste, & run this as a complete example” and “just the code I’m talking about”. If you look at my “Exhausting List of Differences” post you’ll see that I usually opted for the former. A lot of those code snippets have Module Program : Sub Main : End Sub : End Module boiler plate in them because I wanted it to be fairly easy (and consistent) to just paste them in your IDE and go. It’s less of a win if it’s just a few statements to drop in your Main method but once you start having helper functions you’re calling or data classes you’re referencing it becomes a hassle to “copy this part at the class level, and this part inside the Main method” and with a feature like this all of that goes away in my documentation.

If you look at the docs for VB String Interpolation, for example, you can see the doc writers balancing these same concerns.

And to take it to the next step you have a tool like try.dot.net which aims to let you run code right on a documentation web page. Other great examples include sharplab.io (previously known as TryRoslyn), .NET Fiddle, and more.

I’ve seen the same issue multiple times throughout the years. I’ve seen more than one developer set out to make a IRC bot that will run some VB code but on the back end they have to do some transformation to make the code legally compile-able. That interaction becomes much simpler if the language itself naturally supports simple programs. A feature like this removes that entire step if you can literally hand those statements wholesale to the compiler like a black box and just run its output and send back the result (modulo all the security issues with doing that).

Summary

Top-level executable statements and declarations in the vanilla VB.NET language is great for documentation, education, and sets up an easier play for things like shell scripting and tools like Try .NET, SharpLab, .NET Fiddle, and the like.

This is Scenario A, it’s 1 of 5 that I’ve worked out. That’s right there are 4 more demo/posts on this one prototype and they’re different from this one so you’re going to want to pay attention.

I’m trying a different approach with this prototype than in the past because my first video was over 15 minutes and my last video was over 20 minutes long and I think it’s better both for you all and for me to break these prototype-focused posts up. As my friend Jon might say, “Ain’t nobody got time to watch a 30-minute video someone shared on Twitter” and the whole reason I make these videos is so they can be shared and viewed. It also lets me post a little more frequently and longer on a single topic without “going dark” as long to work on the next big project/video and requires a lot less editing.

Let me know your thoughts on this approach and of course on the feature itself. And, as always, Please Share!

Regards,

-ADG

6 thoughts on “Top-Level Code Prototype: Scenario A

  1. This is brilliant, and much needed. I’ve often turned to LINQPad to write similar simple apps, as LINQPad also implicitly wraps the statements up in a class and method. However, unlike LINQPad, I could compile this to an executable for sharing.

    It’s also a fantastic way to introduce beginners to programming.

    Like

  2. Pingback: Top-Level Code Prototype: Scenario B | Anthony's blog

  3. Wow~ ⊙o⊙
    I’ll definitely upgrade all of my frequency modified VBScripts to VB.Net once this feature is available!
    This feature will be more powerful if it supports the #R preprocessor to reference assemblies and even Nuget packages.
    #R “PresentationCore.dll” ‘ Reference dll from GAC or %PATH%
    #R Nuget “System.Memory” ‘ Reference nuget package with latest stable version and default feed
    #R Nuget “System.IO.Compression” Version >= “4.3.0” ‘ Reference nuget package with version range
    #R Nuget “System.Text.Encoding.CodePages” Preview Version >= “4.4.0” ‘ Reference nuget package with version range (Include preview versions)
    #R Nuget “System.Runtime.CompilerServices.Unsafe” Version “4.5.0” ‘ Reference nuget package with fixed version
    #R Nuget “Microsoft.ML.Core” Preview Feed “https://dotnet.myget.org/F/dotnet-core/api/v3/index.json” ‘ Reference nuget package with the latest preview version and custom feed

    Like

  4. Pingback: Top-Level Code Prototype: Scenario D | Anthony's blog

  5. There is a great scenario for Top-Level Code Prototype. It can achive your block expressions like this:
    “`vb.net
    Dim timeOfWeek =
    “`

    You just declare implicit function for the block inside the “ and invoke it!

    This can also achieve many more purposes, like embedding code in interpolated strings:
    “`vb.net
    Dim Msg= $” time Of Week is {}”
    “`

    And in XML literals:
    “`
    Dim x =

    <(For i=1 to 10
    @i
    Next)>

    “`
    More details: https://github.com/dotnet/vblang/issues/422

    Like

Comments are closed.