Proper coding format

From Scriptwiki
Revision as of 17:04, 25 September 2008 by Aca20031 (talk | contribs) (two spaces)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

All programing and scripting languages are different, and therefore all have their own set of unwritten rules. These rules, called coding conventions, are not enforced by the language itself. In other words, if you do not follow coding conventions for a language, the script or program will still work correctly.

Why then should we follow coding conventions? The conventions are designed to help you and anyone else who will ever have to read your code. By following these conventions, people who read your code are better able to understand its purpose, and even spot errors, without having to follow the code step by step.

It is important to note that mIRC is an interpreted language and format matters more here than in most languages. Bad formatting can actually cause your code to not work, when it is otherwise inerrant.

Indenting and Code Blocks

This convention is essentially the same in every coding language. The idea is to use indentions to group code that belongs together, usually in code blocks (between { and }) together. For example:

on *:TEXT:*:#: {
  TextCommand
  if (condition) { 
    IfCommandOne
    IfCommandTwo
  }
  else {
    ElseCommandOne
  }
}

The first part of the code is not indented. It is fully flush with the left side of the scripts editor. This is because this code is not part of any user-defined code block.

The if statement and TextCommand is indented once. This is because they are in one set of { }, or within the FIRST code block. Everything within the first code block is all part of the on text event and its alignment should be the same as everything else that is triggered given one condition: the on text event is triggered. What I mean by this is simple: Everything with just one indent is triggered if the on text event is triggered. That is the only condition.

The IfCommandOne is indented twice. It is in the second code block ({ { code } }). This command, as well as IfCommandTwo, is triggered given TWO conditions: The on text event is triggered, AND the if statement is true.

The else command is indented once. You may argue that this makes no sense, because it has two conditions: The on text event is triggered AND the if condition is false. This however is not necessarily true. mIRC runs the "else" command reguardless of the value of the if statement above it. The commands within the else, however, are not triggered.

In Summary, indentions are based on code blocks ({ }). One indention per code block. You may also say that they are based on the number of conditions that must be passed before the commands on that level of indention are reached.

Note: The scripts editor has a "Check Brackets" button in the top-right hand corner. Please use this constantly. It would adjust your indenting based on your code blocks for you, and alert you of incomplete blocks.

Calling Aliases

Aliases have two functions in mIRC: They can either simply perform a command, or they can act as a custom identifier and return data. A coder should be able to look at your code and tell whether the alias is intended to return data, or just to execute commands.

Command Aliases

alias quitReason {
 quit Bye guys! I'm out because: $1-
}

It is not correct to use $quitReason(I'm tired) This example alias does NOT return data via the return command. It should therefore be called as a command

/quitReason I'm tired

Identifier Aliases

alias quitReason {
 return I'm tired
}

It is pointless to call this as a command, such as

/quitReason

Although it is not erroneous to do so. This example alias returns a value and should always be called using $quitReason (or $quitReason())