05.03.2013 Views

Amiga Computing - Commodore Is Awesome

Amiga Computing - Commodore Is Awesome

Amiga Computing - Commodore Is Awesome

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

vs<br />

I)<br />

• • 0<br />

• • • •<br />

• • •<br />

• • 111 •<br />

his months theme, in a nutsnell, is<br />

about writing understandable code,<br />

and while most programmers will<br />

doubtless have their own ideas on<br />

what constitutes an easy-to-read program, I<br />

thought it might be useful to recap on the<br />

Conventions I use. One thing that is obviously<br />

helpful is the use of understandable names for<br />

variables and I tend to use lowercase names.<br />

adding underscore characters to improve<br />

readability.<br />

For example, an Mexx statement which reads:<br />

iii t_f ligT<br />

f. se t nit fle e - didif<br />

lUE .1<br />

/, has decided to dolt *I<br />

!s<br />

s<br />

I<br />

n<br />

With m function names I capitalise the first letter of<br />

each y part of the function name -<br />

CalculateAverageo, m<br />

GetResponsen and so on.<br />

ARexx i itself doesn't care about the capitalisation<br />

(function n names are all treated as upper case<br />

anyway) d but the above mentioned arrangement<br />

does , seem to aid readabefity.<br />

m Above all make sure the name tells you<br />

something u about what the function does -;t<br />

may c seem all very clever at the time to create a<br />

function h called HaveANiceDay() but six months<br />

later p its likely to be you who's sitting there<br />

wondering r what on earth It does!<br />

e ARexx variables are effectively typeless so<br />

you f don't need to declare variables as hokdino<br />

enumbers,<br />

strings and so on Despite this,<br />

rbelieve<br />

it is actually very useful to be able to<br />

r<br />

imply something about the type of data held in<br />

ea<br />

variable from its name.<br />

d<br />

You may have noticed that I often add a S<br />

t<br />

Suffix to Men variables which are specifically<br />

o<br />

used to hold text smngs. I might, for example<br />

suse<br />

nameS and address$ to collect name and<br />

o<br />

address strings from a user. If. on the other<br />

m<br />

hand. I knew that the user input was going to<br />

e<br />

be a number then id use variable names like<br />

t<br />

value, age, x, or n.<br />

h<br />

should point out that in this case these<br />

i<br />

'pseudo type' arrangements are not a common<br />

n<br />

ARexx convention and internally. ARexx cares<br />

g<br />

little about the types of values placed into a<br />

l<br />

variable. Nevertheless the convention has<br />

i<br />

Served me well and it is one I recommend.<br />

k<br />

e<br />

With some advanced applications it is also<br />

: occasionally useful to adopt additional<br />

conventions. Prefixing global variables with the<br />

character g_ and suffixing pointer variables<br />

I1a5511 wding• •••• Il • •••<br />

lte115<br />

•••<br />

•••<br />

i[H<br />

1<br />

Praire.. 1<br />

1<br />

•:<br />

-J1<br />

ite rkbe nch<br />

-<br />

1<br />

,<br />

1<br />

dmi ;She ti<br />

* spy<br />

his micro w<br />

t<br />

▪ Fine tC e 1 1 0<br />

sut s<br />

lions •e sotts<br />

H •<br />

•d :C ld:t. tc4<br />

s t t<br />

* /<br />

Set pe rsm•te rs to be to rotor units. .0 .<br />

' tit 0o<br />

Outer<br />

simply by loading values into vanables [which<br />

are subsequently never changed[ My<br />

preference is to use uppercase names for<br />

constant values like this.<br />

ESCAPE • 'WA<br />

By getting into the habit of placing such<br />

Fair comment<br />

All Mem( programs have to start with a comment line so there's a good chance there<br />

will, at least, be a program name at the start of your scripts. But why stop there -<br />

additional comments can make a world of difference to understanding a program.<br />

Don't make the mistake. inCidentally, of thinking that comments are just for the<br />

benefit of other users and that you understand your code well enough not to need<br />

additional remarks. You may understand your code when you write it. but it<br />

amazing how code tricks, which seemed perfectly obvious at the time they were<br />

written, appear to loose their 'inherent obviousness' as time goes on.<br />

You should not get carried away to the point where you impose unreasonable<br />

numbers of restrictions on either yourself or anyone else who has to read your code.<br />

The aim is to adopt a set of coding guidelines which help and are easily usable, and<br />

luckily, for the most part at least, all that s needed is common-sense coupled to a<br />

consistent methodical approach!<br />

Copy Je st<br />

T<br />

O<br />

N<br />

.<br />

▪ ftine<br />

lt 1 1<br />

1 1<br />

t • C a C<br />

i<br />

s g::1 :<br />

h<br />

e•<br />

a /<br />

d<br />

• Oft the So<br />

4. I t thpfe •F<br />

/4.<br />

"<br />

•.4 t .rne .141<br />

-<br />

,<br />

b<br />

r<br />

pleti<br />

o " - • a/NOV<br />

a<br />

'<br />

r t<br />

e<br />

.<br />

0/<br />

A hills extra Cara g when wrelona Allema script*<br />

can produce clividanda ) In the ions run!<br />

:<br />

t<br />

Using _p can help tell e you something about the definitions near the start of a program. you will<br />

data stored in the variable. l<br />

t<br />

Another rule of thumb' i concerns avoiding<br />

always know where to look for them the<br />

separation also makes the values easy to change<br />

the use of absolute Constants n within the bulk of and because symbolic names rather than the<br />

your program code unfortunately. ARexx<br />

underlying definitions themselves are used, the<br />

doesn t provide much direct help in this area program automatically becomes easier to<br />

but 'pseudo constant' values can still be set up understand.<br />

This 'trick' is especially useful when dealing<br />

with control character sequences (this of course<br />

was what last months instalment was all about'<br />

but the ideas can also be applied in other areas<br />

as well. There is. for instance. a good case,<br />

particularly with larger scripts, for eliminating<br />

explicit text messages from the bulk of your<br />

code. If. for example, you Set up this initial error<br />

message definition<br />

Indium/ . 'sorry this •ILUS is not<br />

correct'<br />

then within the main sections of the program<br />

the appropnate error message can be displayed<br />

using:<br />

say WRONG_ViLin<br />

Equally important is trie fact mat the message.<br />

which may actually get used in a number of<br />

different code areas of the script, is now defined<br />

in a single place This ensures that any changes<br />

Co the initial definition results in those<br />

modifications automatically being used<br />

throughout the program!<br />

<strong>Amiga</strong> <strong>Computing</strong><br />

MAY 1995<br />

•••• •<br />

If qou haue<br />

trouble<br />

understanding<br />

Ahem (Ode<br />

qou'ue written<br />

months or<br />

gears ago,<br />

the5e guide<br />

lines from<br />

Paul OLIPrad<br />

(fluid make<br />

tour coding<br />

life ea5ier

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!