From dd6719738936be31cdaa1758ca86d5eb14dcab3d Mon Sep 17 00:00:00 2001 From: Barry Mieny Date: Mon, 4 Oct 2010 16:33:58 +0200 Subject: Cleanup of stray spaces and tabs --- user_guide/general/styleguide.html | 124 ++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 62 deletions(-) (limited to 'user_guide/general/styleguide.html') diff --git a/user_guide/general/styleguide.html b/user_guide/general/styleguide.html index 1200a2c12..7b7d837d9 100644 --- a/user_guide/general/styleguide.html +++ b/user_guide/general/styleguide.html @@ -107,12 +107,12 @@ Style Guide a UTF-8 encoded file, and the BOM can have a negative side effect in PHP of sending output, preventing the application from being able to set its own headers. Unix line endings should be used (LF).

- +

Here is how to apply these settings in some of the more common text editors. Instructions for your text editor may vary; check your text editor's documentation.

- +
TextMate
- +
  1. Open the Application Preferences
  2. Click Advanced, and then the "Saving" tab
  3. @@ -121,9 +121,9 @@ Style Guide
  4. Optional: Check "Use for existing files as well" if you wish to modify the line endings of files you open to your new preference.
- +
BBEdit
- +
  1. Open the Application Preferences
  2. Select "Text Encodings" on the left.
  3. @@ -137,7 +137,7 @@ Style Guide

    PHP Closing Tag

    -

    The PHP closing tag on a PHP document ?> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced +

    The PHP closing tag on a PHP document ?> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should OMIT the closing PHP tag, and instead use a comment block to mark the end of file and it's location relative to the application root. This allows you to still identify a file as being complete and not truncated.

    @@ -158,7 +158,7 @@ echo "Here's my code!";
    - +

    Class and Method Naming

    Class names should always have their first letter uppercase, and the constructor method should match identically. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names.

    @@ -193,8 +193,8 @@ function get_the_file_properties_from_the_file() // wordy function get_file_properties() // descriptive, underscore separator, and all lowercase letters
    - - + +

    Variable Names

    The guidelines for variable naming is very similar to that used for class methods. Namely, variables should contain only lowercase letters, use underscore separators, and be reasonably named to indicate their purpose and contents. Very short, non-word variables should only be used as iterators in for() loops.

    @@ -213,8 +213,8 @@ $group_id $last_city
    - - + +

    Commenting

    In general, code should be commented prolifically. It not only helps describe the flow and intent of the code for less experienced programmers, but can prove invaluable when returning to your own code months down the line. There is not a required format for comments, but the following are recommended.

    @@ -257,8 +257,8 @@ $parts = explode("\n", $str); $parts = $this->foo($parts);
    - - + +

    Constants

    Constants follow the same guidelines as do variables, except constants should always be fully uppercase. Always use CodeIgniter constants when appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.

    @@ -275,8 +275,8 @@ SUPER_CLASS_VERSION $str = str_replace(LD.'foo'.RD, 'bar', $str);
    - - + +

    TRUE, FALSE, and NULL

    TRUE, FALSE, and NULL keywords should always be fully uppercase.

    @@ -290,9 +290,9 @@ if ($foo == TRUE) $bar = FALSE; function foo($bar = NULL)
    - - + +

    Logical Operators

    Use of || is discouraged as its clarity on some output devices is low (looking like the number 11 for instance). @@ -310,9 +310,9 @@ if ( ! $foo) if ( ! is_array($foo))

    - - - + + +

    Comparing Return Values and Typecasting

    Some PHP functions return FALSE on failure, but may also have a valid return value of "" or 0, which would evaluate to FALSE in loose comparisons. Be explicit by comparing the variable type when using these return values in conditionals to ensure the return value is indeed what you expect, and not a value that has an equivalent loose-type evaluation.

    @@ -350,27 +350,27 @@ function build_string($str = "") $str = (string) $str; // cast $str as a string
    - - + +

    Debugging Code

    No debugging code can be left in place for submitted add-ons unless it is commented out, i.e. no var_dump(), print_r(), die(), and exit() calls that were used while creating the add-on, unless they are commented out.

    // print_r($foo);
    - - + +

    Whitespace in Files

    No whitespace can precede the opening PHP tag or follow the closing PHP tag. Output is buffered, so whitespace in your files can cause output to begin before CodeIgniter outputs its content, leading to errors and an inability for CodeIgniter to send proper headers. In the examples below, select the text with your mouse to reveal the incorrect whitespace.

    INCORRECT:

    - + <?php // ...there is whitespace and a linebreak above the opening PHP tag // as well as whitespace after the closing PHP tag -?> +?>

    CORRECT:

    <?php @@ -378,15 +378,15 @@ function build_string($str = "") ?>
    - - + +

    Compatibility

    Unless specifically mentioned in your add-on's documentation, all code must be compatible with PHP version 4.3+. Additionally, do not use PHP functions that require non-default libraries to be installed unless your code contains an alternative method when the function is not available, or you implicitly document that your add-on requires said PHP libraries.

    - - + +

    Class and File Names using Common Words

    When your class or filename is a common word, or might quite likely be identically named in another PHP script, provide a unique prefix to help prevent collision. Always realize that your end users may be running other add-ons or third party PHP scripts. Choose a prefix that is unique to your identity as a developer or company.

    @@ -402,8 +402,8 @@ class Pre_xml ext.pre_xml.php class Pre_import mod.pre_import.php
    - - + +

    Database Table Names

    Any tables that your add-on might use must use the 'exp_' prefix, followed by a prefix uniquely identifying you as the developer or company, and then a short descriptive table name. You do not need to be concerned about the database prefix being used on the user's installation, as CodeIgniter's database class will automatically convert 'exp_' to what is actually being used.

    @@ -419,30 +419,30 @@ exp_pre_email_addresses

    NOTE: Be mindful that MySQL has a limit of 64 characters for table names. This should not be an issue as table names that would exceed this would likely have unreasonable names. For instance, the following table name exceeds this limitation by one character. Silly, no? exp_pre_email_addresses_of_registered_users_in_seattle_washington

    - - + +

    One File per Class

    Use separate files for each class your add-on uses, unless the classes are closely related. An example of CodeIgniter files that contains multiple classes is the Database class file, which contains both the DB class and the DB_Cache class, and the Magpie plugin, which contains both the Magpie and Snoopy classes.

    - - + +

    Whitespace

    Use tabs for whitespace in your code, not spaces. This may seem like a small thing, but using tabs instead of whitespace allows the developer looking at your code to have indentation at levels that they prefer and customize in whatever application they use. And as a side benefit, it results in (slightly) more compact files, storing one tab character versus, say, four space characters.

    - - + +

    Line Breaks

    Files must be saved with Unix line breaks. This is more of an issue for developers who work in Windows, but in any case ensure that your text editor is setup to save files with Unix line breaks.

    - - + +

    Code Indenting

    Use Allman style indenting. With the exception of Class declarations, braces are always placed on a line by themselves, and indented at the same level as the control statement that "owns" them.

    @@ -498,12 +498,12 @@ for ($i = 0; $i < 10; $i++) } }
    - +

    Bracket and Parenthetic Spacing

    In general, parenthesis and brackets should not use any additional spaces. The exception is that a space should always follow PHP control structures that accept arguments with parenthesis (declare, do-while, elseif, for, foreach, if, switch, while), to help distinguish them from functions and increase readability.

    - + INCORRECT: $arr[ $foo ] = 'foo'; @@ -514,13 +514,13 @@ $arr[$foo] = 'foo'; // no spaces around array keys INCORRECT: function foo ( $bar ) { - + } CORRECT: function foo($bar) // no spaces around parenthesis in function declarations { - + } @@ -531,9 +531,9 @@ CORRECT: foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis
    - - - + + +

    Localized Text

    Any text that is output in the control panel should use language variables in your lang file to allow localization.

    @@ -544,9 +544,9 @@ return "Invalid Selection"; CORRECT: return $this->lang->line('invalid_selection');
    - - + +

    Private Methods and Variables

    Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.

    @@ -554,9 +554,9 @@ return $this->lang->line('invalid_selection'); convert_text() // public method _convert_text() // private method
    - - + +

    PHP Errors

    Code must run error free and not rely on warnings and notices to be hidden to meet this requirement. For instance, never access a variable that you did not set yourself (such as $_POST array keys) without first checking to see that it isset().

    @@ -574,9 +574,9 @@ _convert_text() // private method

    NOTE: Setting the display_errors setting with ini_set() at runtime is not identical to having it enabled in the PHP environment. Namely, it will not have any effect if the script has fatal errors

    - - + +

    Short Open Tags

    Always use full PHP opening tags, in case a server does not have short_open_tag enabled.

    @@ -589,9 +589,9 @@ _convert_text() // private method CORRECT: <?php echo $foo; ?>
    - - + +

    One Statement Per Line

    Never combine statements on one line.

    @@ -605,9 +605,9 @@ $bar = 'that'; $bat = str_replace($foo, $bar, $bag);
    - - + +

    Strings

    Always use single quoted strings unless you need variables parsed, and in cases where you do need variables parsed, use braces to prevent greedy token parsing. You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.

    @@ -622,9 +622,9 @@ $bat = str_replace($foo, $bar, $bag); "My string {$foo}" "SELECT foo FROM bar WHERE baz = 'bag'"
    - - + +

    SQL Queries

    MySQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE, AS, JOIN, ON, IN, etc.

    @@ -645,16 +645,16 @@ $query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, fooba ORDER BY foobaz LIMIT 5, 100");
    - - + +

    Default Function Arguments

    Whenever appropriate, provide function argument defaults, which helps prevent PHP errors with mistaken calls and provides common fallback values which can save a few lines of code. Example:

    function foo($bar = '', $baz = FALSE)
    - + -- cgit v1.2.3-24-g4f1b