Named constants

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
 
Line 4: Line 4:
  
 
This improves the readability of the code, and makes it way easier to maintain.
 
This improves the readability of the code, and makes it way easier to maintain.
 +
 +
 +
== Example - ''Without constants''==
 +
 +
Here a simple class has a writeBuffer and readBuffer method taking three values (the buffer, the starting point, and the size of the buffer). At each method the size of the buffer is declared '512'.
 +
 +
public class StreamAccess {
 +
    .
 +
    .
 +
    void writeBuffer(string input)
 +
    {
 +
        Stream s = new Stream();
 +
        s.WriteToBuffer(input, 0, 512);
 +
        s.Close();
 +
    }
 +
    string readBuffer(Stream s)
 +
    {
 +
        string output;
 +
        s.ReadBuffer(output, 0, 512);
 +
        s.Close();
 +
        return output;
 +
    }
 +
 +
== Example - ''With constants''==
 +
 +
In this example however, a ''const int'' declares the constant ''BUFF_SIZE'' which contains this '512' value. This allows the programmer to quickly change the value without changing each value in place.
 +
 +
  public class StreamAccess {
 +
    .
 +
    .
 +
    const int BUFF_SIZE = 512;
 +
    .
 +
    .
 +
    void writeBuffer(string input)
 +
    {
 +
        Stream s = new Stream();
 +
        s.WriteToBuffer(input, 0, BUFF_SIZE);
 +
        s.Close();
 +
    }
 +
    string readBuffer(Stream s)
 +
    {
 +
        string output;
 +
        s.ReadBuffer(output, 0, '''BUFF_SIZE''');
 +
        s.Close();
 +
        return output;
 +
    } s.Close();
 +
}

Latest revision as of 02:32, 20 October 2010

A Named Constant is a descriptive, meaningful, non literal name that is given to take the place of a number, string or other expression. It is similar to a variable, but cannot be modified. It has a global scope.

Named Constants should be used for values that occur in many places in the code. This allows you to easily change the value by changing the constant definition instead of changing the value in each place.

This improves the readability of the code, and makes it way easier to maintain.


Example - Without constants

Here a simple class has a writeBuffer and readBuffer method taking three values (the buffer, the starting point, and the size of the buffer). At each method the size of the buffer is declared '512'.

public class StreamAccess {
    .
    .
    void writeBuffer(string input)
    {
        Stream s = new Stream();
        s.WriteToBuffer(input, 0, 512);
        s.Close();
    }
    string readBuffer(Stream s)
    {
        string output;
        s.ReadBuffer(output, 0, 512);
        s.Close();
        return output;
    }

Example - With constants

In this example however, a const int declares the constant BUFF_SIZE which contains this '512' value. This allows the programmer to quickly change the value without changing each value in place.

 public class StreamAccess {
    .
    .
    const int BUFF_SIZE = 512;
    .
    .
    void writeBuffer(string input)
    {
        Stream s = new Stream();
        s.WriteToBuffer(input, 0, BUFF_SIZE);
        s.Close();
    }
    string readBuffer(Stream s)
    {
        string output;
        s.ReadBuffer(output, 0, BUFF_SIZE);
        s.Close();
        return output;
    } s.Close();
}
Personal tools