Tuesday, 15 January 2013

ASP: variables naming convention


When naming variables in ASP, we should consider some conventions.

Because the way we write code is quite important for us and, possibly, other people, we do need to keep things as much readable as we can. That means, for example, we should use indents when writing procedures and functions or we should follow a common structure in code blocks.

In general we need to make things easy to read and understand.
That is why there are common naming convention for variables. And in this article we are going to explore them.

Variable data types
As you may already know, when we create a variable we usually know what type of data it will be used for. In VBScript we don't explicitly set a variable data type, however by using prefixes we can help us and others to understand what is the intended use for the variable itself.
If you read code often you know what I mean. Different is when, for example, you find code snippets on the net (variable names have in fact more imaginative forms due to the need of being more clear). But when we write code for a web application or a web site, we do need to be more, let me say, technical. And so we need to make things more clear.
There are many data types, but for each one of them, we have a three letters prefix.

Variable names
According to the above, variable names should have a non-capitalised prefix, indicating the data type, followed by a word which should be unique and understandable. That could pose a problem: how can a word be clear and immediately understandable, but at the same time not to long?
When we think about a name for a variable, we might make the mistake to use a too long word. Something easy to imagine is "strName", but sometimes that's not enough.

Summarising: we need to choose the right variable data type and then we need to imagine an appropriate short but immediately understandable word which will explain what the variable is.

The two parts, together will form the variable name.

Data type prefixes
Let's start from the data type prefixes. The following list contains the data type and the related prefix:

Data Type Prefix
Boolean bln
Byte byt
Collection object col
Currency cur
Date-time dtm
Double dbl
Error err
Integer int
Long lng
Object obj
Single sng
String str
User-defined type udt
Variant vnt
ADO command cmd
ADO connection cnn
ADO field fld
ADO parameter prm
ADO recordset rst

As you can see, the prefix is not capitalised. The prefix is then followed by the name. In this matter, it is advisable to start with a capital letter following the form "prefixName".

Ok, I think I said enough for today.
I hope you find the above info interesting. If so leave a comment below!

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.