Coding tips

Coding tips Generic coding tips for scripters (and not only)

Here comes a small list of coding tips.
These apply to programming in general, not only to KVIrc scripting.
1. Comment your code
A well commented code is easy to maintain, and easy to read by others.
2. Indent your code
Indentation increases the code readability; this is again for you and other developers that will be going to read your code.
3. Use TABS to indent your code
...and use only tabs to indent.
Tabs are better than space since most code editors allow you to set the tab since and thus to have the indentation steps smaller or bigger.
This is really important since the indentation size is really a matter of personal taste.
Mixing spaces and tabs is Evil (tm), since it makes the code look really ugly in editors that have the tab size different than yours; in some cases the code gets really unreadable.
4. Use descriptive variable names
Using 'foo' as variable name implies tracing its semantic the next time that you're going to read the code that uses it.
This is really annoying and time-consuming, especially if the project is getting large.
Obviously using thisIsACounterVariable as name for a simple counter is also a suicide.
A good convention on variable names can speed up writing, debugging and maintaining code.
Encoding the type of the variable in the variable name might be also a good idea, but this is a matter of taste; personally I feel really well with that.
Just as example, here go my fundamental convention rules for C++:
- The type of the variable is encoded at the beginning of the variable name:
- b prefix for the boolean variables
- i prefix for signed integers
- u prefix for unsigned integers
- f and d prefixes for floating point stuff
- sz prefix for strings (this is rather for string classes)
- ...
- Pointers have a p prefix prepended
- Global variables start with a g_ prefix
- Member variables start with a m_ prefix
- Exception comes for local variables with obvious semantics
- i,j,k,l for local loop counters
- aux and tmp for local obvious short-term temporary variables
So actually by only reading g_pszQuitMessage I know that this is a global pointer to a string variable containing a quit message. :)

Index, Miscellaneous