2008-07-01

convention

LAST UPDATED: 2008-07-03

Shortcuts: C C++ Java PHP Python Ruby

Ruby:
# The name of a class name is a noun.
# Camel case is used.
# (Every word starts with an upper case letter.)
class RectangularShape
    
    # The name of a class constant if a noun.
    # The name must start with an upper case letter
    #   as required by the language.
    MAXIMUM_WIDTH = 1024
    MAXIMUM_HEIGHT = 768
    
    # The name of a class variable is a noun.
    # The name must start with "@@"
    #   as required by the language.
    # Lower case letters are used.
    # Every word is separated by an underscore.
    @@instance_count = 0
    
    # The name of a class accessor method
    #   has the same name as the class variable.
    def self.instance_count
        @@instance_count
    end
    
    # Constructors must be named "initialize"
    #   as required by the language.
    def initialize(x, y, w, h)
        @instance_id = @@instance_count++
        @x_coordinate = x
        @y_coordinate = y
        @width = w
        @height = h
    end
    
    # The name of an instance accessor method
    #   has the same name as the instance variable.
    def instance_id
        # The name of an instance variable is a noun.
        # The name must start with "@"
        #   as required by the language.
        # Lower case letters are used.
        # Every word is separated by an underscore.
        @instance_id
    end
    
    def x_coordinate
        @x_coordinate
    end
    
    def y_coordinate
        @y_coordinate
    end
    
    def width
        @width
    end
    
    def height
        @height
    end
    
    # The name of a method starts with a verb.
    # Lower case letters are used.
    # Every word is separated by an underscore.
    def move_to(x, y)
        @x_coordinate = x
        @y_coordinate = y
    end
    
    def contains?(x1, y1)
        # The name of a local variables starts
        #   with a lower case letter or an underscore.
        # Short forms or single letter names are used.
        # Every word is optionally delimited by an underscore.
        retval = (x1 >= @x_coordinate) &&
                 (x1 < @x_coordinate + @width) &&
                 (y1 >= @y_coordinate) &&
                 (y1 < @y_coordinate + @height)
    end
end

# The name of a global variable is a noun.
# Lower case letters are used.
# Every word is separated by an underscore.
$debug_mode = 0

No comments: