Delphi Programming

and software in general.

Thursday, August 7, 2008

Writing Readable Code - Paul's Snippet - Show your formatting!

Paul made an comment in my previous post, but unfortunately the Blogger comment system ate all the whitespace. Paul, if you read this - please reformat the code below and email it to me as a zipped attachment! I would also love to receive everybody else's reformatted version as well!
Your anonymity is guaranteed (Unless you explicitly permit me to name you and/or link to a site or something).

Please note that there was a syntax problem in the original snippet, and I have indicated that below. I suggest that unless Paul instruct us otherwise, we take out the semi-colon after the end on that line.

try
if ConditionA then
Code(1)
else if ConditionB then
if ConditionC then begin
if ConditionD then
code(2);
code(3);
end; // I assume this semi-colon has to go
else if ConditionE then begin
code(4);
morecode('X');
if ConditionF then
code(5)
else if ConditionG then
code(6);
end else begin
code(7);
morecode('Y');
end;
finally
code(8);
end;

3 comments:

  1. Hmmm, comments system doesn't want to allow a pre tag, so I don't think my indentation is going to be preserved, but I think the indentation should be clear even if it does get lost.

    So here goes nothing....

    procedure WhenC;
    begin
    if ConditionD then
    code(2);
    code(3);
    end;

    procedure WhenE;
    begin
    code(4);
    morecode('X');
    if ConditionF then
    code(5)
    else if ConditionG then
    code(6);
    end;

    begin
    try
    if ConditionA then
    Code(1)
    else if NOT ConditionB then
    EXIT;

    if ConditionC then
    WhenC
    else if ConditionE then
    WhenE
    else
    begin
    code(7);
    morecode('Y');
    end;
    finally
    code(8);
    end;
    end;

    Incidentally, Post #2 in my multicast events series is now up if you're interested. With downloadable code!

    :)

    ReplyDelete
  2. @Jolyon: could you email me the above rewrite please? I don't want to mangle your indent style :)

    @All: The mentioned semi-colon has to go, or the code won't compile.

    ReplyDelete
  3. @Jolyon: P.S. You introduced a bug in that rewrite.

    ReplyDelete