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;
(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;
:D
ReplyDeleteWhat about 0? He forgot to handle the Nr = 0 case!
ReplyDeleteSourcecode in RAID 5? ^^
ReplyDeletePrimož Gabrijelčič what about any other case? not just Nr = 0!
ReplyDeleteMaybe there used to be different forms at some time and one after another became obsolete?
ReplyDeleteRemoving redundant or obsolete code - my favorite refactoring
ReplyDeletePerhaps it is meant for Multithreading?
ReplyDeleteUwe Raabe how would multithreading make this any more reasonable?
ReplyDeleteDaniela Osterhagen How would anything make this anymore reasonable?
ReplyDeleteUwe Raabe Isn't that what Daniela was asking?
ReplyDeleteI guess Uwe is playing "stupid programmers advocate" here trying to find reasons why this code exists ;)
ReplyDeleteIt could be a breakpoint construct. Break on fifth file opened. yes, you can do that with conditions - but who knows...
ReplyDeleteJørn Einar Angeltveit where does nr come from?
ReplyDeleteBest part is the "//Create form" comment
ReplyDeleteno one notice the "typo" in "OpneForm"?
ReplyDeleteHeinz Toskano I was assuming that it is some foreign language.
ReplyDeleteOpne is a valid dialect form of Open in Norwegian.
ReplyDeleteSteve Riley yap, the comment is the best part xD
ReplyDeleteI can "improve" on that ...
ReplyDelete// Create form
case Nr of
0: ; // Ignore this
else OpneForm(Fil, False);
end;
"Should" be this, no? :)
ReplyDelete// Create form
if (Nr in [1..9]) then
OpneForm(Fil, False);
This is already refactored version; 1st it was like :
ReplyDeleteif 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. :)
Simon Stuart if you raise an exception in the if branch, why have an else branch at all?
ReplyDeleteSo 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.
ReplyDeleteWe could modernize it...
ReplyDeletetype
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);
Lars Fosdal Oh, well done.
ReplyDeleteNo exception catch from all 1..9 Forms :)
ReplyDeleteLars 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.
ReplyDeleteLars Fosdal Well done! Now that's real enterprise code! ;p
ReplyDeleteStefan Glienke I won't say what sort of enterprise....
ReplyDeleteBill Meyer - Clearly, the starship variant ... i.e. far out...
ReplyDeleteSimon 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