Well we have been playing around with VB.NET 2005/2008 for awhile and even a few additions created with some service packs and it is time for something new to talk about. What is in store for the future and is Microsoft really listening to the developers in the industry? Throughout this entry of the Programming Underground we will talk about some of the features proposed for VB.NET 10 and what it could mean for the development of software as we know it. Keep in mind that not all these features here are going to absolutely make it into the next version (or everything that will be new in version 10) but it is our job as programmers to always look a few steps ahead to what is coming and how it might work. So lets gaze into the crystal ball and see what is up with VB.NET here on the Programming Underground!
Throughout this entry I will be talking about a few features proposed in the document that can be found at VB Future – New Features in Visual Basic 10 Document. I am not going to go through them all, but I will pick a few that I think are interesting and if I see any potential problems I will give you the heads up.
1) First up, lambdas. Lambdas were a new feature brought in with the idea of LINQ (in a big way) with the introduction of Visual Studio 2008. The idea is that lambdas would act as an anonymous function and can take parameters as well as contain expressions and statements. Lambdas can be used to build delegates and expression trees. LINQ uses them extensively with the idea of filtering and querying collections of data. In the new version coming up they are expanding on the idea to include single line, multi-line lambdas as well as single line sub lambdas. What Microsoft appears to be doing here is really expanding the idea of an anonymous function in other languages like Java. I think it is a great idea myself and something that should have been available a long time ago.
With the idea of having sub lambdas (lambdas which are sub procedures instead of functions) programmers no longer have to specify a return value. They can simply create lambdas that could be a simple one line iterative loop for arrays or collections. Where each element in array or collection only needs to be printed or outputted. Why build a 3 piece sub procedure for a simple iteration? But what is with these examples spread across multiple lines with no continuation character? Our next topic…
2) The multi-line continuation underscore is becoming extinct! HURRAY!!! Ever code in PHP or an interpreted language that ignores whitespace? Like the freedom of writing your code across multiple lines without having to worry about some kind of continuation character? This feature is slowly becoming a reality.
<Attribute()> Function Go( ByVal x As Integer, ByVal y As Integer, ByVal z As Integer ) Dim query = From n In { 123, 456, 789 } Order By n Select n + x End Function
Now of course the example provided above (taken from the new features document) is a bit extreme but notice that now we will be able to split code across multiple lines. This has the advantage of making code more readable, but this can be a HUGE mistake by Microsoft as well. Sure other languages like PHP can split code across lines without a problem, but often times it is abused in PHP as well. The idea here is to make things readable, and from the example above, you can see it is a bit disorienting at first for such a simple function.
Thinking from the stand point of a DIC helper in the VB.NET forum, I can already tell you we are going to have a lot of people asking about code that has been split across multiple lines probably at a time when it shouldn’t have been. Plus this is a formatting privilege, not a right! Where people are given an inch they love to take a mile and surely we will be seeing code that has been “lined up to look pretty” when in fact it is not going to be pretty to anyone but the programmer coding it.
But in either case, for professional programmers I am sure it will be a well received ability. I know myself that I have hated to keep track of line continuations… even though I use very few of them since I keep my functions small and compact. Line continuation hasn’t been completely removed mind you, but the document I listed at the beginning of this entry shows you all the places where it is considered “implicit” in the new version. Enjoy it!
3) Auto-implemented Properties. You know about properties right? Creating “getters” and “setters” to set internal values of a class. Well in the new version they are going to be squished down into single line statements for the basic generic properties. What do I mean by generic? I am talking about those properties which really don’t need too much validation and pretty much are a bunch of lines that simply allow a programmer to “set a first name” for their Customer class.
Here is the way we do it now….
' Private variable here in our class Private _FirstName As String ' Property called "FirstName" which gets and sets the variable _FirstName Property FirstName() As String Get Return _FirstName End Get Set(ByVal value As String) _FirstName = value End Set End Property
A lot of code just to set a simple string of our class. Anyone who has built custom data container like this, that basically hold a lot of information with little functionality, knows what I am talking about when they had to create 10 properties x 8 lines each. 80 lines just for a small class? Crikies! Well the engineers at Microsoft have decided to squish down these basic properties (and only the basic properties… not customized) down into one line. Below is the same property but defined in the new way…
' Say whaaaa? Property FirstName() As String
One line? Yeah, one line. Behind the scenes VB.NET will be creating the get and set and wire it to a variable of the same name but with a leading underscore. This is for basic properties only. Basic here I mean for properties that don’t need to do extensive validation of code coming into the property (which there probably should be) and or where you need only one get or set, not both (aka writeonly or readonly properties). It can take an initializer too which is cool… Property FirstName() As String = “Tom”
This subtle tweak can be useful and great for programmers who need to build small data container classes. However, in practicality, that is the only way it will be used. Now I guess you could build in necessary validation prior to property setting with something like this…
' Private function to check if name is less than 4, if it is, return an empty string Private Function checkName(Byval name as String) as String if name.length < 4 then return "" else return name end if End Function ' A couple name properties back in our data container class Property FirstName as String = checkName(firstNameOfCustomer) Property LastName as String = checkName(lastNameOfCustomer)
I am not too sure this example is realistic but it could be done. This example would pass the first and last name of a customer through our checkName routine prior to the property being set. But as you can see, this would almost be the same as actually writing the code in the property itself. So unless you need no validation, which I find is rarely the case, this would certainly be useful to you. These one line properties can also have things like interface implementations on them as well… another advantage I suppose.
4) Collection initializers. I am sure we have all seen code that has like 20 list.add statements. Wouldn’t it be nice to initialize a list with one line and specify the default values? Well that is what Microsoft are thinking…
' New one line style Dim list = New List(Of String) From {"abc", "def", "ghi"} ' Old way list.Add("abc") list.Add("def") list.Add("ghi")
I am actually loving this feature and I think it is going to be a well welcomed addition to the world. The concept of initialization of things like this is going to be of great use to the newbies who are getting their feet wet with .NET. I can’t tell you how much I hate looking through non refactored code of a hundred list.Add statements just to populate a single list control. Nice, neat and clean describes this feature and I love it. I can’t see any real drawbacks here. This would work with objects as well and just remember, the List is going to contain objects that need to be cast back to their original form before using specific methods! That part is not going to change.
5) Array Literals. I figured there would be an initializing setup that I would just hate because it is going to be such a pain in the neck to get newbies to realize their mistakes. Lets take an example of the line… Dim b = {1, 2, 3.5} First of all, what the hell is that? I am a firm believer that when you declare something you should declare it explicitly at first glance. No where in that statement does it really say you are using an array. But that is not my biggest gripe. My biggest is that most newbies would assume that it is going to be an array with a couple integers (1 and 2) and a double. Wrong! VB.NET is going to say this is an array of doubles because since it has a double (and the compiler doesn’t want to lose information) it will convert the integers to double as well. The final array will be 1.0, 2.0 and 3.5.
Professionals have no problem understanding this concept but the people new to the language are going to see a blurry data type declaration rule here. Is it integers? Is it doubles? Is that even an array or am I trying to set a variable to a string of “{1,2,3.5}”? It was a good idea, bad execution I think. Check out the code below…
' Array of (,) Dim e = {{1, 2, 3}, {4, 5, 6}} ' Array of ()() - Jagged Array Dim f = {({1, 2, 3}), ({4, 5, 6})}
Horrible idea because that is going to destroy readability and just make it harder for us at DIC to offer good quality explanations of why their code is not working. They probably will miss a parenthesis or a curly brace or not even realize they have setup a jagged array when they wanted a 2D array instead.
6) Last feature I am covering here is Nullable Optional Parameters. For those not familiar with nullable types, they are variables declared as instances of System.Nullable(T) and are often seen as being declared with a question mark. You may have seen them already in 2008 code and look like Integer? b = Nothing. Now this is not a standard data type per say (like integer, double, single etc), it is an object that represents the normal datatypes but where it supports methods of a Nullable object. Meaning it can be asked questions like if it has a value…
if b.HasValue then ' do some code end if
Great I love nullable values because it makes it that much more safe for handling values that may, or may not, contain null values. In the new version of VB.NET they are looking at making Optional parameters (parameters to functions or subs that don’t necessarily need to be added during the call, but will take a default value if left out) and making them nullable. Not only nullable, but that they can take an initializer as well which is pretty cool. Here are a couple of examples they give…
' Optional z integer parameter, but nullable and is initialized to Nothing Sub Add(x As Integer, y As Integer, Optional z As Integer? = Nothing) ' Optional z double parameter, nullable and initialized to 4 Sub Add(x As Integer, y As Integer, Optional z As Double? = 4)
Pretty straight forward. Then in the Add Sub procedure you can check if the nullable type “z” has value etc. Great little trick. Not much else needed there and I consider it a minor tweak that could have some interesting outcomes in the future as people play around with it.
So that is my basic overview of some of the new features coming into the language. In the document I mention above they cover another topic called General variance / covariance that you may find of interest. So be sure to check out the document for a little more explanations and examples to some of the topics covered here. I hope you found this informational and useful. Keep in mind that it doesn’t just take learning a language to keep you in the programming industry, it takes dedication to staying on top of it. Always explore the future of a language and what it could mean to you and your programs. Programmers love to live on the bleeding edge and pushing the envelope. It is our lifestyle.
Thanks for reading! 🙂