While reviewing discussion forum postings for sqr-info.com,
I've been noticing some definite coding customs. Some of these are a little puzzling and have the effect of unnecessarily
complicating the code.
Among the very common patterns are:
An overuse of ENCODE
The mistaken belief that BEGIN-SELECT, BEGIN-SQL and their associated SQL clauses must start in column 1 (only the actual column/field names of a BEGIN-SELECT block must start there)
The practice of surrounding the concatenation operator (||) with no
whitespace whatsoever
Concatenating literal strings
Unnecessary variable assignments
Unnecessary renaming of column values
Here is an example. Suppose that we want to select the city and state from the database and
then construct a variable containing the values in the form "City, State". Most of the
sample code I've seen follows a form similar to this:
if...
do...
do...
begin-select
CITY &city
STATE &state let $city = rtrim(&city, ' ')
let $state = &state
let $comma = ','
encode <32> into $space
let $city_state = $city||$comma||$space||$state
FROM PS_PERSONAL_DATA
WHERE...
end-select
do...
do...
end-if
But isn't the following equivalent much easier to write and read?
if...
do...
do...
begin-select
CITY
STATE
let $city_state = rtrim(&city, ' ') || ', ' || &state
FROM PS_PERSONAL_DATA
WHERE...
end-select
do...
do...
end-if
I don't know where some of these practices come from. Maybe they are left over from
rules in old versions of SQR. In any case, we can often save time, avoid bugs and make our
programs more maintainable by just taking the direct route.