Difference between revisions of "Local Declaration Guidelines"

(Feature Arguments)
(Local Declarations)
Line 31: Line 31:
  
 
== Local Declarations ==
 
== Local Declarations ==
 +
Routine local declaration should also be prefixed to prevent potential conflicts between routine arguments or class attributes. The prefix ''<e>l_</e>'' is typically used for variable moniker names longer than two character. There are other exceptions, these are discussed below.
  
<e>l_</e>
+
=== Well Known Variable Names ===
 +
Indexing/counter variables, as used in iteration loops, should not use a local prefix and should be terse. The variable ''<e>i</e>'' should be use to indicate it is an index variable. Additional index variables should follow alphabetically from ''<e>i</e>'' onwards.
 +
 
 +
Generally, paired with an indexing/counter variable, a stopping condition count or number-of-items variable is also used. There are two conventions used for this, generally used interchangeably; A count will use the same rules for local declarations and be called ''<e>l_count</e>'', a number-of-items variable will use a well-known contracted variable name '<e>nb</e>''.
 +
 
 +
<e>
 +
f (a_array: ARRAY [INTEGER])
 +
    local
 +
        i, nb: INTEGER
 +
    do
 +
        from
 +
            i := a_array.lower
 +
            nb := a_array.upper
 +
        until
 +
            i > nb
 +
        loop
 +
            ...
 +
            i := i + 1
 +
        end
 +
    end
 +
</e>
 +
 
 +
The case will commonly arise when multiple counters/number-of-items variables need to be used. In such cases the counter/number-of-items variable should be suffixed with the associated indexing/counter variable name. This is akin to [http://en.wikipedia.org/wiki/BASIC|BASIC]'s <code>NEXT x</code> instruction:
 +
 
 +
<e>
 +
f (a_array: ARRAY [ARRAY [INTEGER]])
 +
    local
 +
        l_sub_array: ARRAY [INTEGER]
 +
        i, nb_i: INTEGER
 +
        j, nb_j: INTEGER
 +
    do
 +
        from
 +
            i := a_array.lower
 +
            nb_i := a_array.upper
 +
        until
 +
            i > nb_i
 +
        loop
 +
            l_item := a_array[i]
 +
            if l_item /= Void then
 +
                from
 +
                    j := l_item.lower
 +
                    nb_j := l_item.upper
 +
                until
 +
                    j > nb_j
 +
                loop
 +
                    ...
 +
                    j := j + 1
 +
                end
 +
            end
 +
            i := i + 1
 +
        end
 +
    end
 +
</e>
 +
 
 +
==== Rescue Clauses ====
 +
 
 +
When adding a rescue clause with a retry a state variable, typically used to determine if a <e>retry</e> has been performed, there is no need to use a local declaration prefix. In fact, it's recommended there is no prefix. Instead just use a variable named ''<e>retried</e>'':
 +
 
 +
<e>
 +
f
 +
    local
 +
        retried: BOOLEAN
 +
    do
 +
        if not retried then
 +
            ...
 +
        end
 +
    rescue
 +
        retried := True
 +
        retry
 +
    end
 +
</e>
  
 
=== In Contracts ===
 
=== In Contracts ===

Revision as of 11:15, 9 January 2009

Construction.png Not Ready for Review: This Page is Under Development!

Local declaration style guidelines for contributing to the Eiffel Software code repository.

Feature Arguments

Feature arguments should begin with the prefix a_ and nothing more. The prefix a_ represent a contraction of the word argument and does not represent the singular inflection - a noun - Therefore it is not valid to utilize an when the suffixed argument word begins with a vowel. The following is a correct usage:

f (a_string: STRING; a_integer: STRING)
        ...
    do
    end

Here, the second argument a_integer, is not considered A Integer but The Argument Integer, hence the use if a_ instead of _an_.

In-line Agent Feature Arguments

When working with an in-line agent, to prevent conflicts with the enclosing feature's arguments, the prefix ia_ should be used. The same rules regarding English language rules apply here as they do to feature arguments. The IA prefix represents an In-line Argument.

f (a_string: STRING; a_integer: STRING)
        ...
    do
        process (agent (ia_string: STRING; ia_integer: INTEGER)
            do
                ...
            end (a_string, a_integer))
    end

Local Declarations

Routine local declaration should also be prefixed to prevent potential conflicts between routine arguments or class attributes. The prefix l_ is typically used for variable moniker names longer than two character. There are other exceptions, these are discussed below.

Well Known Variable Names

Indexing/counter variables, as used in iteration loops, should not use a local prefix and should be terse. The variable i should be use to indicate it is an index variable. Additional index variables should follow alphabetically from i onwards.

Generally, paired with an indexing/counter variable, a stopping condition count or number-of-items variable is also used. There are two conventions used for this, generally used interchangeably; A count will use the same rules for local declarations and be called l_count, a number-of-items variable will use a well-known contracted variable name 'nb.

f (a_array: ARRAY [INTEGER])
    local
        i, nb: INTEGER
    do
        from
            i := a_array.lower
            nb := a_array.upper 
        until
            i > nb
        loop
            ...
            i := i + 1
        end
    end

The case will commonly arise when multiple counters/number-of-items variables need to be used. In such cases the counter/number-of-items variable should be suffixed with the associated indexing/counter variable name. This is akin to [1]'s NEXT x instruction:

f (a_array: ARRAY [ARRAY [INTEGER]])
    local
        l_sub_array: ARRAY [INTEGER]
        i, nb_i: INTEGER
        j, nb_j: INTEGER
    do
        from
            i := a_array.lower
            nb_i := a_array.upper 
        until
            i > nb_i
        loop
            l_item := a_array[i]
            if l_item /= Void then
                from
                    j := l_item.lower
                    nb_j := l_item.upper
                until
                    j > nb_j
                loop
                    ...
                    j := j + 1
                end
            end
            i := i + 1
        end
    end

Rescue Clauses

When adding a rescue clause with a retry a state variable, typically used to determine if a retry has been performed, there is no need to use a local declaration prefix. In fact, it's recommended there is no prefix. Instead just use a variable named retried:

f
    local
        retried: BOOLEAN
    do
        if not retried then
            ...
        end
    rescue
        retried := True
        retry
    end

In Contracts

Due to the current scoping rules of object-tests

rl_ for require el_ for ensure cl_ for check ll_ for loop il_ for loop invariant vl_ for loop variant

Indexing

When a conflict between local delcarations exist a form of indexing must be use

equals (a_object: ANY; a_other_object: ANY): BOOLEAN