Changes between Initial Version and Version 1 of Convention_de_codage


Ignore:
Timestamp:
Jul 7, 2006, 9:18:41 PM (18 years ago)
Author:
Daimonos Tereutes
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Convention_de_codage

    v1 v1  
     1Cette page est un copier/coller de ce qui se trouve sur le forum, même pas wikifié
     2
     3[color=green]Ceci est un premier jet, et une traduction incomplète, les suggestions sont bienvenues[/color]
     4Inspiré de [url=http://gforge.org/docman/view.php/1/2/coding-standards.html]gforge[/url]
     5
     61. Commentaires
     7
     8Guidelines
     9
     10Non-documentation comments are strongly encouraged. A general rule of thumb is that if you look at a section of code and think "Wow, I don't want to try and describe that", you need to comment it before you forget how it works.
     11
     12    * C++ style comments (/* */) and standard C comments (//) are both acceptable.
     13    * Use of perl/shell style comments (#) is prohibited.
     14
     15PHPdoc Tags
     16
     17Inline documentation for classes should follow the PHPDoc convention, similar to Javadoc. More information about PHPDoc can be found here:
     18
     19http://www.phpdoc.de/
     20
     21File Comments:
     22
     23Every file should start with a comment block describing its purpose, version, author and a copyright message. The comment block should be a block comment in standard JavaDoc format along with a CVS Id tag. While all JavaDoc tags are allowed, only the tags in the examples below will be parsed by PHPdoc.
     24
     25Tous les fichier de nainwak doivent porter la mention de copyright de l'association:
     26[code]/**
     27 *
     28 * brief description.
     29 * long description.  more long description.
     30 *
     31 * @author Haiken
     32 * @author Codeur phantome <on.ne.saura.jamais@nainwak.com>
     33 * @copyright 2002-2005 (c) association Nainwak
     34 *
     35 * @version  Reloaded 1.0
     36 *
     37 */[/code]
     38
     39Function and Class Comments:
     40
     41Similarly, every function should have a block comment specifying name, parameters, return values, and last change date.
     42
     43[code]/**
     44 * brief description.
     45 * long description.  more long description.
     46 *
     47 * @author    firstname lastname email
     48 * @param     variable  description
     49 * @return    value     description
     50 * @date      YYYY-MM-DD
     51 * @deprecated
     52 * @see
     53 *
     54 */[/code]
     55
     56Note
     57
     58The placement of periods in the short and long descriptions is important to the PHPdoc parser. The first period always ends the short description. All future periods are part of the long description, ending with a blank comment line. The long comment is optional.
     59
     602. Formatting
     61
     62Indentation
     63
     64Toute l'indentation doit être faite avec 2 ou 3 espaces et [b]sans tabulation[/b].
     65
     66Les balises PHP
     67
     68Il est impératif de délimiter le code PHP avec <?php ?> . L'usage de <? ?> est incorrect. C'est la manière la plus portable d'inclure du code PHP. De plus, les parseurs XML peuvent être trompés avec la syntaxe courte.
     69
     704. Expressions
     71
     72    * Use parentheses liberally to resolve ambiguity.
     73    * Using parentheses can force an order of evaluation. This saves the time a reader may spend remembering precedence of operators.
     74    * Don't sacrifice clarity for cleverness.
     75    * Write conditional expressions so that they read naturally aloud.
     76    * Sometimes eliminating a not operator (!) will make an expression more understandable.
     77    * Keep each line simple.
     78    * The ternary operator (x ? 1 : 2) usually indicates too much code on one line. if... else if... else is usually more readable.
     79
     805. Functions
     81
     82Function Calls
     83
     84Functions shall be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:
     85
     86$var = foo($bar, $baz, $quux);
     87
     88As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:
     89
     90$short          = foo($bar);
     91$long_variable  = foo($baz);
     92
     93Définition des Fonctions
     94
     95Les déclaration de fonction doivent suivre la convention unix:
     96
     97[code]function fooFunction($arg1, $arg2 = '') {
     98    if (condition)
     99   {
     100        statement;
     101    }
     102    return $val;
     103}[/code]
     104
     105Les Arguments avec de valeurs par défaut vont à la fin de la liste d'arguments. Lorsque c'est approprié, les fonctions doivent toujours renvoyer une valeur significative. Un example un peu plus long:
     106
     107[code]function connect(&$dsn, $persistent = false) {
     108    if (is_array($dsn))
     109    {
     110        $dsninfo = &$dsn;
     111    } else
     112    {
     113        $dsninfo = DB::parseDSN($dsn);
     114    }
     115
     116    if (!$dsninfo || !$dsninfo['phptype'])
     117    {
     118        return $this->raiseError();
     119    }
     120    return true;
     121}[/code]
     122
     1236. Objects
     124
     125Objects should generally be "normalized" similar to a database so they contain only the attributes that make sense.
     126
     127Each object should have Error.class as the abstract parent object unless the object or its subclasses will never produce errors.
     128
     129Each object should also have a create() method which does the work of inserting a new row into the database table that this object represents.
     130
     131An update() method is also required for any objects that can be changed. Individual set() methods are generally not a good idea as doing separate updates to each field in the database is a performance bottleneck.
     132
     133fetchData() and getId() are also standard in most objects. See the tracker codebase for specific examples.
     134
     135Common sense about performance should be used when designing objects.
     136
     1377. Naming
     138
     139    * Constants should always be uppercase, with underscores to separate words. Prefix constant names with the name of the class/package they are used in. For example, the constants used by the DB:: package all begin with "DB_".
     140    * True and false are built in to the php language and behave like constants, but should be written in lowercase to distinguish them from user-defined constants.
     141    * Function names should suggest an action or verb: updateAddress, makeStateSelector
     142    * Variable names should suggest a property or noun: UserName, Width
     143    * Use pronounceable names. Common abbreviations are acceptable as long as they are used the same way throughout the project.
     144    * Be consistent, use parallelism. If you are abbreviating number as 'num', always use that abbreviation. Don't switch to using no or nmbr.
     145    * Database tables should be named with underscores _ between words like: my_table and indexes on tables should be named with the table name first with the underscores removed, then field names mytable_field1field2.
     146    * Use descriptive names for variables used globally, use short names for variables used locally.
     147
     148[code]      $AddressInfo = array(...);
     149
     150      for($i=0; $i < count($list); $i++)[/code]
     151
     1528. Structures de contrôle
     153
     154These include if, for, while, switch, etc. Here is an example if statement, since it is the most complicated form:
     155
     156[code]if ((condition1) || (condition2))
     157{
     158    action1;
     159} elseif ((condition3) && (condition4))
     160{
     161    action2;
     162} else
     163{
     164    defaultaction;
     165}[/code]
     166
     167Les structures de controle devraient avoir une espace entre le mot clef et la parenthèse ouvrante, afin de les distinguer des appels de fonction.
     168
     169Vous devez impérativement utiliser les accolades, même lorsque techniquement, elle ne sont pas requises. Leur présence augmente la lisibilité et réduit la probabilité que des erreurs de logique apparaissent à la suite d'ajout de lignes.
     170
     171Pour la structure switch:
     172
     173[code]switch (condition)
     174{
     175    case 1:
     176    {
     177        action1;
     178        break;
     179    }
     180    case 2:
     181    {
     182        action2;
     183        break;
     184    }
     185    default:
     186    {
     187        defaultaction;
     188        break;
     189    }
     190}[/code]
     191
     192La notation unix est tolérée, mais déconseillée, elle me semble moins claire ex:
     193
     194[code]switch (condition) {
     195    case 1: {
     196        action1;
     197        break;
     198    }
     199    case 2: {
     200        action2;
     201        break;
     202    }
     203    default: {
     204        defaultaction;
     205        break;
     206    }
     207}[/code]
     208
     209
     2109. Including PHP Files
     211
     212Anywhere you are unconditionally including a class file, use require_once. Anywhere you are conditionally including a class file (for example, factory methods), use include_once. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with require_once will not be included again by include_once. Note: include_once and require_once are keywords, not functions. Les parenthèses autour du nom de fichier à inclure sont facultatives, ceci dit il est fortement recommandé de les mettre. De plus, utilisez des ' (apostrophes) et non pas des " (guillemets):
     213
     214[code]include_once('pre.php');[/code]
     215
     21610. Structure du code
     217
     218[i]ça va viendre[/i]
     219
     22011. Requêtes (My)SQL
     221Petit Ajout pour Mysql:
     222Il est important d'utiliser toujours la même syntaxe avec mysql car dans le cas contraire, ce dernier est incapable de metre les requêtes en cache.
     223
     224Par exemple:
     225[code]SELECT * FROM BANIP[/code]
     226est différant de
     227[code]select * from BANIP[/code]
     228pour la cache de requête de mysql.
     229
     230Je recommande donc d'utiliser systématiquement la casse telle que proposée par phpMyAdmin, c'est à dire avec les mots clefs en majuscules, après avoir supprimé les éventuel retours à la ligne.
     231Ceci afin d'avoir une casse consistante. De plus c'est un bon moyen de vérifier au passage que cette requête est a peu près optimisée (cf [url=http://dev.mysql.com/doc/refman/4.1/en/explain.html]EXPLAIN[/url]).
     232
     23312. Une petite illustration marrante
     234
     235http://poisonedminds.com/comics/pm20060621.png