Tải bản đầy đủ (.pdf) (5 trang)

SQL PROGRAMMING STYLE- P39 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (69.99 KB, 5 trang )


2.1 Typography and Code 27

See how quickly you can find each clause, reading from left to right?
Next, if you put each clause on a line of its own, you can read the code
still faster:

SELECT a, b, c
FROM Foobar
WHERE flob = 23;

We will deal with rules for the vertical components later.

Exceptions:

None
Keywords come in two types, reserved and nonreserved words. The
reserved words are part of the SQL language; the nonreserved words are
metadata names that appear in the environment and will not cause
syntax errors in an actual SQL program. They are also not very likely to
be used in a real application.

<key word> ::= <reserved word> | <non-reserved word>
<non-reserved word> ::=
ADA
| C | CATALOG_NAME | CHARACTER_SET_CATALOG | CHARACTER_SET_NAME
| CHARACTER_SET_SCHEMA | CLASS_ORIGIN | COBOL |
COLLATION_CATALOG
| COLLATION_NAME | COLLATION_SCHEMA | COLUMN_NAME |
COMMAND_FUNCTION
| COMMITTED


| CONDITION_NUMBER | CONNECTION_NAME | CONSTRAINT_CATALOG
| CONSTRAINT_NAME
| CONSTRAINT_SCHEMA | CURSOR_NAME
| DATA | DATETIME_INTERVAL_CODE
| DATETIME_INTERVAL_PRECISION | DYNAMIC_FUNCTION
| FORTRAN
| LENGTH
| MESSAGE_LENGTH | MESSAGE_OCTET_LENGTH | MESSAGE_TEXT | MORE |
MUMPS
| NAME | NULLABLE | NUMBER
| PASCAL | PLI
| REPEATABLE | RETURNED_LENGTH | RETURNED_OCTET_LENGTH
| RETURNED_SQLSTATE

28 CHAPTER 2: FONTS, PUNCTUATION, AND SPACING

| ROW_COUNT
| SCALE | SCHEMA_NAME | SERIALIZABLE | SERVER_NAME |
SUBCLASS_ORIGIN
| TABLE_NAME | TYPE
| UNCOMMITTED | UNNAMED
<reserved word> ::=
ABSOLUTE | ACTION | ADD | ALL | ALLOCATE | ALTER | AND
| ANY | ARE | AS | ASC
| ASSERTION | AT | AUTHORIZATION | AVG
| BEGIN | BETWEEN | BIT | BIT_LENGTH | BOTH | BY
| CASCADE | CASCADED | CASE | CAST | CATALOG | CHAR | CHARACTER
| CHAR_LENGTH
| CHARACTER_LENGTH | CHECK | CLOSE | COALESCE | COLLATE |
COLLATION

| COLUMN | COMMIT | CONNECT | CONNECTION | CONSTRAINT
| CONSTRAINTS | CONTINUE
| CONVERT | CORRESPONDING | COUNT | CREATE | CROSS | CURRENT
| CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP |
CURRENT_USER
| CURSOR
| DATE | DAY | DEALLOCATE | DEC | DECIMAL | DECLARE | DEFAULT
| DEFERRABLE
| DEFERRED | DELETE | DESC | DESCRIBE | DESCRIPTOR | DIAGNOSTICS
| DISCONNECT | DISTINCT | DOMAIN | DOUBLE | DROP
| ELSE | END | END-EXEC | ESCAPE | EXCEPT | EXCEPTION
| EXEC | EXECUTE | EXISTS
| EXTERNAL | EXTRACT
| FALSE | FETCH | FIRST | FLOAT | FOR | FOREIGN | FOUND | FROM
| FULL
| GET | GLOBAL | GO | GOTO | GRANT | GROUP
| HAVING | HOUR
| IDENTITY | IMMEDIATE | IN | INDICATOR | INITIALLY | INNER |
INPUT
| INSENSITIVE | INSERT | INT | INTEGER | INTERSECT | INTERVAL |
INTO
| IS
| ISOLATION
| JOIN
| KEY
| LANGUAGE | LAST | LEADING | LEFT | LEVEL | LIKE | LOCAL |
LOWER

2.1 Typography and Code 29


