Wimdows.NET

Wim's .NET blog

Dump the Module keyword in VB.NET!

Though I'm mainly a C# developer, I now and then get exposed to some VB.NET stuff. No, this is not going to be a C# vs. VB.NET debate. We've seen enough heated arguments and flame wars on that topic over the years.

Something about VB.NET Console applications created in Visual Studio.NET (all versions), bugs me though: the dreaded Module keyword. The default skeleton for a Console app in VB.NET looks like this:

Module Module1
    Sub Main()
    End Sub
End Module


Whilst under the hood, a module is simply a class with static members and a private default constructor to prevent instantiation, I don't think its use should be promoted like that. And I really wonder why MS hasn't changed the default Console app skeleton to look as follows:

Class Program
    Shared Sub Main()
    End Sub
End Class


In my opinion, the Module keyword shouldn't have even existed in VB.NET. It's one of the reasons why a lot of VB.NET code I've seen simply gets dumped in a Module, and Object Oriented Programming goes out the window. Of course, there's nothing stopping you coding like that in VB.NET without using the Module keyword, or even in C# for that matter. But it is a step in the right direction in trying to get developers to think about object oriented class design first (static/shared vs. instance members etc), before shoving anything and everything in a Module.
Posted: Sep 03 2006, 12:53 PM by Wim | with 27 comment(s)
Filed under: , ,

Comments

PaulWilson said:

They actually added the same concept in C# v2.0 with the ability to declare a "static class" -- so good or bad aside, its not just VB anymore.  :)

# September 3, 2006 10:33 AM

Joe Brinkman said:

By this same logic, then everyone should stop programming in any non-object oriented language.  Functional languages like XSLT are wrong.  Assembly language: totally worthless.  

Languages, and various language features, should not be judged on their "purity", but rather on their utility.

I don't care what feature you implement, someone will abuse it.  Instead of complaining about the abuse, use it as an opportunity to train someone on a better way to accomplish the same task.

# September 3, 2006 11:11 AM

Wim said:

Paul - yes, I'm aware of that, but that still feels different.

I guess the bottom line of my argument is that 'classic VB' coders know the Module concept from before and a lot pretty much start applying it everywhere in their VB.NET code as well (because, hey VB.NET also has the Module keyword), leading very quickly to a total non-OO architecure.

Joe - the Module 'feature' is hardly a feature. It doesn' add anything other then provide a familiar keyword for classic VB developers which leads to non-OO practice.

# September 3, 2006 11:54 AM

Wim said:

Joe - with regards to your first remark:

I'm talking about VB.NET which *is* OO. You seem to read between the lines that I'm saying everything non-OO is bad; read again.

# September 3, 2006 11:58 AM

Joe Brinkman said:

Wim,

 I am not trying to imply that you think that everything non-OO is bad.  The problem is that you complain that a feature of VB.Net is not OO.  That is an argument about OO purity, which I find in general to be a very weak argument since almost every modern OO language supports some non-OO features.

VB had a long history before it ever receieved any OO features.  The fact that the .Net versions have fully embraced OO does not mean that it should turn its back on all the older developers who are used to some of the older features.  When VB.Net was first created, there was a huge backlash by the VB community, including some very vocal MVPs.  For this reason and others, Microsoft left in some of the functionality provided in earlier versions.

I am not sure where you are finding the abuse of the module keyword because I have rarely run across it in the last 6 years of ASP.Net development.  Most instances I have seen used it for utility methods that really didn't fit in nicely in the object model.  The options are to shove the method into a class where it doesn't belong, create a new class with the single static utility method (thereby creating lots of little utility classes), or creating a larger utility module, that is just used when you can't find a good place for a given method.  If you use the third method, then as you accumulate methods you should go back and look for ways to refactor your code to move related functionality into their own classes.  The fact that developers don't refactor their code has little to do with the use of the Module keyword.  I would guarantee you that they aren't refactoring the rest of their code either.  Removing the Module keyword would not change that.

