1 (edited by mathmathou 2015-12-15 01:26:13)

Topic: [Solved] Coding condition... the theorical way

Hello Dmitry and all MVD fans,


I turn to you today because I am questioning my logical syntax... smile


Let's say I have a series of functions and I want to chain them to achieve a result with conditions like this :

  • Check a serial number (empty, already in databse or OK)

  • Get a URL based on this serial (check if begins by http)

  • Retrive source code of a page (is it empty or OK)

  • Retrieve a name in the page (is it empty or OK)


All those conditions occur one after another and or dependant on each other, that is why I nest them.


Here is the way I would organize the logical syntax with conditions (of course this is not real code, it's just to show the logicial structure) :

begin
    if serial field empty then                          //BEGIN TEST 1
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if serial allready in databse then             //CONTINUES TEST 1
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if serial not empty AND not in database        //CONTINUE TEST 1
        begin
            if URL does not match http then             //BEGIN TEST 2
                begin
                    Cancel := True;
                    ShowMessage('Error');
                end
            else if URL matches http then               //CONTINUE TEST 2
                begin
                    if source empty then                //BEGIN TEST 3
                        begin
                            Cancel := True;
                            ShowMessage('Error');
                        end
                    else if source not empty then       //CONTINUE TEST 3
                        begin
                            if name empty then          //BEGIN TEST 4
                                begin
                                    Cancel := True;
                                    ShowMessage('Error');
                                end
                            else if name not empty then //CONTINUE TEST 4
                                begin
                                    Final code;
                                end;                    //CLOSE TEST 4
                        end;                            //CLOSE TEST 3
                end;                                    //CLOSE TEST 2
        end;                                            //CLOSE TEST 1
end;

I have always been doing it like this, and I feel it is the correct way to do it. But what would happend if I did it the linear way like :


begin
    if serial field empty then                    //BEGIN TEST 1
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if serial allready in databse then       //CONTINUES TEST 1
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if serial not empty AND not in database  //CLOSE TEST 1
        begin
            DO SOME CODE
        end;
    if URL does not match http then               //BEGIN TEST 2
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if URL matches http then                 //CLOSE TEST 2
        begin
            DO SOME CODE
        end;
    if source empty then                          //BEGIN TEST 3
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if source not empty then                 //CLOSE TEST 3
        begin
            DO SOME CODE
        end;
    if name empty then                            //BEGIN TEST 4
        begin
            Cancel := True;
            ShowMessage('Error');
        end
    else if name not empty then                   //CLOSE TEST 4
        begin
            Final code;
        end;
end;

But then, if I do it the linear way, will the logic still be respected and the success of CONDITION 1 will trigger CONDITION 2, or will all conditions be tested whatever the result of the previous condition ?


Yes, I know, I sometimes have a disturbed mind smile


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: [Solved] Coding condition... the theorical way

Here you can find some info about if then else

http://www.delphibasics.co.uk/Article.asp?Name=Logic
http://www.delphibasics.co.uk/RTL.asp?Name=If
http://delphi.about.com/od/beginners/a/ … -traps.htm

Dmitry.

Re: [Solved] Coding condition... the theorical way

Very interesting links.


I was not so far from the "correct way" with my first approach then smile


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor