Legacy code snippet of the day:

Legacy code snippet of the day:
(Yes.  This is found in real production code.)

   // Create form
   case Nr of
     1: OpneForm(Fil, False);
     2: OpneForm(Fil, False);
     3: OpneForm(Fil, False);
     4: OpneForm(Fil, False);
     5: OpneForm(Fil, False);
     6: OpneForm(Fil, False);
     7: OpneForm(Fil, False);
     8: OpneForm(Fil, False);
     9: OpneForm(Fil, False);
   end;

Comments

  1. What about 0? He forgot to handle the Nr = 0 case!

    ReplyDelete
  2. Primož Gabrijelčič what  about any other case? not just Nr = 0!

    ReplyDelete
  3. Maybe there used to be different forms at some time and one after another became obsolete?

    ReplyDelete
  4. Removing redundant or obsolete code - my favorite refactoring

    ReplyDelete
  5. Perhaps it is meant for Multithreading?

    ReplyDelete
  6. Uwe Raabe how would multithreading make this any more reasonable?

    ReplyDelete
  7. Daniela Osterhagen How would anything make this anymore reasonable?

    ReplyDelete
  8. I guess Uwe is playing "stupid programmers advocate" here trying to find reasons why this code exists ;)

    ReplyDelete
  9. It could be a breakpoint construct. Break on fifth file opened.  yes, you can do that with conditions - but who knows...

    ReplyDelete
  10. Jørn Einar Angeltveit​ where does nr come from?

    ReplyDelete
  11. Best part is the "//Create form" comment

    ReplyDelete
  12. no one notice the "typo" in "OpneForm"?

    ReplyDelete
  13. Heinz Toskano I was assuming that it is some foreign language.

    ReplyDelete
  14. Opne is a valid dialect form of Open in Norwegian.

    ReplyDelete
  15. Steve Riley yap, the comment is the best part xD

    ReplyDelete
  16. I can "improve" on that ...

       // Create form
       case Nr of
         0: ; // Ignore this
         else OpneForm(Fil, False);
       end;

    ReplyDelete
  17. "Should" be this, no? :)

     // Create form
       if (Nr in [1..9]) then
         OpneForm(Fil, False);

    ReplyDelete
  18. This is already refactored version; 1st it was like :
    if Nr = 1 then
      OpneForm(Fil, False)
    else if Nr = 2 then
      OpneForm(Fil, False)
    else if Nr =3 then
      OpneForm(Fil, False)
    else if Nr = 4 then
      OpneForm(Fil, False)
    etc. :)

    ReplyDelete
  19. Simon Stuart if you raise an exception in the if branch, why have an else branch at all?

    ReplyDelete
  20. So why raise an exception. You could just show a messagebox and terminate the program with Halt(4711). I have seen quite a lot programs doing that, so it must be good practice.

    ReplyDelete
  21. We could modernize it...

    type
      TOpneAction = reference to procedure;

      TOpneFormFactory = class(TDictionary    procedure Populate(const aIndexes: array of Integer; const aAction:TOpneAction);
        procedure Opne(const aIndex: Integer);
      end;

    procedure TOpneFormFactory.Populate(const aIndexes: array of Integer; const aAction:TOpneAction);
    var
      ix: Integer;
    begin
      for ix in aIndexes
       do Add(ix, aAction)
    end;

    procedure TOpneFormFactory.Opne(const aIndex: Integer);
    var
      Action: TOpneAction;
    begin
      if TryGetValue(aIndex, Action)
       then Action
        else raise Exception.Create('Call 911'); 
    end;

    ...

    var
      OpneFactory: TOpneFormFactory;

    begin
      OpneFactory := TOpneFormFactory.Create;
      OpneFactory.Populate([1,2,3,4,5,6,7,8,9],
        procedure
        begin
           OpneForm(Fil, False);
        end);

    ...

      // Create form
      OpneFactory.Opne(Nr);

    ReplyDelete
  22. No exception catch from all 1..9 Forms :)

    ReplyDelete
  23. Lars Fosdal The scary bit is that modernized code looks from afar like any other snippet of modernized code, and not like a WTF... even though it still is the same WTF.

    ReplyDelete
  24. Lars Fosdal Well done! Now that's real enterprise code! ;p

    ReplyDelete
  25. Stefan Glienke I won't say what sort of enterprise....

    ReplyDelete
  26. Bill Meyer - Clearly, the starship variant ... i.e. far out...

    ReplyDelete
  27. Simon Stuart Your response is just about like what I was going to post and one that Bill Meyer can really appreciate. I really cannot improve on it, but then again, what I am referring to, there would not have been the raise exception. :-)

    ReplyDelete

Post a Comment