# September 3, 2006 3:06 PM

Wim said:

Joe,

In the last 6 years I have seen this several times in VB.NET apps, web or otherwise. And that's only because I usually do C# work. In other words, I'm sure it actually is more wide spread. This is the same code that's usually littered with COM Interops to things like MSXML2 and uses classic ADO record sets...

VB.NET wouldn't turn its back on the VB developers by not having the Module keyword.

Whilst crappy non-OO style coding is possible regardless and in any OO language is not the point.

The point is that VB developers are being given something they know (though only a keyword), and as such some don't look any further and create Modules where in many cases they should be creating instance classes instead.

I'm not sure how to put it any clearer than that.

# September 3, 2006 6:19 PM

Anthony D. Green, MCTS said:

Your argument assumes that the majority of visual basic users are lazy unmotivated folk who will make no attempt ever to learn anything. You suggest that a VB user will see the VB runtime library, modules, and the forms editor and completely ignor .NET and code as though they were in VB6 with snaplines.

Trust me, if this is the developer removing the Module keyword won't change that fact.

VB had classes pre.NET and continues to have them and people who used them before .NET will still use them now.

Inheritence isn't an issue with static members. Polymorphism isn't an issue with static issues. Encapsulation isn't an issue for methods which require no state. A module is perfect for some methods.

There is a lot of... irrational judgement going on about OOP dogma and languages. OO is great, I love it but it's not a panacea nor is it universally applicable. OO is just another abstraction on lower level things (which demonstrates that things not OO can be abstracted).

Some people feel like they need MS to hold their hand and make all their design decisions for them. Tell them how to code - when to code - what to code. "We don't like this coding methodology so we've engineered your language such that you can't, now do as we say". But more advance coders who don't use toy languages like to have options at their disposal so they can make informed decisions on the right tool for the job with consideration to multiple criteria. VB is a multiparadigm language that doesn't force a limited single-minded mentality on its users and that's just one of the things I like about it.

You can't blame the language because some programmers lack the self control to make good decisions anymore than you can claim that C# has better programmers because it doesn't give them any choice. One chooses to be a good developer and one chooses to better themselves in their craft every single day - it's not something that is handed to you when you get your semicolon training wheels.

# September 14, 2006 12:01 AM

Ken said:

What I would like to know...are modules inefficient ?   No, they are not OO and there are obviously some cases where you need shared access.  I am working on a project now that was converted from VB.  All they did was create modules for just about everything.  Very little use of OO concepts.  Basically, they implemented the global keyword in .NET.  Its a bit like being back in the stone ages.  I am trying to convince the IT head OO would be better, faster and easier.  Any suggestions to make my point would be appreciated.

# October 7, 2006 2:20 PM

Tony said:

One really can't say OOP is better then structured programming (VB6) or vice versa.  What you need to do if you want to convince your IT head to switch to OO is make it clear to him/her that VB.NET is an OO language and it makes little sense to use structured programming paradyn in an OO language.

The problem I see with VB6 users going to VB.Net is while they are coding in .Net, they are still stuck with structured programming because they don't understand OOP.

[Quote]

Inheritence isn't an issue with static members. Polymorphism isn't an issue with static issues. Encapsulation isn't an issue for methods which require no state. A module is perfect for some methods.

[/Quote]

I don't advocate one programming paradyn over another but it sure sounds like you don't fully understand OOP here.

I personally would love to code

myInt = num(myString)

instead of

myInt = Int.Parse(myString)

But since I'm working in an OOP environment, I use the latter since it's bad practice to mix.

# October 10, 2006 3:44 PM

Tony said:

Sorry for the typo from the previous post

paradyn = paradigm

