03: Label Convention

"Readible - Modular - Expandable code"

Applied Object-Based Programming relies on several simple labeling rules to imitate many OOP features such as Namespaces and Encapsulation. In this lesson you will learn how using a Label Convention will increase your programming productivity. Why you may ask? My answer is simple: by following these rules your code become structured and standardized. Others who know these rules will be able to quickly interpret and extend your code.

Labels are made up of the following words: Prefix, Object, BaseObject, Property, Purpose.

Prefix - Optional acronym for the project. It is used for all labels. Ideal for code libraries.
Object - Name of the Object
BaseObject - Name of an Base Object Pointer
Property - Name of an Object's Property
Purpose - A Verb or short phrase that describes the purpose/action of Object's Method or Construct.

The Rules

Below are the specifc rules for labeling an Object's Methods and Constructs.

Constants: All Caps. Each word separated with an underscore(_).

{PREFIX}_{OBJECT}_{BASEOBJECT}_{PURPOSE}

Example: Const MYGAME_PLAYER_MAXCOUNT%

Globals and Arrays: Capitalize the first letter of Object, BaseObject, Property, Purpose. Each word separated with an underscore(_).

{Prefix}_{Object}_{BaseObject}_{Property}_{Purpose}

Example: Global MyGame_Player_Active%
Example: Dim MyGame_PlayerID(MYGAME_PLAYER_MAXCOUNT%)

Functions (Methods): Capitalize the first letter of Object, BaseObject, Property, Purpose. Only the Prefix is separated with an underscore(_).

{Prefix}_{Object}|{BaseObject}{Property}{Purpose}.{ReturnObject}

Example: Function MyGame_playerWeaponAmmunitionLoad(this.player)
Example: Function MyGame_playerWeaponAmmunitionLoadByPower(this.player)

Function Parameters and Local Variables: All lower case. No underscore(_).

{prefix}{object}{methodparameter}

Example: Function MyGame_playerMovement(this.player, mygameplayerx#, mygameplayery#)

Object Pointer by ID: Capitalize the first letter of Object, BaseObject, Property, Purpose. Each word separated with an underscore(_). Uses Global Keyword and '[]'.

Global {prefix}{object}_ID.{ReturnObject}[Elements]

{Prefix}{Object}_ID[objectid]=instance

Example: Global MyGame_player_ID.player[MYGAME_PLAYER_MAXCOUNT%]
Pointer Assignment: MyGame_player_ID[this\id]=this

Object Key: 'ID' Suffix added to Field label.

Field ID%
Field {baseobject}ID%

Example: Field playerWeaponProfileID%

Highly Recommended Reading: Writing Readable Code by Brent P. Newhall

Exercise:

  1. Using the OBP Label Convention (no prefix required), create a the following Methods and Constructs for the invader object.
    1. A Constant that represents the maximum number of Invaders: INVADER_MAX. Assign the value of 64 to the Constant.
    2. A Global that counts the number of invaders created: invader_Index
    3. A Method: invaderNew (Constructor). The method performs the following tasks:
      1. Excepts no parameters.
      2. Create an invader instance and assigns it to this.invader.
      3. Add 1 to invader_Index.
      4. Assign invader_Index value to this\id property.
      5. Uses this\id property as the Element Value for the Array Object Pointer invader_ID . Assign this.invader to it.
      6. Return this.
    4. A Method: invaderDelete (Destructor). The method performs the following tasks:
      1. Excepts the parameter: this.invader
      2. Delete this
      3. Nothing Returned

Email-in this Exercise for Grade!