HamsterSpeak is a very syntactically flexible language. Whitespace is almost entirely ignored, many symbols have textual representation, and so forth. However, this flexibility can also be a bad thing if it results in messy, hard to follow code. On the Wiki, this is extra important, as many of the code examples are intended to assist beginners in learning.
Note: This article was intended for the OHR Wiki. However, you are free to adopt these standards in your own coding.
Before we jump into the code itself, a quick tutorial on how to actually post code:
script, foo, begin
script, foo, begin #blah end
(Note the leading spaces, even on the blank lines)
Please do not just post a script as plain text, as it can be difficult to read.
As mentioned above, the compiler will ignore whitespace just about anywhere, in a process known to computer scientists as "smushing". However, for the sake of our readers, do not take full advantage of this:
s cr i p t, f oo,beg in, bla h (12,end)
Instead, all keywords should be written without embedded white space ("begin" instead of "be gin", etc.), and all function names should be written as they appear in the Plotscripting Dictionary (which is, generally, as it would be in english "key is pressed" instead of "keyispressed"). Also, please put only a single command on a line.
Also, there should be a single space after a comma, to keep lists and the like neat and tidy, and a space between a function and its parameters.
script, foo, begin blah (12) end
Begin and End
In all but a few cases, one can freely interchange begin with (, and end with ). That doesn't mean you should, however.
script, foo,( blah, begin, 12) bar(42,end,end
Instead, the general guidelines are:
- Use ( and ) for parameters
- Use begin and end for blocks (if, script, etc.)
script, foo, begin blah (12) bar (42) end
When naming and choosing variables, keep these things in mind:
- They should be descriptive
- They should be short
- There should enough variables that you don't have to "reuse" them...
- ... but no more.
script, foo, begin variable (a,b,c) a:= get hero picture (1) b:= 12 c:= sqrt (42) #35 lines of code... b:= a a:= get hero palette (2) #etc. end
Variable names should rougly describe their purpose.
script, foo, begin variable (hero pic, hero pal, root 42) hero pic:= get hero picture (1) root 42:= sqrt (42) #35 lines of code... hero pal:= get hero palette (2) #etc. end
Note: root 42 is not a very good example of variable usage. However, the author was having a difficult time thinking of random examples. He appologizes profusely.
One tab == 2 spaces. Remember that.
When you enter a block, tab in once. When you leave a block, tab out once.
script, foo, begin variable(count,npc) count:=10 if(1==2) then, begin while(count) do, begin for(npc,count,1,-1) do, begin walk npc(npc,up,1) end end end end end
Without indentation, you'd never know that I inserted an extra end somewhere.
script, foo, begin variable(count,npc) count:=10 if(1==2) then, begin while(count) do, begin for(npc,count,1,-1) do, begin walk npc(npc,up,1) end count -= 1 end end end #end <-- the extra end
Hopefully, you're one step closer to producing good, clean code!