delphiif-statementdelphi-2006

Removing part of condition in some cases


Is there a way to remove part of condition in IF statement depending if I need it or not. Next code is example, because in my code there are a lot of user defined functions and procedures in my language:

IF A THEN
Q := TQuery.Create(Application);
IF B AND C AND D AND E AND Q.FieldByName('Example').AsInteger = 1 then
BEGIN
...
END

So, let's say after creating TQuery that I have imported some data into it (I didn't write that part of code here). Is there a way to remove part with Q in the second IF statement if the Q wasn't created (because the condition A was not satisfied) or do I have to writhe the whole IF statement again just without Q part?

I was thinking using something like CASE to check if Q is created and if it is not, just to skip that part of sentence. If it is possible, how can I skip it?


Solution

  • Quick and dirty way

    IF B AND C AND D AND E AND (NOT A OR Q.FieldByName('Example').AsInteger = 1) then
    

    As a note, try to keep your if conditions simpler.

    EvaluationRequested := A AND C ...
    QueryNeedsAval := NOT A OR ... 
    if EvaluationRequested AND QueryNeedsAval then 
    begin
       ...
    end;