Commenter correctement

Commenter correctement

Exemple d'utilisation abusive de commentaires.

switch @state
  when ig.Entity.RUNNING                      # If entity is running
    switch @AiLevel
      when 1                                  # If AI level is equal to 1
        if @flip                              # If entity is fliped
          @vel.x = -50
        else
          @vel.x = 50
 
        if @distanceTo(ig.game.player) < 120  # If distance beetween player and entity is inferior to 120 pixels
          @state = ig.Entity.SHOOTING         # Entity goes in state SHOOTING

Ces commentaires sont inutiles. Souvent, le code se documente de soi même, notamment grâce à l'utilisation de variables intermédiaires.

switch @state
  when ig.Entity.RUNNING
    switch @AiLevel
      when 1
        @vel.x = if @flip then -50 else 50
 
        playerIsNear = @distanceTo(ig.game.player) < 120
        if playerIsNear
          @state = ig.Entity.SHOOTING
 
tutoprog/commenter_correctement.txt · Dernière modification: 2011/09/28 16:05 par Sephi-Chan