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.
AI
Resumo por IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Código fonte
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<link rel=File-List href="./Contents_files/filelist.xml">
<title>Code formatting – The part about variable naming</title>
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author> Thomas Waddell</o:Author>
<o:LastAuthor> Thomas Waddell</o:LastAuthor>
<o:Revision>1</o:Revision>
<o:TotalTime>28</o:TotalTime>
<o:Created>2001-10-17T13:34:00Z</o:Created>
<o:LastSaved>2001-10-17T14:02:00Z</o:LastSaved>
<o:Pages>2</o:Pages>
<o:Words>539</o:Words>
<o:Characters>3074</o:Characters>
<o:Lines>25</o:Lines>
<o:Paragraphs>6</o:Paragraphs>
<o:CharactersWithSpaces>3775</o:CharactersWithSpaces>
<o:Version>9.3821</o:Version>
</o:DocumentProperties>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;
mso-font-charset:2;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:0 268435456 0 0 -2147483648 0;}
@font-face
{font-family:Kaufmann;
panose-1:0 0 0 0 0 0 0 0 0 0;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-format:other;
mso-font-pitch:variable;
mso-font-signature:3 0 0 0 1 0;}
@font-face
{font-family:"Formal Script";
panose-1:2 11 114 0 0 0 0 0 0 0;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:3 0 0 0 1 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
h1
{mso-style-next:Normal;
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
page-break-after:avoid;
mso-outline-level:1;
font-size:12.0pt;
font-family:Arial;
mso-font-kerning:0pt;}
p.MsoBodyText, li.MsoBodyText, div.MsoBodyText
{margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
mso-bidi-font-size:12.0pt;
font-family:Arial;
mso-fareast-font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;
text-underline:single;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;
text-underline:single;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:35.4pt;
mso-footer-margin:35.4pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<body lang=EN-GB link=blue vlink=purple style='tab-interval:36.0pt'>
<div class=Section1>
<h1><span style='font-size:14.0pt;mso-bidi-font-size:12.0pt'>Code formatting –
The part about variable naming<o:p></o:p></span></h1>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>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. <a
href="http://www.planetsourcecode.com/vb/scripts/showcode.asp?txtCodeId=28172&lngWId=1">Click
here</a> to read the last article first (Strongly recommended).<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><b><span style='font-family:Arial'>Contents:<o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-family:Arial'><span style='mso-tab-count:
1'> </span></span></b><b><span style='font-size:11.0pt;mso-bidi-font-size:
12.0pt;font-family:Arial'><a href="#Intro">Introduction</a><o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><span style='mso-tab-count:1'> </span><a
href="#Conv">My Conventions</a><o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><span style='mso-tab-count:1'> </span><o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></b></p>
<p class=MsoNormal><a name=Intro><b><span style='font-family:Arial'>Introduction:</span></b></a><b><span
style='font-family:Arial'><o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></b></p>
<p class=MsoBodyText>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</p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> something <span style='color:
blue'>as</span> <span style='color:blue'>DataType<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoBodyText>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:</p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> a <span style='color:blue'>as</span>
<span style='color:blue'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> b <span style='color:blue'>as</span>
<span style='color:blue'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> c <span style='color:blue'>as</span>
<span style='color:blue'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> d <span style='color:blue'>as</span>
<span style='color:blue'>string<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> e <span style='color:blue'>as</span>
<span style='color:blue'>string<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoBodyText>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:</p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> PhoneNumer <span
style='color:blue'>as</span> <span style='color:blue'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> InsuranceNumber <span
style='color:blue'>as</span> <span style='color:blue'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> HouseNumber <span
style='color:blue'>as</span> <span style='color:blue'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> FirstName <span style='color:
blue'>as</span> <span style='color:blue'>string<o:p></o:p></span></span></p>
<p class=MsoBodyText><span style='color:blue'>Dim</span> SecondName <span
style='color:blue'>as</span> <span style='color:blue'>string</span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:blue'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Much easier to remember what is supposed to go into each
variable. So much easier to debug. Consider calculations as well.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>d = a/b*c+1\a*sin(d^2)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Most people (including the author of that code 1 week later)
would look at this and end up with a very bad headache.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Result = FirstInput / SecondInput * Pi+1 \ FirstInput*Sin
(Result ^ 2)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>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.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>I would just like to say that recently I downloaded an MP3
player by somebody whom I will not name.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Their variable names went something like this:<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>JohnSucks<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Irule<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>MaryJaneIsHot<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>UnusedThingy<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Argh<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>ThisIsTooLongNow<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>Hmmmm<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>E.T.C<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'>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.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><a name=Conv><b><span style='font-family:Arial'>My
conventions:</span></b></a><b><span style='font-family:Arial'><o:p></o:p></span></b></p>
<p class=MsoNormal><b><span style='font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></b></p>
<p class=MsoBodyText>Always put an indicator in the variable name as to what
data type it is. Like this:</p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:#3366FF'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> strName <span style='color:#3366FF'>as</span>
<span style='color:#3366FF'>string<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:#3366FF'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> intStrName <span
style='color:#3366FF'>as</span> <span style='color:#3366FF'>integer<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:#3366FF'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> lngName <span style='color:#3366FF'>as</span>
<span style='color:#3366FF'>long<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:#3366FF'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> sngName <span style='color:#3366FF'>as</span>
<span style='color:#3366FF'>single<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:#3366FF'>Dim</span><span style='font-size:11.0pt;
mso-bidi-font-size:12.0pt;font-family:Arial'> blnName <span style='color:#3366FF'>as</span>
<span style='color:#3366FF'>Boolean<o:p></o:p></span></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;mso-bidi-font-size:12.0pt;
font-family:Arial;color:#3366FF'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoBodyText>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? </p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoBodyText>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 <span style='font-family:Wingdings;
mso-ascii-font-family:Arial;mso-hansi-font-family:Arial;mso-char-type:symbol;
mso-symbol-font-family:Wingdings'><span style='mso-char-type:symbol;mso-symbol-font-family:
Wingdings'>J</span></span>), it is considered good practice.</p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoBodyText>Another thing is to try and avoid the <b>variant</b> 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.</p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoBodyText>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 <span style='font-family:Wingdings;
mso-ascii-font-family:Arial;mso-hansi-font-family:Arial;mso-char-type:symbol;
mso-symbol-font-family:Wingdings'><span style='mso-char-type:symbol;mso-symbol-font-family:
Wingdings'>J</span></span>.</p>
<p class=MsoBodyText><span style='font-family:Kaufmann'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
<p class=MsoBodyText><span style='font-size:16.0pt;mso-bidi-font-size:12.0pt;
font-family:Kaufmann'><Coding Genius></span><span style='font-size:16.0pt;
mso-bidi-font-size:12.0pt;font-family:"Formal Script"'><o:p></o:p></span></p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
<p class=MsoBodyText><![if !supportEmptyParas]> <![endif]><o:p></o:p></p>
</div>
</body>
</html>
Comentários originais (3)
Recuperado do Wayback Machine