The thing with OOP is that it doesn't force you to use OOP paradigm.  I can easily code VB6 style in C++.  But I won't because why would I want to use procedure programming in an OOP language?  Just like one does not mix different naming convention in the same code, a developer needs to be disciplined enough to not mix the different paradigm.

# October 10, 2006 4:06 PM

Wim said:

Ken - that's a tough one.

Usually if things are coded that way, you can easily point out code patterns that are repeated often, and as such offer OOD suggestions to promote code-reusability and reduce  code bloat.

# October 11, 2006 3:22 PM

Mark said:

I had a similar issue and I ended up leaving the company.  For .net, I will use public shared for objects that truly need this scope and not just put everyting in a module.   In fact, I don't even use modules.

I have seen too many sloppy VB6 apps where almost everything was global/public to cut development time - and in just about every case, documentation was nowhere in sight

# December 21, 2006 8:41 AM

Martis said:

December , at : am Re:  tramadol online y pharmacy cods pharmacy treatment pharmacy dosage  tramadol online pharmacy  tramadol online Opium-Eater FDA pharmacy Buy Bontril SR tramadol online  tramadol online  

<a href=http://www.aifam.com?lZPrPwg6cU >tramadol online</a> [url=http://www.aifam.com?lZPrPwg6cU]tramadol online[/url] http://www.aifam.com?lZPrPwg6cU  

Tuesday,  tramadol hcl December , at : pm Re: rstMeds Questions views:  tramadol hcl  tramadol hcl Sheri -- Tuesday, December , at  tramadol hcl : pm Re: Hi tramadol hcl  

<a href=http://pnope.com/bmld >tramadol hcl</a> [url=http://pnope.com/bmld]tramadol hcl[/url] http://pnope.com/bmld  

Secure. Huge name-brand discretely Store. No consultation  cheap tramadol required,  cheap tramadol guaranteed fast delivery   cheap tramadol cheap tramadol and quality.  cheap tramadol We Ship Today,Fedex peace -- No Tuesday  

<a href=http://urlnip.com/?mZyOKAzy >cheap tramadol</a> [url=http://urlnip.com/?mZyOKAzy]cheap tramadol[/url] http://urlnip.com/?mZyOKAzy  

Stress  tramadol online of pharmacy. is your online medications from compensation  tramadol online  tramadol online for y Phentermine  tramadol online cods Phentermine treatment Phentermine phenylephrine  tramadol online Phentermine treatment Order  

<a href=http://www.aifam.com?lZPrPwg6cU >tramadol online</a> [url=http://www.aifam.com?lZPrPwg6cU]tramadol online[/url] http://www.aifam.com?lZPrPwg6cU

# March 19, 2007 3:32 PM

Odysseas said:

interesting

# May 18, 2007 8:34 AM

Dino said:

Cool.

# May 18, 2007 5:29 PM

Arsenios said:

Cool!

# May 19, 2007 5:26 PM

Fotis said:

Cool.

# May 21, 2007 2:50 PM

Christodoulos said:

Interesting...

# May 22, 2007 7:43 AM

Georgios said:

Cool.

# May 23, 2007 12:00 AM

Tasos said:

Sorry :(

# June 5, 2007 8:28 AM

Samaras said:

Nice!

# June 5, 2007 9:42 AM

Evripides said:

Nice...

# June 6, 2007 2:15 PM

Gregorios said:

Cool.

# June 10, 2007 10:10 AM

Giatas said:

interesting

# June 11, 2007 12:02 PM

Chris said:

I enjoyed learning about how to solve this issue. Rather then debate the issue would someone please suggest best practice books that you learned from or sites that would teach and reinforce the OO programing to those coming from a life of structured.

# June 25, 2007 9:14 AM

justanotherguy said:

In modules i dont get this: "  Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. "

guess thats enough to choose for it?

# June 27, 2007 11:52 AM

justanotherguy said:

didnt know the simple fact that u had to use the class name, and modules don't require this.

my mistake ;)

# November 29, 2007 9:10 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)