Why VB is Best

As I said in my previous post, I believe that Visual Basic is the best language for many types of applications. It provides all the power of C# and Java, but has arguably better tools and syntax. For now, I’ll concentrate on the positive, but in a later post I’ll discuss what I feel are VB’s shortcomings, and my suggestions for addressing them.

For me, the most valuable VB language features are its Readable English Keywords, Line-Orientation, and Case-Insensitivity. The ability to quickly create GUI applications using drag-and-drop may have been the original selling point, but to me what makes VB great is the language. It’s currently my preferred tool for working on personal projects.

Readable English Keywords

For the most part, VB is a very easy language to read, and the keywords chosen come closer to conveying the correct meaning than with other languages.

Many languages have a long list of special symbols that the user is required to memorize before the language is readable. Often the same symbol will mean completely different things depending on the context. While VB has the commonly used Math symbols, and even a separate concatenation symbol, overall its philosophy is to favor the use of keywords to help the user to understand the code. (As you’ll see later, I even think some of the current symbols should be removed.)

The best VB keywords are those that help the reader to understand what’s going on without having to be fluent in VB or any other OO language. This helps beginners to learn the concepts, and also helps experienced programmers write more maintainable code.

Examples

Inherits, NotInheritable, MustInherit

The VB syntax for object derivation is very clear and explicit as long as you’re familiar with the concept of inheritance in object oriented languages.

Class Foo 
  Inherits Bar 
End Class

Furthermore the NotInheritable keyword is clearly related, and has the obvious meaning.

NotInheritable Class Bar 
End Class

This would cause the Foo class above to have a compilation error.

The MustInherit keyword is also clearly related to the other two.

MustInherit Class Bar 
End Class

Even someone new to the language should be able to guess that this prevents the Bar class from being instantiated on its own.

If you contrast the above with the corresponding concepts in Java (extends, final, and abstract) and C# (‘:’, sealed, abstract), it should be clear that the VB keywords are superior. Although Extends, NotExtendable, and MustExtend would work almost as well, the term “inheritance” for the whole concept is usually used even in Java circles.

Overrides, Overridable, MustOverride, NotOverridable

Similarly, the concept of “virtual” is overused in computer science, and therefore a poor choice for describing class methods that can be overridden in derived classes. The VB keywords have a more obvious meaning, and may even make OO polymorphism easier to grasp for beginners.

The equivalent Java ( [virtual], virtual, abstract, and final), and C# (overrides, virtual, abstract, and sealed) are not as clear and don’t work as well together.

Shared, Static

VB uses the keyword Shared for methods and fields that apply at the class level. The C#, Java, C++ keyword static seems a little less clear to me, and gets muddled up with the separate concept for local data within a function that lives past the lifetime of the function. All four languages use static to describe this concept.

Class Foo

Public Shared Function CreateFoo() As Foo

End Function

Private Sub Bar

Static called As Integer = 0

called += 1

End Sub

End Class

And, Or, Not

Using these simple keywords instead of the common &&, !, and || is more approachable and actually easier to type, because the latter require shift-key combinations. The C-style symbols are also fairly arbitrary and make C-style languages just a little harder to learn and use.

ByRef, ByVal

Like C#, these do require you to understand the concepts of passing by value versus passing by reference, but I find the VB keyword a little clearer. Allowing the programmer to be explicit about ByVal also enhances readability, because I no longer have to remember which is the default. (It’s ByVal.)

Do, While, Until, Loop

I find the VB loop syntax to be flexible enough for any purpose, and much more intuitive than Java, C#, or C++. All the following are allowed.

Do Until x 
Loop 
Do While x 
Loop 
Do 
Loop Until x 
Do 
Loop While x

You can also Continue or Exit a loop explicitly.

For, Each, In

I think the VB iteration syntax is nearly perfect.

For Each x In y

is not so different from C#:

foreach (x in iterable)

but the required parentheses seem to break up the statement in a weird place.

Java is worse, because it uses an arbitrary ‘:’ symbol that you have to train yourself to read as “in”.

for (x : iterable)
For, To, Step

The counted loop is also fairly simple, and I think much easier to read than C#, Java, C++ equivalents.

For c = 0 To 9 
For n = 1 To 20 Step 2

as compared to :

for (c = 0; c < 10; ++c) 
for (n = 1; n <= 20; n += 2)
Summary

The above is certainly not an exhaustive list of the well-named features in VB, and I certainly don’t imply that all VB keywords have good names. (Later, I’ll explore in more detail just which keywords I think should be renamed, and I even advocate removing almost 90 keywords.) However, overall I find most of the VB keywords to be more intuitive than alternative languages, especially for those who don’t have preconceived notions of what a concept should be named. (e.g. virtual) This makes the language more approachable for beginners, but also easier for experienced programmers.

