PDA

View Full Version : How to post code examples


Darren
05 Jul 05, 05:27 PM
There are a few guidelines for posting code in this forum which will help greatly. I understand that everyone has their own preferences, but some consistency will make life easier for everyone :)

Most of the following guidelines should be used when writing code anyway, as they make it easier for you to understand and maintain your own code.

Formatting

Firstly, please use the CODE formatting for any code snippets. To do this, click on the # button when entering code, this way you get nicely recognisable code with all of its indenting retained. Alternatively, you can enter [ CODE ] at the start and [ /CODE ] at the end of your code (no spaces between the brackets and "CODE").

Indenting

Please indent your code. It makes it much easier to read.

Comments

Please put comments in your code, or accompanying the code, otherwise it will be of no use to anyone.

Use of Constants

Where possible, use constants for Group Addresses instead of numbers or meaningless tags. This way anyone can quickly get the code working without having to change tags everywhere.

Omitted Code

Where code has been omitted for clarity, indicate this using an ellipsis { ... }, or a comment such as { switch loads here }.

Variable Naming

Please attempt to give your variables meaningful names. A variable called "x" doesn't convey much information and can make code very hard to understand.

By following these guidelines, you will post pretty code like :


{ constants }
PressGroup = 5; { this triggers the PressCounter to be incremented }

{ variables }
PressCounter : integer; { this counts the presses }

{ initialisation }
PressCounter := 0;

{ module code }

{ When the button is pressed, increment the PressCounter. }
{ When the counter gets to 10, do stuff. }
if GetLightingLevel(PressGroup) = ON then
begin
PressCounter := PressCounter + 1;
if PressCounter = 10 then
begin
{ do stuff here }
end;
end;


rather than ugly (but equivalent) code like :

x : integer;
x := 0;
if GetLightingLevel("Group 5") then begin
x := x + 1; if x = 10 then begin
{ do stuff here }
end; end;


Example Projects

If you are willing to post a whole project, this would make it even easier. If you choose to do this, please post a project archive so that all of the necessary images and the C-Bus project are included. Alternatively, if you use a C-Bus project installed with the software (such as "home" or "clipsal") and you don't use any image files, you can just post the CTD file.