PDA

View Full Version : Use of custom equates



marcuslee
02-09-2008, 16:37
I understand the use of constants, and I think I understand the use of predefined equates. But, I wanted to clarify in my own mind the use of custom equates, such as this example that I found in the forums:


%id_button = 10 'assign number to button to identify it

Which corresponds to this button:


control add button, hDlg, %id_button, "CLICK ME", 5, 5, 50, 50, %ws_border

If I understand correctly, the second line could have been written this way:


control add button, hDlg, 10, "CLICK ME", 5, 5, 50, 50, %ws_border

So, the use of this custom equate is to make the code easier to read? Is there any other purpose?

Mark

ErosOlmi
02-09-2008, 16:44
The main purposes of constants (we call them equates), in my opinion, is not readability but code maintainability.

Going on with your code, you will refer to that button other times, for example when you will have to check which control fired a %WM_COMMAND message to the window. If you used a number and later you will need to recode it for any reason, you will have to search all the places where you have used that number and change it. If the script is complex (so far the most complex script I've see is about 11000 lines) you will have a lot of work and quite sure you will make some error. With an equate you will just change it once.

Of course with equates, code readability will also get a lot of benefit. Unless you are just writing a little code test, you will see that going back to your code after some time will greatly benefit from equates usage.

The above is valid whatever programming language you will use.

Ciao
Eros

ErosOlmi
02-09-2008, 16:59
I would like to add also the same indication when defining variable names.

What about

IF N > 100 THEN W = %TRUE
compared with


%ItemsPerPage = 100
IF NumberOfItems > %ItemsPerPage THEN Wait = %TRUE

At first sight first example seems OK, why bother about it?
Read it after 1 day or after a week ;)
Or sent it to someone else for sharing, maintenance or update/upgrade.

Ciao
Eros

marcuslee
02-09-2008, 17:05
I would like to add also the same indication when defining variable names.

At first sight first example seems OK, why bother about it?
Read it after 1 day or after a week ;)
Or sent it to someone else for sharing, maintenance or update/upgrade.


I know what you mean. I have a VBA excel spreadsheet system I use for my business. I created the code in stages. As it grew, I discovered that some of my variable names where hard to read for this very reason. I caught the important ones in time, though. I did leave local variables that were only used for a For/Next loop. For example:



For x = 1 to MaxCounter
'some code
Next x


Mark