Here are some of my personal computer conventions.
+ or %20). If possible, avoid dashes (-) and underscores (_). Definitely avoid all other non-alphanumeric characters. This will avoid having to encode those characters in many situations.cmdDog v Dog.IBM. Some strongly oppose this rule. EG: Ibm.BigDog./bin./0best) and back ups or archives. Otherwise, the file should be accessed by pointers, shortcuts, aliases, etc.Option X is usually set by choosing the Tools menu, the Options selection, and then the General tab..
x =
"west " +
"wind " +
"blows";
y =
"west "
+ "wind "
+ "blows";
For comma separated values where each "value" starts on a different line, consider using leading commas (instead of trailing commas). EG: Compare these two blocks:
// Trailing commas:
obj = {
id: 2,
useAccessibilityFeatures: true,
autoLoad: true,
name: 'demo', //This erroneous trailing comma is hard to spot
// translateHelpTexts: true
};
// Leading commas:
obj = {id: 2 // Having an item in the same line as the brace helps with code folding
, useAccessibilityFeatures: true
, autoLoad: true
, name: 'demo'
// , translateHelpTexts: true
};
On the other hand, your text editor may have a convenient join feature. EG: Compare these two blocks:
// This block: a, b, c // Might get joined as: a, b, c // While this block: a , b , c // Might get joined as: a , b , cNote that an extra trailing comma is ignored for many cases in C (and therefore C#, Java, etc.). Your text editor may also have utilities for visually catching extra commas may not be as important. Your compiler or code checker may also check for missing commas too (EG: jslint.com). The main reason to avoid leading commas is probably because so much code is written without them. See http://www.sencha.com/forum/showthread.php?6796-Leading-comma-or-Trailing-comma-that-is-the-question., http://www.thatjeffsmith.com/archive/2010/12/trailing-or-leading-commas/.
color=red versus color = red) is a matter of consistency, legibility and (of course) syntax requirements. Usually you use whitespace to form little groups.
<p class="banana" name="Equador">.Provider=SQLOLEDB; Server=W2Kacctg; Database=pubs;.foo(3)), an array and its brackets (EG: bar[4]), or the like, but do use them around keywords in the language.
if (c == 5) {y = 4;} // Yes
if(c == 5) {y = 4;} // No
Fred, the frog. Miles per gallon (mpg).
function dome(x) {
p = [a, b, {c: 1, d: 2}];
q = (x + 3) / 4;
for (i = 0; i < 5; i++) {
r = 6 * i;
}
}
( (left parenthesis) of its parameter list. There should be one space between the ) (right parenthesis) and the { (left curly brace) that begins the statement body.
function fun(x) {y = 4;} // Yes
function fun (x) {y = 4;} // No
function fun(x){y = 4;} // No
( (left parenthesis). If the space is omited, then it can appear that the function's name is function, which is an incorrect reading.
ontap: function (x) {y = 4;} // Yes
ontap: function(x) {y = 4;} // No
if (c == 5) {y = 4;} // Yes
if (c == 5) y = 4; // No
x = "<a href=\"" + url + "\" title=\"" + tip + "\">" + link + "</a>"; y = '<a href="' + url + '" title="' + tip + '">' + link + '</a>';Of course some languages don't give you a choice. EG: In C# ' is used for
char, while " is used for string.
` ‘ ’ ' “ ” " o O 0 1 l I ifoo; // Yes foo; //No /* Yes * yes * yes */ /*No*/ -- Yes --No, especially in MySQL # Yes #No
Note that most of these apply especially to Visual Basic and/or VBScript, but other languages may have more language-specific conventions.
tlpObject, but must declare (eg Dim or var) locally.PtlpObject or ptlpObject.tlpObject unless both client-side and server-side variables exist. In that case use CStlpObject and/or SStlpObject.GtlpObject or APPtlpObject.KtlpObject.Session("Object"). Skip the tlp becuase these are all really strings.strX is a variable, while txtX is a text control.strSerialNumber to pass into or receive from a query string, but use SN in the query string itself.SSub().FtlpFunction().Follow the above for JavaScript, Java, C, C#, etc. except for the following:
private string myField;.myFun(int myParam).PI.Vector v = new Vector(3, 4, 5);. I confess that I frequently violate this to be perverted.IMyInterface.TMyGeneric.MyException. Suffix with "Exception".Here are the various case or notation conventions. The terms are ordered to build on concepts (i.e. they're not ordered alphabetically).
This is an example, George.a an the), prepositions (of between under through, etc.), or conjunctions (and or, etc.). EG:Notes on the Production of Essays in the Arts and Sciences.Notes On The Production Of Essays In The Arts And Sciences.lowercaseexampleUPPERCASEEXAMPLE
InterCapsExampleintraCAPSexample
this example uses spaces
thisexampledoesnot
this-example-uses-hyphens
this_example_uses_underscores
main_left-big (hypens and underscores)
THIS EXAMPLE USES SPACES
THISEXAMPLEDOESNOT
THIS-EXAMPLE-USES-HYPHENS
THIS_EXAMPLE_USES_UNDERSCORES
MAIN_LEFT-BIG (hypens and underscores)firtLetterNotCapitalized (aka camel case, camelBack case, lowerCamel case, humpBack case)FirstLetterCapitalized (aka UpperCamel case, Proper case, Pascal case)ILoveMyIBM (Acronyms all UPPER case)ILoveMyIbm (Acronyms capitalized like proper noun)thisISanEXAMPLEpulmiCORTpulmiZYMEiS tHIs rEaLly sO cOol? (Is this really so cool?)L|-|!z 5|_|(|<s (this sucks)
strMyName (A string variable)Indentation conventions are used for writing blocks of code for human readability. This comes mainly from Indent style [W].
while (x == y)
{
something();
somethingelse();
}
finalthing();
myfunction(
alpha, bravo, charlie,
delta, echo, foxtrot,
gamma
)
{
dostuff();
}
while (x == y) {
something();
somethingelse();
}
finalthing();
if (x == y) {
if (somefunc('hello', 'world',
'foo', 'bar')) {
a = b;
}
}
while (x == y)
{
something();
somethingelse();
}
finalthing();
while (x == y)
{
something ();
somethingelse ();
}
finalthing ();
stuff(n):
{ x: 3 * n;
y: doStuff(x);
y + x }
while(x==y){
something();
somethingelse();}
while(x==y){
something();
if(z==w){
somethingelse();}}
finalthing();
while(x==y){something();somethingelse();}finalthing();
In word processors, use tabs instead of spaces. This is because proportional fonts won't line up with spaces.
In code, the issue of tabs v spaces is much more complicated and opinionated! The only thing certain is that most programmers will code using monospaced fonts. See links like these:
It is suggested that "tabs" equate to a length between 2-8 spaces --pick a size an stick with it. A lot of Microsoft and Macintosh code has 4-space sized tabs( ), while a lot of Unix code has 8-space sized tabs ( ). Some people have more complicated rules about how many spaces they use in depending on context. In some cases you can adjust the IDE to interpret hard tabs in different soft tab sizes.
"Tab" can be hard tabs (\t, Chr(9), etc.) or soft tabs (faked with spaces). Left side indentation with hard tabs saves on the amount of white space characters used. If you need to count "tabs" in order to see how deeply nested a block is, then it is much easier to count hard tabs than soft tabs. However, as the example show, if your code is viewed in an IDE (Integrated Development Environment) where tabs are sized differently, then the code can look bad.
EGts4. This example uses hard tabs (represented with ->) and spaces (represented with .). Looks good... so far!
->if(x==3) -> ->// hard tab comment
->{...............// soft tab comment
-> ->func();
-> ->// hard tab comment
........// soft tab comment
->}
But what if it's moved to an IDE where the tabs are 8-spaces instead of 4? It looks screwy!
->if(x==3) -> ->// hard tab comment
->{...............// soft tab comment
-> ->func();
-> ->// hard tab comment
........// soft tab comment
->}
EGt4. This example is the same as EGts4 but with just hard tabs. Looks good... so far!
->if(x==3) -> ->// hard tab comment
->{ -> -> -> ->// soft tab comment
-> ->func();
-> ->// hard tab comment
-> ->// soft tab comment
->}
But what if it's moved to an IDE where the tabs are 8-spaces instead of 4? It looks screwy!
->if(x==3) -> ->// hard tab comment
->{ -> -> -> ->// soft tab comment
-> ->func();
-> ->// hard tab comment
-> ->// soft tab comment
->}
EGs4. This example is the same as EGts4 but with just spaces. Looks good... so far!
....if(x==3)........// hard tab comment
....{...............// soft tab comment
........func();
........// hard tab comment
........// soft tab comment
....}
But what if it's moved to an IDE where the tabs are 8-spaces instead of 4? It still looks great!
....if(x==3)........// hard tab comment
....{...............// soft tab comment
........func();
........// hard tab comment
........// soft tab comment
....}
EGts: One practice is "Indent good, align bad". Indent from column 0 and avoid using "precision" alignment because the tab size might be different in different enivronments. Using this practice, different users can freely set their IDE to the tab size they want.
Instead of this:
my_function ( ->parameter1,
-> -> ->parameter2,
-> -> ->parameter3) {
->x = 8;
->....// Comment under the 8
}
Try this:
my_function (
-> -> ->parameter1,
-> -> ->parameter2,
-> -> ->parameter3
) {
->x = 9;
->// Don't try to get precisely under the 9
}
Whether 4-space (above) or 8-space (below), it still looks good:
my_function (
-> -> ->parameter1,
-> -> ->parameter2,
-> -> ->parameter3
) {
->x = 9;
->// Don't try to get precisely under the 9
}
If the coder had used soft tabs instead of hard tabs above, the problem would not have arisen but other viewers would have to put up with the original coder's preferred size for soft tabs. While it may seem that the solution is to use hard tabs for indentation, and to avoid precision alignment, there are common cases for preferring spaces/soft tabs:
\t is often used to represent a hard tab in a character string. On the other hand, if you are writing code that writes code, especially nested code, it may be easier to put in \ts instead of copying in multiple spaces.In general, whenever you get someone else's code, check their tabs and spaces before you use it. Problems arise when you are working on code written by multiple developers and the developers have used a different number of spaces for their tabs. As long as there is no confusion in the depth of nesting, do not try to adjust the code. It is usually not worth the time to fix and the fix process is prone to error. On the other hand, some people will diddle with the tabs and spaces anyway.
Page Modified: (Hand noted: 2007-10-04 21:40:01Z) (Auto noted: 2011-10-27 18:26:37Z)