--------------------------
Here is the coding standard I do use in my case. Actually, the following standard of coding was written by me and was published in Asia's #1 Software Technology Magazine DeveloperIQ in April 2004 issue. (pg. 74-76).
--------------------------
It’s better to say useless, when a work doesn’t follow a very significant word of dictionary - “RULE” or “STANDARDS”. Unlike any other job may it be not related to our very context, VB has its own standards of coding. I don’t know how many developers follow these sorts of standards or follow after reading this article.
Here in the following section you can find basic coding standers for Visual Basic.
Initial Standards:
• In a Visual Basic project there are numbers of forms, modules, class files, resource files etc. Firstly all these files must be stored in the same folder, including the main project file (.vbp) with a related name and proper prefix.
• Related name means the “customer details” form should not be saved as “abc” it must be as “custDet”. A Proper prefix must be attached such as “customer details” form should be saved as “frmCustDet”. Below a list of prefix for various VB source files.
File Type Prefix Example
---------- ------- ----------
Project prj prjMyProject
Form frm frmCustDet
MDIForm mdi mdiMain
Class cls clsAdd
Module mod modUser
Resource File res resIcon
Property Page ppg ppgText
Data Report dsr dsrCust
User Control UCtl UCtlTree
User Document UDoc UDocInfo
• Must use naming convention for variables, controls etc. (Discussed in Previous issue Mar’04 ‘Give A Professional Touch to Your VB Application’)
• Suitable project title must be there with every project alone with proper version information, company name, file description, legal copyright & product name.
• Must specify a project icon, later after compilation it would be the icon for the .exe file.
• Always use a resource file to store icon, images etc. required for the project. It saves the memory in a huge extent by decreasing the size of the main application. It stores those media files separately in a file with .res extension.
To make a resource file go to ‘Add-Ins’ menu & click the ‘Add-In manager’ option. Then select VB6 Resource Editor & in the Load Behavior option select Loaded/ Unloaded.
After this go to ‘Tools’ menu & click ‘Resource Editor’. Then from the menu icon of ‘Resource Editor’ IDE add required icons, images or other files.
Here you’ve to put separate ID for each file. Suppose a background image is stored in the resource file and the ID is 101, then you’ve to write following codes to call that image.
Private Sub Form_Load()
imgBck.Picture = LoadResPicture(101, vbResBitmap)
End Sub
• The “OPTION EXPICITE” statement must be there in all source file. By using this statement you must have to declare each variable you need in your project. It stops any unused variable declaration & helps while debugging.
To declare this statement automatically while coding, go to ‘Tools’ menu and select ‘Options’. Then in the ‘Editor’ tab select ‘Require variable declaration’.
• Menu name must have a ‘mnu’ prefix. e.g. mnuFile.
• Menu separator must be used when there are logical breaks in the menu items. e.g Suppose in the ‘File’ menu ‘New’, ‘Open’, ‘Save’, ‘Page Setup’, ‘Print’ and ‘Close’ are the menu item. Then a separator must be used after ‘Save’ menu item & another before ‘Close’.
• Shortcut keys for menu items must be used as Windows. Such as we ‘Ctrl+C’ for copy, ‘Ctrl+X’ for cut & ‘Ctrl+V’ for paste.
• Every main menu must have an accelerator key. Such as ‘Alt+E’ for ‘Edit’ menu.
Coding Standards:
1. Variable Declarations
--------------------------
• Variables must be declared explicitly. Option Explicit should appear at the top of all modules and forms.
• Use the ‘Dim’ keyword for from level variables, but for module level variables ‘Private’ or ‘Public’ keyword should be used.
• Local variables should be called at the top of the procedure, not in the body section.
• Variables must no be declared ‘As New’ without proper requirement. Declaring as ‘As New’ has a scope of Creating Objects which may lead to over processing.
• ‘As’ keyword must be used while declaring a variable. ‘As’ keyword affirms a particular sort of data type. e.g. Dim strName As String.
• One variable should be pointed for a particular task, instead of several unmatched tasks.
2. Procedure Declaration
--------------------------
• Procedures must have a defined scope of such as ‘Public’, ‘Private’.
• Procedures must contain a clear exit code to clean up when they are not in a live role.
• Parameters in the procedure must be with a perfect data type.
e.g. Public Function chkNM(ByRef nm As String)
• Parameters should be declared as ‘ByRef’ for performance reason, when they are being not to be changed use ‘ByVal’.
Passing by values (ByVal) the procedure gets an exact copy of the passed argument.
Passing by reference (ByRef) the procedure gets a direct access to the passed arguments memory address.
3. Error Handling
-------------------
• Each and every function & procedure must have a error handler.
• ‘On Error GoTo’ statement should be use for an unwanted runtime error. Before & after the Error statement ‘Exit Sub’ should be used.
e.g.
On Error GoTo errMsg
Coding Begins
------------
------------
Coding Ends
Exit Sub
errMsg:
Msgbox “Error Message”, vbCritical
Exit Sub
• ‘On Error Resume Next’ statement should be placed correctly, as when a error occurs it immediately jump to the next line of code by ignoring the error. While using this kind of statement, the raised error must be trapped using Err.number statement and take a necessary step.
e.g. Suppose you are executing a particular exe file. Then the code should be as follow by using ‘On Error Resume Next’ Statement.
On Error Resume Next
Call Shell("C:\filename.exe")
If Err.Number = 53 Then
MsgBox "File not found", vbCritical
End If
• ‘On Error goto 0’ statement should not be use when you are not sure for a possible error. It skips the error trapping within a particular procedure.
4. Coding Structure
-----------------------
• Use a single statement per line.
• Avoid writing long codes in a single line; continue in next line using ‘_’ operator when a line goes over 80-85 characters of code.
• Never use a single line ‘If___Then’ statement & this statement must be end with an ‘End If’ statement.
• Proper indentation is a vital fact in coding. By default indent setting for ‘TAB’ key is 4 spaces. Indentation should be as follows…………………..
If myStr=”” then
statement1;
else
if myStr=”myname” then
statement2;
End If
End If
Nested ‘If______Then’ or ‘Do_______While’ or ‘For______Next’ statement must be intended properly level wise.
• A ‘Comment’ to a code tell us the purpose of that particular piece of code. And when it’s a group project comment is a very essential part. On the other hand it would be helpful for us while debugging & we editing or updating a code after a long time.
Comment must be intended as the same level of the concerning code.
5. Performance
------------------
• It is a must to clean up the used memory when the application comes to an end. Following table shows you how to retrieve used memory.
Type of Variable Code to Retrieve Used Space (Memory)
------------------- --------------------------
Form Unload frmFormName / Unload Me
String strStringName=vbNullString
Variant varVariantName=Empty
Object Set objObjectName= Nothing
• Must avoid dead codes. As dead codes are no longer needed by the application, it unnecessarily absorbs some memory & increase the size of the main application. While debugging many developers use just a comment mark before the dead code instead of erase that out.
• If possible use ‘Image Box’ instead of ‘Picture Box’, as it loads a picture much faster.
• Must not use any static variable. As static variables are reside in the memory for the entire execution time. On the other hand dynamic variables are more efficient, because dynamic variables are created when the procedure is on a run & killed immediately as the procedure ends.
• Fixed length variables engage more memory compared to variable length variable. So, if possible must declare variable length one to make your memory less occupied.
• For inserting blank spaces do not use ‘Chr$()’, as it decreases the performance. As a substitute use ‘vbTab’ statement for a better performance.
• If possible, always use the ‘Private’ keyword in procedures. As it will make that declared variable available only in that particular module, which improves the performance.
• For faster performance use ‘Compile to Native Code’. although it needs more memory. For faster optimization use ‘Optimize for Fast Code’, although it needs more memory.
On the other hand ‘Optimize for Small Code’ avoids unnecessary access to virtual memory of local disk, but in the running situation it absorbs physical memory in a huge.
6. Others
-----------
• Never use a fixed full file address when using ‘Open’ or ‘Close’ statement, as it differs computer to computer; rather use any path using system function. Such as app. path or use GetSysDir().
• Every file must be opened with a particular file name (e.g. Open …………#1) & while closing use the related file number after the Close statement.
• ‘+’ operator should not used while concatenating two strings. In this case ‘&’ operator must be used.
• Always use an affirmative ‘Boolean’ variable. e.g. You want to declare a Boolean variable for checking null values; in this is use ‘blnIsNull’ instead of ‘blnIsNotNull’.
• The ‘GoTo’ statement should be avoided except ‘On Error GoTo’ as it may cause unwanted skipping of important.
:)
Main Topics
Browse All Topics





