Compartilhar via


Do you amass nasty feelings about your designs?

I remember the time when structured programming lessons first came into my attention. Control flow constructs like sequence, choice, loops, etc.

In particular, the condition-controlled loop that places the condition test after the loop body, for example do-while loop of C/C++ or C#, or Repeat-Until loop of Pascal. I have not seen this type of loop very often in programs, in my experience, from the group of condition-controlled loops, the most frequently used is the loop that places the condition test before the loop body.

For example, the operation of reading string lines from a text file, I have seen it many times expressed as the following loop in C# code:

 void showfile(string filename)
{
 using(StreamReader reader=File.OpenText(filename))
  {
       string textline=reader.ReadLine();
      while(textline != null)
     {
           Console.WriteLine(textline);
            textline=reader.ReadLine();
     }
   }
}

It works but, I don´t like the nasty feeling left by invoking ReadLine method in two different places.

Using the do-while loop in C# –as in the following code– then that nasty feeling disappears and instead a somewhat emotional pleasure is left (see "Miroyokutaki hinshitsu").

 void showfile(string filename)
{
    using(StreamReader reader=File.OpenText(filename))
  {
       do
      {
           string textline=reader.ReadLine();
          if(textline == null) break;
         Console.WriteLine(textline);
        }while(true);
   }
}

Many software designers –I have seen– tend to disregard their own nasty feelings of this kind and their detailed design (source code) is less, much less of what it could be.

Fortunately, professional software designers do not ignore such nasty feelings about their designs.

For example, Andrew Koenig and Barbara E. Moo write very good insights about design in the following work:

Accelerated C++ Practical Programming by Example
by Andrew Koenig and Barbara E. Moo
Addison-Wesley, 2000
ISBN 0-201-70353-X
https://www.acceleratedcpp.com/

Comments

  • Anonymous
    January 13, 2007
    The comment has been removed

  • Anonymous
    January 13, 2007
    Nice alternative David. Thank you.

  • Anonymous
    February 17, 2007
    And yet the first one worked. It could work for 1, 5, 10 years and nobody would ever know but you. I understand the point but cannot appreciate the gain to the general audiance. I liken it to some old houses I have worked on.  For a hundred years they sheltered those that lived in them. Kept those in them alive in many cases. Then one day somebody wants to make it 'better'. And the ones making it better complain of the poor craft of the builders. At times I think we forget the actual intent and purpose of what we build.

  • Anonymous
    February 18, 2007
    The comment has been removed