General comments to be written
| Coder's Crib Sheet | Top |
Problem. You are usually focused on the big picture and the logic you are implementing. You end up wasting time finding and fixing compiler errors -- nitty gritty mistakes in command syntax and the like. It's typically worse if you have been away from coding for weeks or months, or if you are working with more than one language on a project.
Solution. The Crib Sheet is designed for lookup of command syntax within 5 seconds. A little test driving is useful to gain sufficient familiarity with the Crib Sheet to find a command or topic quickly.
Tips
| Naming Conventions for Variables | Top |
Problem. You don't know (or remember) the data type of a variable. Not knowing which commands are applicable to the variable leads to coding errors. Perhaps the variable's name doesn't make it clear and no nearby comment is helpful. You are working in one of the loosely structured languages I feature on this website, so you have no data definition to refer to, unlike in C or Java. While I can usually tell a string from a number, and rarely need to differentiate which type of number (integer or floating point) a numerical variable is, less common data types pose challenges to rapid, bug-free coding.
Solution. I use coded prefixes, as listed below.
Data type prefixes
Multi-dimensional examples ... left-most character is the outer dimension
Data source/location prefixes
Examples combining data types and source/locations (I write all variables with a leading "$".)
| Code Structure Before Content | Top |
Problem. Runaway code -- notably unclosed parentheses or curly braces -- cause compiler errors that can be time consuming and frustrating to run down.
Solution. Code the structure first, then back up to fill in content. I typically lay out a structure like the following before filling in any content.
if () {
}
else () {
}
My new text editor, PSPad, is helpful in this regard. When you type the first character of a structural pair -- quotation mark, parenthesis, curly brace, angle bracket (for HTML tag) -- PSPad automatically adds the associated closing character. It can't know, for example, that you intend to add an "else" clause as part of a compound "if" clause.
| Advanced Comments | Top |
| Using Quotation Marks Wisely | Top |
You know, of course, that double quotes are interpolating, whereas single quotes are not. With this principle in mind, I follow two guidelines in my use of quotes.
I use single quotes unless I need double quotes. The benefit is that when I'm skimming my code for possible problems, I can safely ignore all content within single quotes. I don't carry this to extremes however -- preferring "a backslash isn't needed" to 'a backslash isn\'t needed'.
Quotes are essentially clutter, adversely affecting the readability of your code. When a string contains many quotes, notably in multi-line concatenations, a "Here" document is usually preferable. If Here documents (not available in JavaScript) are new to you, by all means befriend them.
PHP: $string = <<< SOMENAME Note triple left angle brackets. All variables are interpolated, as though this was a double quoted string. Backslash variable names -- Ex: \$var -- if embedding code snippets in a Here doc. Include as many lines as you want. SOMENAME; // starts in col 1. Ends with a semi-colon Perl: $str = << "SOMENAME"; # or 'SOMENAME'; or SOMENAME; Note double, not triple, left angle brackets. Note trailing semi-colon on first line. 'SOMENAME' is the non-interpolating version. The other two -- "SOMENAME" and SOMENAME -- are interpolating. SOMENAME # starts in col 1. No trailing semi-colon.
You already understand the basic premise behind taking the time to write comments in your code. When you return to the code later, perhaps years later, comments can be life savers in terms of getting back up to speed.
The tips below are outside-the-box uses for comments you may not have thought of yet.
Tips
Look for the line <!-- start of tips/advanced_comments.html -->, which is the top line in this file,
and the line <!-- end of tips/advanced_comments.html -->, which ends the file.
Use an inline PHP statement: <?php // comment goes here ?>.