Naming Conventions

Naming conventions for writing program code:  a personal view

I normally use LiveCode.  LiveCode is a programming language derived from HyperTalk, itself inspired by SmallTalk and that again by Simula.  It is an object oriented system that uses a very English-like syntax.  Here is an example LiveCode statement:

if the last word of field "UserInput" is not an integer then beep

This looks more like a sentence about a program than a piece of actual program code.  Yet it is still formed according to strict rules.  I like programming in LiveCode for the reasons I give for choosing a programming language.  Of course, even in LiveCode one can write less readable pieces of code:

repeat with i=1 to abs(fDifference)

add lDelta to line iLine of lV[1]; subtract 1 from iLine

end repeat

Nevertheless, LiveCode is so nice that I could not resist writing a few tutorials.

Naming conventions

Identifiers start with a capital and each word in the name also has a capital.  Underscores are not used.
There may be a prefix letter, which then is lower case.  I chose these prefix letters:

c constant

f formal parameter

g global variable

i index (loops, arrays)

l local variable (if all other prefixes are respected, this one might be dropped)

n numbers of things

p custom property

h label field name (heading)

The use of these prefix letters significantly reduces the number of different names to be invented by allowing meaningful variants of a base name.

For example, the base name Line could be used in fLines (formal parameter), lLines (local temporary copy), iLine (index into fLines or lLines), nLines (the number of lines in fLines) and so on.

LiveCode unfortunately does not allow procedure nesting.  For handlers that should be nested I use names prefixed with those of the handler they should be in.

Examples:

constant cMaxWindowWidth = 350

global gFolderPath, gList

 

function MakeList fFolderPath

set the width of this stack to cMaxWindowWidth

set the defaultfolder to fFolderPath

put the folders into lFolders

put the number of lines of lFolders into nFolders

repeat with iLine = 1 to nFolders

MakeListInsertFile iLine of lFolders

end repeat

end MakeList

 

on MakeListInsertFile fFileFolderName -- a subroutine of function MakeList

put fFileFolderName&return after gList

end MakeListInsertFile

Coding conventions

If a number of statements should not be broken up because they are part of an "atomic" sequence, they are written on the same line (separated by semicolons).
E.g. a swap:

put a into temp; put b into a; put temp into b -- swap a and b

Nothing should be inserted between these statements, they are in fact a single statement.

Another one:  in LiveCode it is impossible to change chunks of custom property values:

-- this does not work:

put "a" after line i of the pProp of me

-- this works, but the three statements must be kept together:

put the pProp of me into lv; put "a" after line i of lv; set the pProp of me to lv

Statements may also be put several on one line in order to make code more compact:

if something then put "A" into lv1; put false into lFound; else exit mouseup end if

There must always be a good reason to put more than one statement on a line, but there sometimes is!