SQL PROGRAMMING STYLE- P40 docx

5 162 0
SQL PROGRAMMING STYLE- P40 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 2 Fonts, Punctuation, and Spacing C ODE IS USUALLY set in a monospace font. After more than a century of manual typewriters and decades of punchcards, we find that it is actually easier to read code in a monospace font than a proportional font. Punctuation marks get the same spacing as a letter in a monospace font, but would be lost in a proportional font. 2.1 Typography and Code Your brain and eyes do not follow code the same way that they follow text, process mathematics, read maps, or look at pictures. In fact, there are a lot of individual differences in human brains. Some people like text editors that use colors for various syntax elements in a programming language. Other people get headaches from colored program editors and want to see black-and-white text. Likewise, a newspaper that put nouns in red, verbs in green, and other such things would simply not work. Yet black-and-white maps are much more difficult to read than those with colors. Why? This has to do with color perception and how fast you can switch between the left and right halves of your brain. There is a test for brain damage in which the examiner flashes cards with words printed in various colored inks (e.g., the word “RED” written in green ink). The examiner asks the subject for the word or 24 CHAPTER 2: FONTS, PUNCTUATION, AND SPACING the color and times the responses. The rate is fairly constant over the subject’s lifetime, so a change is a symptom of some physical or chemical change. Now, try reading this phrase: Paris in the the Spring. Almost nobody reading this for the first time catches the fact that the word “the” appears twice. The point is that there is a vertical component to how we read text in chunks of words. Code on a page is read from left to right and from top to bottom, with a lot of vertical eye movement that you would not have if you were reading pure text. A few years ago, the following posting made the rounds in newsgroups. I am not sure if it is genuinely from Cambridge University, but it makes its point very nicely: Aoccrdnig to rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, the only iprmo- etnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a total mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid does not raed ervey lteter by istlef, but the wrod as a wlohe. Because the parser guarantees that running code will not have syntax and spelling errors like those in the above text, the reader knows what token to expect next with far more certainty than in plain text. Not only are words seen as wholes, but they are also anticipated within each statement in the programming language. That is, if I see an “IF” token in Pascal or another member of the Algol family, I anticipate the matching “THEN” that completes the statement. Let’s discuss some basic typographic conventions for programming code, which are based on how people read it. 2.1 Typography and Code 25 2.1.1 Use Only Upper- and Lowercase Letters, Digits, and Underscores for Names Rationale: This subset of characters will port to any other programming language. It is very handy to be able to use the same names in both the database and the host languages of the applications. For example, the octothrope or number sign (#) is allowed in several SQL products, but it has a special meaning in other programming languages and could not be used in them. Exceptions: If you are still programming on a machine that uses punchcards, then you have no choice but to use the limited, uppercase-only character. It is hard to imagine such a situation in the 21st century. If the SQL implementation requires special symbols for certain names, then you have no choice. For example, temporary table names begin with an octothrope and parameter names begin with a “petite snail” or “at sign” (@) in Sybase/SQL Server T-SQL dialects. However, it is a good idea to be sure that the names are unique without the special characters, so you can port the code to a more modern implementation. Do not use an underscore as the first or last letter in a name. It looks like the name is missing another component. Leading or trailing underscores also get lost visually without letters or digits around them, thanks to laser-quality printers. Likewise, do not use more than one underscore in a row. The old mechanical line printers could not align underscores, so you could eyeball them, whereas laser printers are microscopically precise. 2.1.2 Lowercase Scalars Such as Column Names, Parameters, and Variables Rationale: Words in books and newspapers are written in lowercase letters because they are easier to read than uppercase words. This is basic typography. Using all uppercase letters is the worst choice. Lowercase text is also read faster than uppercase text. The first measurements are in Woodworth (1938), and Smith and Fisher (1975) have confirmed it. Participants were asked to read comparable passages of text, half completely in uppercase text and half presented in standard lowercase text. In each study, participants read reliably faster with the lowercase text by a 5 percent to 10 percent speed difference. 26 CHAPTER 2: FONTS, PUNCTUATION, AND SPACING Exceptions: Unless there is a compelling physical reason, use lowercase. The only compelling physical reason I can think of is that you are still using punchcards in the 21st century. 2.1.3 Capitalize Schema Object Names Rationale: Schema objects include tables, views, stored procedures, and so forth. Capitalized words begin a sentence in languages that use the Latin alphabet. Additionally, capitalization represents proper nouns—like the names of sets being modeled by tables in SQL—in English, German, and other natural languages. This is the way that readers expect to see these names; don’t surprise them. Exceptions: Unless the name naturally begins with a lowercase letter, there is no reason not to capitalize it. 2.1.4 Uppercase the Reserved Words Rationale: Uppercase words are seen as a unit, rather than being read as a series of syllables or letters. The eye is drawn to them, and they act to announce a statement or clause. That is why headlines and warning signs work. Typographers use the term bouma for the shape of a word. The term appears in Paul Saenger’s book (1975). Imagine each letter on a rectangular card that just fits it, so you see the ascenders, descenders, and baseline letters as various-sized “Lego blocks” that are snapped together to make a word. The bouma of an uppercase word is always a simple, dense rectangle, and it is easy to pick out of a field of lowercase words. Consider this statement: Select a, b, c from foobar where flob = 23; versus: SELECT a, b, c FROM Foobar WHERE flob = 23; . (#) is allowed in several SQL products, but it has a special meaning in other programming languages and could not be used in them. Exceptions: If you are still programming on a machine that. an octothrope and parameter names begin with a “petite snail” or “at sign” (@) in Sybase /SQL Server T -SQL dialects. However, it is a good idea to be sure that the names are unique without the. matching “THEN” that completes the statement. Let’s discuss some basic typographic conventions for programming code, which are based on how people read it. 2.1 Typography and Code 25 2.1.1 Use

Ngày đăng: 06/07/2014, 23:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan