MPL Training
Lesson 4
Black Panther

General Formatting

So, we have looked at an example MPL program, and saw briefly how it was 
constructed. Let's look at how, and why some of it is set up the way it is.

-=-=-=-=-=-=-=-=-

First, let me answer a question before it gets asked. :) Pascal, and therefore
MPL, is a non-case sensitive language. If you have a variable called:

Count

It is exactly the same as:

count
cOuNt
COUNT
...etc.

I know some of you will look at my code and say, "Why are some of the words
capitalize, and sometimes they're not?".

When I'm writing code, I have come up with a specific way I like the code to 
look. It makes it easier for me to work with. So, when I type in a keyword, 
such as WriteLn, I'll use what is referred to as CamelCase. It's just a habit
I got into...

But, just remember that WriteLn, is the same as writeln and WRITELN.

-=-=-=-=-=-=-=-=-

Now, while we're looking at formatting, let's talk a bit about indentations.

No, in Pascal, and MPL, the indents are irrelevant. The compiler doesn't care
if anything is indented. BUT!, for me, it helps to keep the code readable. Here
is an example of completely acceptable code:

procedure bnb
Var
done: Boolean=false
ch  : char
Begin
// get a room, eat at diner, shop, gamble, etc
Repeat
DispFile(rcspath+'bnb.ans')
GotoXY(3,19)
WriteLN('Your Command, '+Plyr.Alias+':')
ch:=upper(OneKey('BESGDR',True))
Case ch Of
'R':done:=true
'B':room
'E':eat
'S':shop
'G':gamble
'D':saloon
End
Until done
End

While this is acceptable code, to the compiler, it can be very difficult for
a person to read. This is why when I write code, I use a specific indentation
method.

Procedure bnb
Var
  done: Boolean=false
  ch  : char
Begin
  // get a room, eat at diner, shop, gamble, etc
  Repeat
    DispFile(rcspath+'bnb.ans')
    GotoXY(3,19)
    WriteLN('Your Command, '+Plyr.Alias+':')
    ch:=upper(OneKey('BESGDR',True))
    Case ch Of
      'R':done:=true
      'B':room
      'E':eat
      'S':shop
      'G':gamble
      'D':saloon
    End
  Until done
End

This is much easier for someone to read, and follow what is happening. My 
personal choice is to use a 'tab' of two spaces, and indent everything within
each 'block' of code. You'll see everything within the 'Begin'/'End' block is
indented, then everything within the 'Repeat' block is indented an additional
two spaces. Using this method, or one similar, can make it very easy for the
programmer to see which 'End' matches up with which 'Begin', etc.

-=-=-=-=-=-=-=-=-

This seems to be an area that many people have questions on, so let's take a 
look at when you need a Begin/End.

The general rule, if you are working with an If/Then statement, if you only 
have one line, you do not need it. You can still use them. So, if you had this
code:

If movie='SpaceBalls' Then
  WriteLn('Great Movie!')
  
you could also write it as:

If movie='SpaceBalls' Then
Begin
  WriteLn('Great Movie')
End

Both of these are perfectly acceptable. I would probably say, if you are in
doubt, put it inside a Begin/End block. It doesn't hurt anything.

However, if you have the following code:

If movie='Riding In Cars With Boys' Then
Begin
  WriteLn('Waste of two hours...')
  TwoHours:='You can never get back'
End

As this has multiple lines to the If/Then, it needs to be inside a Begin/End
block.

The same rules apply if you are using any other type of loop. A For/Do or While
or Repeat/Until are exactly the same as the rules for If/Then.

Also, whenever you have a procedure or function within your MPL program, the
code needs to be inside a Begin/End block.

Procedure PrintName
Begin
  WriteLn(UserName)
End

Even though this is only one line, because it is a procedure, it must be 
included in a Begin/End block.