Line Oriented

Another major feature of VB is its preference for having one statement per line. This is a common practice by many programmers in Java, C#, and C++ as well, but VB has features to encourage the practice.

No Semi-colon necessary

In C-based languages you are required to put a semi-colon at the end of each statement, and an end-of-line has no special meaning. This allows a single statement to span multiple lines, and multiple statements to reside on a single line. I find that both of these practices tend to make code difficult to read. Tools such as Eclipse/Java have built-in code formatters to ensure that each statement resides on a single line, and to ensure that statements that span multiple lines have a consistent format.

In VB, the default is for each statement to be terminated at the end of the line unless an underscore ‘_’ is placed after the statement, allowing a single statement to span multiple lines. VB also allows a colon ‘:’ between statements to force multiple statements on one line. As you’ll see later, I’d actually like these exceptions removed to force the programmer to find a more readable line-oriented style.

Assignment Side Effects

In VB, assignment is a statement, not an expression, and has no return value.

In C++ terms, the VB assignment operator is written like this:

void operator=(T lhs, T rhs);

If I have an example, java program like this:

if (x == 1000) { 
  x = y = z = 0; 
}

Then in VB, I must write it as:

If x = 1000 Then 
  x = 0 
  y = 0 
  z = 0 
End If

Alternatively, I could use the colon:

If x = 1000 Then 
  x = 0: y = 0: z = 0 
End If

One other side-effect is that VB is able to disambiguate assignment from equality checking. The common problem of typing “if (x = y)” instead of “if (x == y)” in C-based languages is eliminated, because “If x = y Then” always refers to equality checking and “x = y” always refers to assignment. (Later, I discuss why I think VB should automatically display a different operator for assignment.)

Syntax Validation

When you press the Enter key, VB knows that the statement is likely complete, and usually does syntax validation as well as possible automatic syntax cleanup. For example, if you type the first line of a multi-line expression such as “If x Then”, VB will automatically insert a closing “End If”, and put the insertion point at the correct spot, formatting your code as necessary. In practice this makes it feel as if VB is really aware of what the programmer is trying to do, and the IDE is an active partner in code construction. Other tools have some of these features, but I’ve never seen another tool be as unintrusive and natural about it as VB, although with VB.NET some things aren’t quite as smooth as in the past, which I’ll discuss later.

Case Insensitive

It’s pretty well known that VB is a case-insensitive language, but some may not realize just how much of a benefit this is to the programmer.

Faster Typing

Despite its propensity for fairly verbose keywords, I’ve always found that actually typing in vb programs is often faster than with other languages. This is partly because you rarely need to use two-key combinations. The IDE will automatically capitalize symbols to match the declaration. My educated guess is that VB will be 10-20% faster to type than an equivalent language like C# or Java. The key is to allow the IDE to share the load, and when switching to VB from another language I usually need some time to train myself not to capitalize symbols, add unnecessary parentheses, and to use caps-lock effectively.

Here’s an example. Type in:

mustinherit class Foo<Enter>public mustoverride function Count as integer<Enter><Delete Line><End><Enter>class Bar<Enter>inherits foo<Enter><Down><Down>return 0<Down>

And what you will see is:

MustInherit Class Foo 
  Public MustOverride Function Count() As Integer 
End Class 

Class Bar 
  Inherits Foo 
  Public Overrides Function Count() As Integer 
    Return 0 
  End Function 
End Class

Only 3 two-key combinations are required for the above in VB. (The initial declarations of Foo, Count, and Bar.) For comparison, the same code requires at least 16 two-key combinations in C#. Furthermore, the VB code doesn’t require cleanup for formatting and indentation, making it even faster to write equivalent code.

Better Naming

By disallowing multiple symbols with names differing only by case, VB forces you to pick less ambiguous names. This leads to better readability than common code in other languages. For example, I’ve often seen code like the following:

Foo foo = new Foo();

If you read this aloud then both “foo” symbols sound exactly alike (which is the definition of less-readable).

To make the best use of VB, you should also pick a naming convention that works well. For example, by always prepending or appending an underscore to private fields, you are increasing the number of two-key combinations required. A better convention is to prepend “my”  or "m" to private fields, and to use camel-case for all other names.

Other

Concatenation Operator

Using the same operator for addition and concatenation leads to less readable code. By having a separate concatenation operator, the following code can compile.

Dim s As String = 123 & "456"