| MATCH | MAX | MIN | MINUTE | MODULE | MONTH
| NAMES | NATIONAL | NATURAL | NCHAR | NEXT | NO | NOT | NULL
| NULLIF | NUMERIC
| OCTET_LENGTH | OF | ON | ONLY | OPEN | OPTION | OR
| ORDER | OUTER
| OUTPUT | OVERLAPS
| PAD | PARTIAL | POSITION | PRECISION | PREPARE | PRESERVE |
PRIMARY
| PRIOR | PRIVILEGES | PROCEDURE | PUBLIC
| READ | REAL | REFERENCES | RELATIVE | RESTRICT | REVOKE |
RIGHT
| ROLLBACK | ROWS
| SCHEMA | SCROLL | SECOND | SECTION | SELECT | SESSION
| SESSION_USER | SET
| SIZE | SMALLINT | SOME | SPACE | SQL | SQLCODE | SQLERROR |
SQLSTATE
| SUBSTRING | SUM | SYSTEM_USER
| TABLE | TEMPORARY | THEN | TIME | TIMESTAMP | TIMEZONE_HOUR
| TIMEZONE_MINUTE
| TO | TRAILING | TRANSACTION | TRANSLATE | TRANSLATION | TRIM
| TRUE
| UNION | UNIQUE | UNKNOWN | UPDATE | UPPER | USAGE | USER |
USING
| VALUE | VALUES | VARCHAR | VARYING | VIEW
| WHEN | WHENEVER | WHERE | WITH | WORK | WRITE
| YEAR
| ZONE

Vendors will also have proprietary reserved words, which should also
be capitalized.


2.1.5 Avoid the Use of CamelCase

Rationale:

The eye tends to look for a word in its usual lowercase or capitalized
form, so CamelCase words tend to lead the eye to the pieces rather than
to the whole word. In particular, a CamelCase word that begins with a
lowercase letter will be scanned starting at the first uppercase letter and
then scanned backward to get the first syllable.
Another problem is that you need to agree on how to mix the cases.
For example, should it be “upcCode,” “UpcCode,” “UPCcode,” or

30 CHAPTER 2: FONTS, PUNCTUATION, AND SPACING

“UPCCode”? In practice, you can wind up with several versions of the
same name.
It is even more difficult to read text in alternating case; that is, where
the letters of a word change from uppercase to lowercase multiple times
within a word (e.g., “AlTeRnAtInG cAsE”). The bouma shape is different
from the same word in its lowercase form. Alternating case has been
shown to be more difficult than either lowercase or uppercase text in a
variety of studies.
Smith (1969) showed that it slowed the reading speed of a passage of
text. Mason (1978) showed that the time to name a word was slowed.
Pollatsek, Well, and Schindler (1975) showed that word matching
was hindered. Meyer and Gutschera (1975) showed that category
decision times decreased.

Exceptions:


If the word naturally appears in CamelCase, such as “MacDonald,” then
use it. If you begin the object name with an uppercase letter, then you
can optionally use it. However, never use CamelCase for a scalar.

2.2 Word Spacing

Put one space between language tokens and do not jam things into a
stream. For example, do write “foobar = 21” instead of “foobar=21,” as
you will often see. Many programmers who grew up with punchcards
were taught to use minimal white space to save the limited number of
columns. For example, FORTRAN II does not need any spaces at all in
its code, nor does the original IBM job control language (JCL) for the
IBM/360 family. Modern programming languages are not this restricted,
and we now have the ability to write code as if people were more
important than computers.

Rationale:

We are now living in the 21st century, and you can add white space for
readability without running over the edge. That is a screen and not a
punchcard in front of you.

Exceptions:

You might have to wrap exceptionally long lines. This is not as big a
problem in a concise language like SQL as it was in a verbose language
like COBOL.

2.3 Follow Normal Punctuation Rules 31


2.3 Follow Normal Punctuation Rules

Rationale:

Try to follow the rules that you would for English punctuation, because
people are used to reading English and their eyes expect certain
conventions.
1. In SQL in particular, you need to follow the rule about having a
space after a comma because the comma and the period are
easy to confuse or to miss visually.
Compare:

SELECT flobs.a,flobs.b,flobs.c,fleq.d
FROM Flobs,Foobar,Fleq;

versus

SELECT flobs.a, flobs.b, flobs.c, fleq.d
FROM Flobs, Foobar, Fleq;

2. Put commas at the end of a line, not the start. A comma,
semicolon, question mark, or periods are visual signals that
something has just ended, not that it is starting. Having a
comma at the start of a line will make the eye tick leftward as it
looks for that missing word that was expected before the
comma.

SELECT flobs.a
,flobs.b

,flobs.c
,fleq.d
FROM Flobs
,Fleq
;

Instead, put comma-separated lists on one line so they can be
read left to right instead of vertically. If you split the list into
two or more lines, see that each line contains related data
elements.

SELECT flobs.a, flobs.b, flobs.c, related group
fleq.d
FROM Flobs, Fleq;

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×