(download)
The first step to writing great Revolution scripts is often relegated to after-thought. I'm referring here to the naming of things. What's the big deal?
The true value of your code is determined more by an endless series of small naming decisions than any other single act.
NAME IT WHAT IT IS
I strongly recommend naming a thing exactly what it is--based on what it does or has done to it. That means using "it" repeatedly in a script would be verboten. Why? Because "it" tells us absolutely nothing about the data to which it points.
The same principle applies to using "x" or "i" as an index variable in a repeat loop. What does the var "i" tell you?
repeat with i = 1 to n
put i && the date into line i of fld 1
end repeat
Wouldn't the following tell you more:
repeat with theLineNo = 1 to theNoLines
put theLineNo && the date into line theLineNo of \
fld "list of dates"
end repeat
This makes your code self-documenting and much easier for you and your antecedents to read. It also establishes a higher market value for your code, app or company, provided you follow your own standards consistently.
NAMING A VARIABLE
When Dan Winkler invented HyperTalk (the basis for Revolution's scripting language) he would call a variable that contained the today's date the following: theDateToday. He would not have used tDate.
Scripters started prefixing vars with single char abbreviations to indicate what type of var they represented: t = temporary; g = global; l = local; a = array; k = constant. A worthy goal and I agree with much of it, actually--just not the business with temp vars. Using "the" as a prefix makes the code more readable, IMO.
My rationale goes as follows: gratuitous use of single character pre or postfixes serve to make the scripting language more codified and cryptic than it needs to be. I suggest using only as much mumbo-jumbo as absolutely necessary. Prefixing a global with a "g" makes sense, if you think you need to use globals, that is.
Some folks name the parameters of a function, command or system message handler with a lower case "p" preceding them. Why? No one can answer this. "So I can tell if it's a parameter." What difference would that make if it was a param? None. It's decoder ring stuff. The priesthood. Ignore its call to your nescient adolescent ego. You've probably got a wife and kid. What do you have to prove?
Since Revolution eliminates the need for data typing, I see no need to include the type of data to which a var points. I wouldn't use "available_boole", but I would use "isAvailable" because it reads very nicely in an "if" statement like this:
if isAvailable then
doReorder
end if
I think you can see where I'm going with whole line of reasoning. Proper naming can not only create readable code, but it makes for maintainable, saleable code.
NAMING A HANDLER
Why not name a custom handler with the same criteria in mind? Keep in mind how the name of a handler will look as you call it from another script.
This handler...
function todaysDate
return the date
end todaysDate
...will look like this when you call it:
put todaysDate() into theDate
function stockIsAvailable
put the number of lines in fld "stock" is not empty \
into haveStock
return haveStock
end stockIsAvailable
Name a variable or handler properly, and it works for you. Otherwise, you end up working for it.
Comments [3]