Terse naming

From Ssdlpedia
(Redirected from Terse variable naming)
Jump to: navigation, search

The terse naming technique is the principal technique for reducing character count complexity metric, but it offers positive implications on the horizontal complexity as well. The technique dictates replacing long identifier names with short, often one or two characters long, names. The challenge is of course is doing that without obfuscating the code. Here is a simple example. Instead of writing

void draw_Quadratic_function(double coefficient_X_squared, double coefficient_X, double free_coefficient, int points_count)
 {
     double values_array[] = new double[points_count];<br>
     for (int index = 0; index < points_count; index++) {
          values_array[index] = coefficient_Xsquared * index * index + coefficient_X * index + free_coefficient;
      draw(values_array,points_count);  
 }

one may simply write

void draw_quad_func(double a, double b, double c, int n)
 {
     double f[] = new double[n];
     for (int i = 0; i < n; i++) {
          f[i] = a * i * i + b * i + c;
      draw(f,n);  
 }

This shorter former is just as clear as its longer version. Further, the shorter form reveals a quick optimization by using Newtwon's formula for the evaluation of polynomials,

void draw_quad_func(double a, double b, double c, int n)
 {
     double f[] = new double[n];
     for (int i = 0; i < n; i++) {
          f[i] = (a * i  + b ) * i + c;
      draw(f,n);  
 }

which incidentally reduces further the character count and the token count metrics.

The preaching of long, verbose and meaningful identifiers is based on the presumption that identifiers may be accessed from many locations in the code, and that the full meaning of the identifier can only be obtained by the tiresome and long chore of examining all these locations. This preaching is also motivated that good, short names are sparse.

The terse identifiers technique is inspired by the observation that there are many identifiers which are visible only at a very limited scope. Further, the application of other spartan programming techniques may make this scope even shorter. Here are a couple of rules of thumb that help in determining when short identifiers are appropriate

  1. Entities that are visible only in a short scope deserve a terse name.
  2. Entities that can be distinguished from others through their containing name space deserve a terse name.
  3. Entities that seem to require long and cumbersome names are indicative of bad design.

It is a consequence of the above rules that variables, most notably parameters and local variables (but also class variables) make the primary candidates for the application of the terse naming convention. Types, routine, and module names usually have a broader scope. The names of such larger scope entities can rarely be one or two characters. Still, names which are one short word, are better than longer concatenation of words. Disambiguation is achieved by proper scoping mechanisms.

Personal tools