Note: If the + operator were used instead, then this would be a compile error.

Optional Duck Typing

VB supports optional relaxing of type rules. When “Option Strict Off” is specified for a file, then you no longer have to explicitly specify the type when declaring variables. VB will also automatically convert types. For example:

Console.WriteLine(123 + “456”) 

will print “579”, not “123456”.

VB will also allow access to any public member of a type as long as it can be found at runtime. This is what’s known in Python as “Duck Typing”, because "If it walks like a duck and quacks like a duck, it must be a duck". VB is the only language I know which allows selectively enabling this feature.

Optional Explicit Declaration

Completely separate from the question of type safety is whether to require variable declarations. Even when duck typing is desired, often it’s nice to still enforce variable declaration. This can eliminate some common typos, but can be selectively disabled just as with type safety.

Declarative Events

VB supports the same events and delegates as C#, but also provides the ability to specify event handlers in the signature of a method.

Class Foo 
  WithEvents myCon As Bar.Connection 
  
  Sub OnConnected() Handles myCon.ConnectedEvent 
  End Sub 
End Class

In this example, the OnConnected method is automatically “wired-up” to the myCon Connection object. This can often be more readable than the alternatives.

Reference Parameters

Occasionally it’s useful to be able to pass arguments by reference. This is supported by most (all?) .NET languages.

Sub ChangeTwoInts(ByRef n As Int32, ByRef m As Int32) 
  n += 1 
  m += 2 
End Sub
Named Parameters

VB allows specifying explicit names for parameters. One place this can be useful is when calling a function that take a boolean.

Public Sub DoSomething(ByVal isFinished As Boolean) 
End Sub 
b.DoSomething(isFinished:=True)

This makes the code much more readable, and eliminates the need for a comment or temporary variable to clarify the code.

Optional Parameters

VB also allows optional parameters, although using this feature for public methods is discouraged because doing so makes the code non-portable. For example, C# doesn’t support code with optional parameters. Still, optional parameters can eliminate code duplication that would be required with method overloading, and shouldn’t be avoided for internal functionality or when portability isn’t important.

Parameter Arrays

VB supports creating methods that take 0 or more optional arguments of a single type. For example:

Public Sub Foo(ByVal ParamArray args() As String)

This function accepts Foo(), Foo(“a”), Foo(“a”, “b”), etc.

Powerful Select

The VB Select statement is similar to the switch statement provided in C-like languages, except that it’s more flexible.

Here are examples that use the advanced VB features.

Select Case age 
Case 0, 5, 10 
  Console.WriteLine("0,5,10") 
Case Is > 50 
  Console.WriteLine(">50") 
Case Is < 10 
  Console.WriteLine("<10") 
Case 16 To 19, Is > 30 
  Console.WriteLine("16-19 or > 30") 
Case Else 
  Console.WriteLine("else") 
End Select 

Dim name As String = "Justin" 
Select Case name 
Case "Abe" To "Barney" 
  Console.WriteLine("a-b") 
Case "Boris" To "Kevin" 
  Console.WriteLine("b-k") 
Case "Kevin" To "Vincent" 
  Console.WriteLine("k-v") 
Case Else 
  Console.WriteLine("else") 
End Select
Full Featured Exception Catching

The .NET framework supports an optional filter for catch statements. VB exposes this functionality allowing you to only catch exceptions when some criteria is met. For example:

Try 
  ' do something 
Catch ex As Exception When LoggingIsEnabled 
  Log(ex) 
End Try
Refactoring

VB has some basic Refactoring support built in, and a license for Refactor! which provides even more. I’m generally not a big fan of refactoring tools, but the VB stuff is pretty intuitive and unintrusive. The important ability to rename things is what I use most.

Powerful Imports

VB can use the Import statement for more than just namespaces. For example, if I include “Imports System.Console” at the top of a file, then I can call “WriteLine()” directly without having to specify the Console class.

Summary

Despite the length of the above list, I’m sure I’ve forgotten some features, but maybe it will entice you to download the free Express version and try it yourself. Overall I think that VB is the best language for most programming, and it should get even better in the future. The next version will likely introduce Type Inference, Lambda Expressions, Closures, LINQ, and other enhancements. In my next post I’ll discuss a large list of new features and changes that I personally would like to see in a future version of VB.

Advertisements
This entry was posted in Programming Languages. Bookmark the permalink.

2 Responses to Why VB is Best

  1. The Best says:

    YES!
    you are right VB is the best of the best programming language in the world…

  2. Vina Rue says:

    Great Job brother, I hope C# programmers read and understand these issues.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s