Commentaire (informatique) - Définition

Source: Wikipédia sous licence CC-BY-SA 3.0.
La liste des auteurs de cet article est disponible ici.

Exemples

C

(extrait du fichier fork.c du noyau Linux)

      /*       * Check if we are over our maximum process limit, but be sure to       * exclude root. This is needed to make it possible for login and       * friends to set the per-user process limit to something lower       * than the amount of processes root is running. -- Rik       */      if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur          && !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))          goto bad_fork_free;      

Perl

(extrait de perlbug.PL dans la distribution standard de Perl)

          # a strange way to check whether any significant editing          # have been done: check whether any new non-empty lines          # have been added. Yes, the below code ignores *any* space          # in *any* line.          while () {              s/\s+//g;              $unseen++ if $_ ne '' and not exists $REP{$_};          }      

PHP

(extrait de MediaWiki, le logiciel utilisé par Wikipedia, une encyclopédie collaborative en ligne)

      /*       * Scan forwards to find beginning of another run of changes.       * Also keep track of the corresponding point in the other file.       *       * Throughout this code, $i and $j are adjusted together so that       * the first $i elements of $changed and the first $j elements       * of $other_changed both contain the same number of zeros       * (unchanged lines).       * Furthermore, $j is always kept so that $j == $other_len or       * $other_changed[$j] == false.       */          while ($j < $other_len && $other_changed[$j])              $j++;      

Utilisation des commentaires

Documentation

Des outils comme doxygen, javadoc ou rdoc permettent de structurer les commentaires présents dans le code et de générer une documentation complète. Celle-ci est la plupart du temps destinée à un programmeur désirant développer avec une bibliothèque, mais il est tout à fait possible d’utiliser cette méthode pour écrire la documentation destinée à l’utilisateur final.

          /**           * Registers the text to display in a tool tip.   The text            * displays when the cursor lingers over the component.           *           * @param text  the string to display.  If the text is null,            *              the tool tip is turned off for this component.           */          public void setToolTipText(String text) {      

(un extrait de la documentation javadoc de Sun Microsystems ; le commentaire est dans un format compréhensible par le processeur javadoc)

Désactivation de code

On utilise souvent les commentaires pour cacher rapidement des portions de code au compilateur ou à l’interpréteur, par exemple pour tester des implémentations alternatives, ou pour désactiver temporairement des fonctionnalités.

Cette désactivation est la plus facile à effectuer lorsque le langage permet des commentaires par blocs. Cependant, dans le cas où la portion de code à cacher contient déjà des commentaires, la syntaxe du langage ne permet pas toujours de simplement d’imbriquer ces commentaires. Certains langages fournissent un niveau supérieur de commentaires pour pallier cela :

#if 0
  code; //commentaire
  encore du code; /* commentaire */
#endif
  • XML :
 
]]>
Page générée en 0.086 seconde(s) - site hébergé chez Contabo
Ce site fait l'objet d'une déclaration à la CNIL sous le numéro de dossier 1037632
A propos - Informations légales
Version anglaise | Version allemande | Version espagnole | Version portugaise