by: MilanKMPosted on 2005-10-03 at 12:50:30ID: 15009302
Hi Jenn3,
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
I’m a Software Developer & till date have done around 10-12 successful Software & Web projects for various clients. As I’m a student I don’t have any team & I have done all those single-handedly & that’s why I’m attracted to answer.
Whenever I do start a new project, I do following points in my documentation.
1> Introduction/ Objectives
2> System Analysis
- Identification of Need
- Preliminary Investigation
i. Questionnaires
ii. Interviews
iii. Observation
iv. Sampling
3> Feasibility Study
- Technical Feasibility
- Economical Feasibility
- Operational Feasibility
4> Software Paradigm Used *
5> Software & Hardware Requirement Specifications
6> Testing & Test Reports
7> Training & Implementation
* I have used RAD model in first 2-3 of my projects but later I moved to Incremental Model as with this model customers can start using the software (surely not with all requirments) much earlier then RAD and it also helps me to track the bugs out and fixed it with the next increment.
Now, ur second question: “What language and technology do you use?”
Well, for software projects with small to medium transaction I use Visual Basic as front-end & Access as back-end. But, for projects that deal with larger data, I use MySQL.
Where as for web-based, project ASP & Access/ MySQL.
But now I’m trying to move to VB.NET (PHP & ASP.net for web-based project), but may be for lack of time (As I’m a student), I think it will take time.
Tools: Exactly what tool ur asking for. By the way, I always like to use any Microsoft tools. For web based graphics I use Macromedia tools e.g. Fireworks for static graphics.
Finally the concerning fact, well I think always it's customer satisfaction & user friendliness. Whenever I do start a project, I always keep one thing in my mind that I’ve to provide such an interface that can be easily handled by a very new PC user after a 2-3 days training. To provide the best possible interface, I always use my own standards of designing & developing.
--------------------------
** To end with one point I can’t resolve, that I can surely declare it that you are already a master (as I read almost all of you posts here at EE) on the discussed area, but why u are with such a question. May be ur dealing with big projects or anything else?
--------------------------
:)