Needless Repetition

Similar to OnceAndOnlyOnce, we should try to avoid needless repetition.. unless the repetition is needed ;-)

This page is here because my pattern may be different than that defined in OnceAndOnlyOnce.

There are many examples of Needless Repetition. Recoding your array algorithms each time is a problem and is addressed in IncludeFileParametricPolymorphism. Some repetition is addressed in Generic programming.. but let's talk about more needless repetition just in source files themselves using the keyboard over and over again:

For example I absolutely hate using the C mysql API like this:

  mysql_some_function
  mysql_other_function
  mysql_another
This is a minor quibble.. but I'd rather go:
  use
    mysql;
  b
    some_function;
    other_function;
    another;
  e.
It's not just about reducing repetition, but also about aesthetics and maintainability and more factors.

If repetition can be avoided and the code is arguably just as clear... then I'm all for it. One problem with the needless repetition is that when you go to modify your functions to mysql5_some_function or mysql2000_some_function your code must be searched and replaced if you decide to change it.

Let's consider also another sample (pretend these database versions exist):

  use
    firebird = firebird_v3; // we import a firebird version 3 module
    mysql = mysql2000;      // we import a mysql2000 module 
  b
    mysql.some_function;
    mysql.other_function;
    mysql.another;
    firebird.some_function;
    firebird.other_function;
    firebird.another;  
  e.
In this situation we address a problem... we are repeating the "mysql" declaration over and over again.. but we are only doing it because it is needed... and is justified... to make the code clear. Firebird also has similar (even exact in some cases) functions so we must repeat the declarations to make it clear which module we are referencing. The area that we save in repetition, is the fact that we do not need to repeat the version of the database in our code everywhere over and over again. The version is not hardcoded into the source file several times before each function call. Repeating the version of the database is needless repetition throughout the code... let's say we upgrade to a forwards/backwards compatible database:
  use
    firebird = firebird_v532; // we import a newer firebird module
    mysql = mysql3400;      // we import a newer database module
  b
    mysql.some_function;
    mysql.other_function;
    mysql.another;
    firebird.some_function;
    firebird.other_function;
    firebird.another;  
  e.
Since we didn't repeat the version in the source code over and over again, and since it was stored at the top of our file only once.. we saved ourselves. We did repeat the mysql declaration over and over again.. and the firebird declaration.. but that was justified. A "with" statement could reduce our code more.. but with statements offer some disadvantages that I won't go into (again whether the repetition is justified and safer/unsafer is the question).
  use
    firebird = firebird_v532; // we import a newer firebird module
    mysql = mysql3400;      // we import a newer database module
  b
    with mysql do 
      some_function;
      other_function;
      another;
    e;
    with firebird do
      some_function;
      other_function;
      another;  
    e;
  e.
Another form of NeedlessRepetition is languages that require continual annoying syntax/notation declarations over and over again that become tiring. This is the BEGIN and END which in the above examples is reduced to b and e. Again it is a minor quibble, but there is no need to pollute the source code with needless repetition if it can be avoided. One could argue that full words are easier to read.. and this is partly a matter of personal taste. But for example mathematicians do not continually type out ADD THIS TO THIS and THIS EQUALS THIS. Instead they use shortform signs such as + and =.

  (let 
    (firebird firebird_v532 
     mysql mysql3400)     
  (with mysql firebird 
      (some_function, other_function, another)
      (some_function, other_function, another)))

in the above case the with operator takes in 'n' arguments and the 'nth' block of code applies to the 'nth' argument only. the brackets are probably a bit wrong, but you get the idea. the same can be done with indentation as well.

    let 
        firebird firebird_v532 
        mysql mysql3400    
            with 
                mysql 
                    some_function, other_function, another
                firebird
                    some_function, other_function, another


EditText of this page (last edited October 28, 2010) or FindPage with title or text search