Web Solution Architect - Rockwell Collins - My ramblings on work, technology, & life in general

Twitter












Microsoft recommends using VBisms!?

A customer recently requested that I provide them with Microsoft's VB.NET coding standard for their developers to use on our project.   To prepare my document I referenced the Visual Basic Coding Conventions located on MSDN.  While reading this article I found some surprising recommendations that I very much disagree with.


Naming Conventions

“Use namespace qualifications rather than adding Imports statements. If a namespace is imported by default in a project, you do not have to fully qualify the code because it will run unqualified with Intellisense when copied and pasted.”

My comments:  Qualifications cause maintainence nightmares when the location of classes or namespace structures change.  I recommend using import statements.

New Keyword

”Use short instantiation” i.e. “Dim employees As New Collection()”  instead of “Dim employees As Collection = New Collection()”

My comments: The long instantiation is not much longer and is consistent with other CLR languages.

MsgBox Function

Use MsgBox instead of MessageBox.Show or Console.WriteLine.“

My comments: MsgBox is old VB6 style.  MessageBox.Show is consistent with other CLR languages.

Use My Namespace

“Use My features in preference to the .NET Framework class library or the Visual Basic run-time library.”

My comments: More VBisms... Other CLR lanuages use the .NET Framework class libraries.

Use the Visual Basic Run-Time Library Members

“Use the Visual Basic run-time library in preference to the .NET Framework class library.”

My comments: Come on...  The VisualBasic32 libary should not be used.

All of these recommendations encourage developers to write VB.NET code in the old VB6 style.  This goes against the common sytax that .NET brought to the table.  They also contribute to making VB seem like a higher level lanuage than other CLR languages, which encourages the VB is for less technical developers argument.  The fact that VB.NET is full of VBisms and we encourage their use is very disheartining... 


  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Microsoft recommends using VBisms!?

What I find truely hilarious (perhaps they don't know their own product very well) is

”Use MsgBox instead of MessageBox.Show or Console.WriteLine.“

1) Instead of Console.Writeline ?! does that not depend on what BEHAVIOR you want? :)

2) is this not already made implicit via "Use the Visual Basic Run-Time Library Members" as MsgBox is in the VB namespace?

perhaps they think many of the vb'ers out there won't know this so they have to explicitly state it, I agree with them and it is the problem with VB development; not the tool, the users (note that there are some good vb deverlopers and bad developers in other languages but the number of BAD developers in VB.NET is appalling)
12/23/2005 9:39 AM | Greg Young

# re: Microsoft recommends using VBisms!?

also from a quick reflector it would seem that the vb message box doesn't do a whole lot but it is still further overhead. The majority of the VB namespace is like this.

if (host1 != null)
{
window1 = host1.GetParentWindow();
}
if ((((Buttons & ((MsgBoxStyle) 15)) > MsgBoxStyle.RetryCancel) || ((Buttons & ((MsgBoxStyle) 240)) > MsgBoxStyle.Information)) || ((Buttons & ((MsgBoxStyle) 0xf00)) > MsgBoxStyle.DefaultButton3))
{
Buttons = MsgBoxStyle.OKOnly;
}
try
{
if (Prompt != null)
{
text1 = StringType.FromObject(Prompt);
}
}
catch (Exception)
{
throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", "Prompt", "String"));
}
try
{
if (Title == null)
{
if (host1 == null)
{
text2 = Assembly.GetCallingAssembly().GetName().Name;
}
else
{
text2 = host1.GetWindowTitle();
}
}
else
{
text2 = StringType.FromObject(Title);
}
}
catch (Exception)
{
throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", "Title", "String"));
}
(MsgBoxResult) MessageBox.Show(window1, text1, text2, ((MessageBoxButtons) Buttons) & ((MessageBoxButtons) 15), ((MessageBoxIcon) Buttons) & ((MessageBoxIcon) 240), ((MessageBoxDefaultButton) Buttons) & ((MessageBoxDefaultButton) 0xf00), ((MessageBoxOptions) Buttons) & ((MessageBoxOptions) (-4096)));
}



actual MessageBox code


{
if (!Enum.IsDefined(typeof(MessageBoxButtons), buttons))
{
throw new InvalidEnumArgumentException("buttons", (int) buttons, typeof(DialogResult));
}
if (!Enum.IsDefined(typeof(MessageBoxIcon), icon))
{
throw new InvalidEnumArgumentException("icon", (int) icon, typeof(DialogResult));
}
if (!Enum.IsDefined(typeof(MessageBoxDefaultButton), defaultButton))
{
throw new InvalidEnumArgumentException("defaultButton", (int) defaultButton, typeof(DialogResult));
}
if (!SystemInformation.UserInteractive && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == ((MessageBoxOptions) 0)))
{
throw new InvalidOperationException(SR.GetString("CantShowModalOnNonInteractive"));
}
if ((owner != null) && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != ((MessageBoxOptions) 0)))
{
throw new ArgumentException(SR.GetString("CantShowMBServiceWithOwner"), "style");
}
IntSecurity.SafeSubWindows.Demand();
int num1 = (int) (((buttons | ((MessageBoxButtons) ((int) icon))) | ((MessageBoxButtons) ((int) defaultButton))) | ((MessageBoxButtons) ((int) options)));
IntPtr ptr1 = IntPtr.Zero;
if ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == ((MessageBoxOptions) 0))
{
if (owner == null)
{
ptr1 = UnsafeNativeMethods.GetActiveWindow();
}
else
{
ptr1 = owner.Handle;
}
}
Application.BeginModalMessageLoop();
DialogResult result1 = MessageBox.Win32ToDialogResult(SafeNativeMethods.MessageBox(new HandleRef(owner, ptr1), text, caption, num1));
Application.EndModalMessageLoop();
return result1;
}

12/23/2005 10:16 AM | Greg Young

# re: Microsoft recommends using VBisms!?

I too was surprised to find these recomendations. Where I work the .net developers do both C# and VB.Net and there is an understanding not to use the VB runtime library if at all possible when writing code in VB.Net. We prefer all developers to use the .net framework methods in both languages. 12/17/2009 2:12 PM | Viktor