source: trunk/docs/coding_standards.txt @ 767

Last change on this file since 767 was 767, checked in by anonymous, 23 months ago

Add App param ‘template_ext’ used to inform services where to find header and footer templates. Minor fixes.

File size: 13.9 KB
Line 
1Strangecode coding standards
229 Aug 2005 20:12:49
3
4
5======================================================================
6Preamble
7======================================================================
8
9Our standards follow closely the PEAR coding standards.
10
11Essential reading:
12http://pear.php.net/manual/en/standards.php
13
14
15=====================================================================
16General guidelines
17=====================================================================
18
19- Respect these coding styles.
20- Don't add npm dependencies if you can avoid it. If a new dependency is unavoidable, be mindful of its size, freshness and added value.
21- Use Svelte for all UI needs.
22- Try to not shoehorn your code into existing functions or components.
23- Simple is better. OOP is not inevitable. Simple functions often work as well, if not better.
24- If you must use OOP, avoid inheritance as much as possible, no one likes digging several layers of abstraction.
25- Comment the code. What, why, how, just make your intent clear.
26
27
28======================================================================
29File Naming Conventions
30======================================================================
31
32script.php              Public accessible scripts.
33
34Auth_SQL.inc.php        One PHP Class to be included. The filename is the
35                        type of class, underscore, name. Or if in subdirs,
36                        this could be /Auth/SQL.inc.php while the class name
37                        remains "Auth_SQL"
38
39some_file.inc.html      HTML file, which may or may not have some PHP,
40                        but will be included by some other PHP file. Never
41                        includes sensitive code as these files may be accessed
42                        directly in the web root.
43
44script.cli.php          A command-line executable script, possibly executed
45                        with CRON. Outputs TEXT if any instead of HTML.
46
47script.inc.eml          Text file to be sent by email.
48
49schema.mysql            Database schema file that goes with the application.
50
51main.screen.css         CSS file with media: screen/print/all
52main.print.css
53
54
55======================================================================
56Indenting and Wrap
57======================================================================
58
59Use an indent of 4 spaces, with no tabs. Code and especially comments should
60usually be wrapped <= 80 characters. Exceptions are made in the case where
61code readability is significantly improved with longer lines.
62
63
64======================================================================
65Control Structures
66======================================================================
67
68These include if, for, while, switch, etc. Here is an example if statement,
69since it is the most complicated of them:
70
71    if ((condition1) || (condition2)) {
72        action1;
73    } else if ((condition3) && (condition4)) {
74        action2;
75    } else {
76        defaultaction;
77    }
78
79Control statements should have one space between the control keyword
80and opening parenthesis, to distinguish them from function calls.
81
82Even in the case of an if with no else clauses, and only a 1 line action,
83the curly braces should still be used:
84
85    if (condition) {
86        action;
87    }
88
89This improves readability and allows easy extension of the clause.
90
91For switch statements:
92
93    switch (condition) {
94    case 1:
95        action1;
96        break;
97
98    case 2:
99        action2;
100        break;
101
102    default:
103        defaultaction;
104        break;
105
106    }
107
108
109======================================================================
110Function Calls
111======================================================================
112
113Functions should be called with no spaces between the function name,
114the opening parenthesis, and the first parameter; spaces between commas
115and each parameter, and no space between the last parameter, the
116closing parenthesis, and the semicolon. Here's an example:
117
118    $var = foo($bar, $baz, $quux);
119
120As displayed above, there should be one space on either side of an
121equals sign used to assign the return value of a function to a
122variable. In the case of a block of related assignments, more space
123may be inserted to promote readability:
124
125    $short         = foo($bar);
126    $long_variable = foo($baz);
127
128
129======================================================================
130Function and Method Definitions
131======================================================================
132
133Function declaractions follow the "one true brace" convention:
134
135    function fooFunction($arg1, $arg2 = '')
136    {
137        if (condition) {
138            statement;
139        }
140        return $val;
141    }
142
143Arguments with default values go at the end of the argument list. Always
144attempt to return a meaningful value from a function if one is appropriate.
145
146Use lowerCamelCase for function and method names to distinguish from php
147internal functions which standard on underscore_space_style_names().
148
149
150======================================================================
151Return Values
152======================================================================
153
154When functions return boolean values, use 'return false;' or 'return true;'
155as opposed to 'return 0;' or 'return 1;' or 'return(-1);', except when
156boolean plurality is necessary.
157
158
159======================================================================
160String Concatenation
161======================================================================
162
163Always include a space before and after the concatenation operator '.' which
164improves readability and improves search-and-replace of adjacent elements.
165
166    $something = $blah . funky() . ".\".=" . $blab;
167
168is better than:
169
170    $something = $blah.funky().".\".=".$blab;
171
172
173======================================================================
174Quote Marks
175======================================================================
176
177Use the single quote marks ' to enclose simple strings whenever possible.
178Double quote marks " require extra parsing and thus slow things down, but
179are necessary if entities there must be swapped-out such as variables or
180control characters.
181
182    $var['singlequote'] = 'singlequote';
183    $var["doublequote-$i"] = "$vars and \n funny \t %s things need doublequotes";
184    $var['doublequote-' . $i] = $var . 'you can do this' . "\t %s" . $var2 .
185    'but it isn\'t any better';
186
187
188======================================================================
189Printing HTML
190======================================================================
191
192For large or complex blocks of HTML, using the following method is much faster
193and safer than echo because the php processor is bypassed and content goes
194directly to output:
195
196    <?php
197    if ($something) {
198        // Here comes multi-line HTML...
199        ?>
200        <div class="sc-tiny">
201            <a href="<?php echo $app->ohref('/contact.php') ?>">Contact us</a>
202        </div>
203        <?php
204    } else if ($somethingelse) {
205        ?><h1>Just a single-line of html</h1><?php
206    }
207    ?>
208
209
210======================================================================
211Comments
212======================================================================
213
214Function comments should follow the Javadoc standard, with detailed
215function comments and one-line pointers along the way:
216http://java.sun.com/products/jdk/javadoc/writingdoccomments/index.html
217
218Here is an example of the codebase method Email::validEmail():
219
220    <?php
221    /**
222     * Validates an email address based on the recommendations in RFC 3696.
223     * Is more loose than restrictive, to allow the many valid variants of
224     * email addresses while catching the most common mistakes. Checks an array too.
225     * http://www.faqs.org/rfcs/rfc822.html
226     * http://www.faqs.org/rfcs/rfc2822.html
227     * http://www.faqs.org/rfcs/rfc3696.html
228     * http://www.faqs.org/rfcs/rfc1035.html
229     *
230     * @access  public
231     * @param   mixed  $email  Address to check, string or array.
232     * @return  bool    Validity of address.
233     * @author  Quinn Comendant <quinn@strangecode.com>
234     * @since   30 Nov 2005 22:00:50
235     */
236    function validEmail($email)
237    {
238        $app =& App::getInstance();
239
240        // If an array, check values recursively.
241        if (is_array($email)) {
242            foreach ($email as $e) {
243                if (!$this->validEmail($e)) {
244                    return false;
245                }
246            }
247            return true;
248        } else {
249            // To be valid email address must match regex and fit within the length constraints.
250            if (preg_match($this->getParam('regex'), $email, $e_parts) && mb_strlen($e_parts[2]) < 64 && mb_strlen($e_parts[3]) < 255) {
251                return true;
252            } else {
253                $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__);
254                return false;
255            }
256        }
257    }
258    ?>
259
260Single line comments should be of the double-slash variety, and be on the line above the
261code being commented upon:
262
263    <?php
264    // If the last word in $var is peanuts, set $need_peanuts to TRUE.
265    $need_peanuts = preg_match('/peanuts$/i', $var);
266    ?>
267
268
269======================================================================
270Including Code
271======================================================================
272
273If you are including a class, function library, or anything else which would
274cause a parse error if included twice, always use include_once. This will
275ensure that no matter how many factory methods we use or how much dynamic
276inclusion we do, the library will only be included once.
277
278If you are including a static filename, such as a conf file or a template
279that is _always_ used, use require.
280
281If you are dynamically including a filename, or want the code to only be
282used conditionally (an optional template), use include.
283
284
285======================================================================
286PHP Code Tags
287======================================================================
288
289Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. Even
290use <?php echo $name; ?> instead of <?=$name?>.
291
292
293======================================================================
294php.ini Settings
295======================================================================
296
297All code should work with register_globals = Off. This means using
298$_REQUEST, $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, and $_ENV
299to access all request, get, post, cookie, session, server, and
300environment data, respectively.
301
302All code should work with error_reporting = E_ALL. Failure to do so would
303result in ugly output, error logs getting filled with lots of warning messages,
304or even downright broken scripts.
305
306All code should work regardless of the setting of magic_quotes_gpc.
307Form data should be passed through stripslashes() if necessary.
308
309No code should assume that '.' is in the include path. Always
310specify './' in front of a filename when you are including a file in
311the same directory. Or better yet, for less abiguity, use:
312include dirname(__FILE__) . '/include_me.inc.php';
313
314
315======================================================================
316XHTML 1.0 Compliance
317======================================================================
318
319All HTML should be valid XHTML 1.0 verified with the
320W3C Markup Validation Service: http://validator.w3.org/
321
322All tag names and parameters must be lower case including javascript
323event handlers:
324
325    <div class="mofogo">...</div>
326    <a href="http://www.example.com/" onmouseover="status=''" onmouseout="status=''">...</a>
327
328All tag parameters must be of a valid parameter="value" form (numeric
329values must also be surrounded by quotes).  For parameters that had no
330value in HTML, the parameter name is the value.  For example:
331
332    <input type="checkbox" checked="checked" />
333    <select name="example">
334        <option selected="selected" value="1">Example</option>
335    </select>
336    <td nowrap="nowrap">Example</td>
337
338All tags must be properly closed. Tags without a closing part must follow the
339XHTML convention and end with a space and a slash:
340
341    <br />
342    <hr />
343    <img src="example.gif" alt="Example" />
344    <input type="submit" value="Example" />
345
346All form definitions must be on their own line and either fully defined within
347a <td></td> pair or be outside table tags. Forms must also always have an action
348parameter:
349
350    <form method="post" action="http://example.com/example.cgi">
351    <table>
352        <tr><td>example</td></tr>
353    </table>
354    </from>
355
356    <table>
357        <tr><td>
358            <form action="javascript:void(0)" onsubmit="return false;">
359            </form>
360        </td></tr>
361    </table>
362
363All JavaScript tags must have a valid language and type parameters:
364
365    <script language="JavaScript" type="text/javascript">
366    <!--
367    ...
368    // -->
369    </script>
370
371Nothing may appear after </html>, therefore include any common footers after
372all other output.
373
374All images must have an alt parameter:
375
376    <img src="example.gif" alt="<?php echo _("Example"); ?>" />
377
378Input field of type "image" does not allow the border parameter, and may render
379with a border on some browsers.  Use the following instead:
380
381   <a href="" onclick="document.formname.submit(); return false;"><img src="example.gif" alt="<?php echo _("Example"); ?>" /></a>
382
383
384======================================================================
385Database Naming Conventions
386======================================================================
387
388All database tables need to make sure that their table and field names work in all
389databases. Many databases reserve words like 'uid', 'user', etc. for
390internal use, and forbid words that are SQL keywords (select, where,
391etc.). Adding '_tbl' to the end of the table name is a good idea for this reason
392and also improves searching for the table names in the code.
393
394A good way to do this for field names is to make the field name
395table_name_field_name.
396
397When building queries referencing two-or-more tables always preface columns
398with the table containing the column (admin_tbl.username, location_tbl.location_address).
399This method is much preferred over using aliases of table names (a.username, l.location_address).
400
401Table names should be singular (user_tbl instead of users_tbl).
Note: See TracBrowser for help on using the repository browser.