Tools Links Login

Code Formatting - the variable naming part

hers is the second part of my article on code formatting. it explains how important variable naming is and how you should declare them.

Original Author: Coding Genius

Code

xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">






Code formatting – The part about variable naming





Code formatting –
The part about variable naming


 


If you have read my last article about code formatting then
you will probably notice that I missed out a part on correct variable naming. href="http://www.planetsourcecode.com/vb/scripts/showcode.asp?txtCodeId=28172&lngWId=1">Click
here to read the last article first (Strongly recommended).


 


Contents:


 


            Introduction


            href="#Conv">My Conventions


           


 


Introduction:style='font-family:Arial'>


 


If you ever make any kind of program then chances are that
95% of the time you will use variables (If not then I suggest you go back and
(re) learn to program). Variables are allocated pieces of memory that visual
basic makes for us whenever we type


 


Dim something as DataType


 


Naming these variables is important. It allows us to
access these parts of memory knowing what we are accessing. What if you had a
list of variables as so:


 


Dim a as
integer


Dim b as
integer


Dim c as
integer


Dim d as
string


Dim e as
string


 


Now unless you have a good memory you will not be able to
track what variables hold what. This would be an absolute nightmare when
debugging. Now consider this:


 


Dim PhoneNumer style='color:blue'>as integer


Dim InsuranceNumber style='color:blue'>as integer


Dim HouseNumber style='color:blue'>as integer


Dim FirstName as string


Dim SecondName style='color:blue'>as string


 


Much easier to remember what is supposed to go into each
variable. So much easier to debug. Consider calculations as well.


 


d = a/b*c+1a*sin(d^2)


 


Most people (including the author of that code 1 week later)
would look at this and end up with a very bad headache.


 


Result = FirstInput / SecondInput * Pi+1 FirstInput*Sin
(Result ^ 2)


 


Sure, it looks a little longer and sure it looks complex
still, but it’s a complex calculation. With good variable names it is easier to
keep track and remember why this multiplies this, why that is being halved and
so on.


 


I would just like to say that recently I downloaded an MP3
player by somebody whom I will not name.


 


Their variable names went something like this:


 


JohnSucks


Irule


MaryJaneIsHot


UnusedThingy


Argh


ThisIsTooLongNow


Hmmmm


 


E.T.C


 


Ok. I think you get the picture. Bad naming is bad coding
and bad coding means people will look at your work and throw it in the trash,
and then they will go and rip you apart in the comment section.


 


My
conventions:


 


Always put an indicator in the variable name as to what
data type it is. Like this:


 


Dim strName as
string


Dim intStrName style='color:#3366FF'>as integer


Dim lngName as
long


Dim sngName as
single


Dim blnName as
Boolean


 


That’s all there is to it. Why bother doing this I hear
you ask? No really…I can hear voices…they are after me…ARRRRRGHH…cough…funny
turn there. So why bother doing this?


 


If you are writing long code that is hundreds of lines and
you cant quite remember what data type your variable is then you can just refer
to its name. This saves you having to right click your variable, click
definition, find out, then have to spend a further 10 minutes trying to find
your place again. Just does it (Nike’s logo J), it is considered good practice.


 


Another thing is to try and avoid the variant data
type at all costs. Do not even go near this. It is a big ugly data type. It
takes up way too much memory and it gets all your data confused. If you put an
integer into it, then goodness knows what will come out the other end, a
string, a long, who knows. This is just a big mix of all the data types into
one. It slows games and things like that down incredibly. So…always…I mean
NEVER use variant unless you really must.


 


That’s about all I can think of right now. If you have any
suggestions then I will be glad to consider them. Please vote and leave
feedback (You know…world peace and the end of famine and stuff will happen.
From the last article remember.) Ok bye bye all J.


 


<Coding Genius>


 


 


 


 





About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 99 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.