Are you still coding IF statements the old and lengthy way? If you haven't programmed your own version of the IF statement to evaluate a statement and return one value or the other, you have stumbled onto a life-saving post!
Keep reading for a simple solution to cleaning up your code by moving your simple conditional statements into a single line.
I've included a standard conditional statement below to illustrate what I see in so many scripts:
'--DISPLAY the number of items processed to the user
response.write ("The script processed " & num_items & " item")'--NOT exactly one item?
IF (num_items <> 1) THEN'--DISPLAY 's' in message
response.write ("s")END IF
'--DISPLAY end of items processed message
response.write (" successfully.")
This is a simple example of a VERY basic conditional statement that prints any of the following outputs to the screen:
The script processed 7 items successfully.
The script processed 1 item successfully.
The script processed 0 items successfully.
Disregarding the comments and line-breaks, there are 5 lines of code required to implement this conditional statement.
I've also seen programmers resort to combining the IF onto a single (yet still separate) line:
'--DISPLAY the number of items processed to the user
response.write ("The script processed " & num_items & " item")'--NOT exactly one item?: DISPLAY 's' in message
IF (num_items <> 1) THEN response.write ("s")'--DISPLAY end of items processed message
response.write (" successfully.")
When I see this in code, I understand the need to condense a conditional statement that is SO simple, but hold on! There IS a better way! The following function can be used to return one of two values, depending on the outcome of a provided condition.
FUNCTION iftrue(byval var1, byval var2, byval var3)
IF (var1) THEN
iftrue = var2
ELSE
iftrue = var3
END IF
END FUNCTION
Once you've saved the above function in your standard functions file (if you don't have one, create one and include it everywhere), you can write the following:
'--DISPLAY the number of items processed to the user
response.write ("The script processed " & num_items & " item" & iftrue(num_items <> 1, "s", "") & " successfully.")
With this simple function in your tool arsenal, you can accomplish amazingly simple code! The few drawbacks to using this function for complicated operations are:
1.) Both values are evaluated as strings and passed to the operation.
2.) This method abstracts the code, possibly complicating the code readability for other developers not familiar w/ your defined functions.
The benefits will however far outweigh the disadvantages as you begin to reuse methods such as this one throughout your code. My personal suite of standard functions includes more than 50 of these nifty little functions that will put your code on a massive diet, reducing the number of lines and simplifying the process of rapidly development.
Over the next few weeks I will post dozens of articles containing my pre-built standard functions with complete explanations. You will see functions like nvl(), void(), contains_invalid_chars(), remove_invalid_chars(), format(), fix_date(), pcase(), replace2(), cstr2(), len2(), byte_size(), lpad(), rpad(), and many more, including valuable list functions for searching and retrieving values fast from delimited cached values.

Comments