Anybody else have a gripe with nested if-then-else statements in Pascal?
Anybody else have a gripe with nested if-then-else statements in Pascal?
What if we had the following statement/syntax available in Pascal? I called it IFCASE for a lack of a better word. In essence it is a CASE statement and will exit as soon as the first true case is reached.
Do you think it makes for easier reading and for less clutter?
IFCASE
when ((X > Y) and (Y < 100)) then
begin
...
end;
when ((X > Z) and (Z < 100)) then
begin
...
end;
when ((X < Y+Z) and (X < 100)) then
begin
...
end;
else
begin
...
end;
END;
Instead of:
if ((X > Y) and (Y < 100)) then
begin
...
end
else
begin
if ((X > Z) and (Z < 100)) then
begin
...
end
else
begin
if ((X < Y+Z) and (X < 100)) then
begin
...
end
else
begin
...
end;
end;
end;
What if we had the following statement/syntax available in Pascal? I called it IFCASE for a lack of a better word. In essence it is a CASE statement and will exit as soon as the first true case is reached.
Do you think it makes for easier reading and for less clutter?
IFCASE
when ((X > Y) and (Y < 100)) then
begin
...
end;
when ((X > Z) and (Z < 100)) then
begin
...
end;
when ((X < Y+Z) and (X < 100)) then
begin
...
end;
else
begin
...
end;
END;
Instead of:
if ((X > Y) and (Y < 100)) then
begin
...
end
else
begin
if ((X > Z) and (Z < 100)) then
begin
...
end
else
begin
if ((X < Y+Z) and (X < 100)) then
begin
...
end
else
begin
...
end;
end;
end;
Follow the SRP, SLA and reduce the cyclomatic complexity of your code and those if/then/else trees will start to disappear.
ReplyDeleteJeroen Wiert Pluimers Agreed. In many instances, I find such long-winded constructs in routines which are far longer than any reasonable standard would dictate. Legacy code issues.
ReplyDeleteSwitch and case directive from paradox .
ReplyDelete