Cascading ifs

Discussion in 'Python' started by =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=, Apr 2, 2007.

  1. Hi experts,

    How would you do this without the more and more indenting cascade of ifs?:

    match = my_regex.search(line)
    if match:
    doSomething(line)
    else:
    match = my_regex2.search(line)
    if match:
    doSomething2(line)
    else:
    match = my_regex3.search(line)
    if match:
    doSomething3(line)

    etc.

    Thanks in advance and regards,
    Ernesto
    =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=, Apr 2, 2007
    #1
    1. Advertising

  2. Ernesto García García wrote:
    > Hi experts,
    >
    > How would you do this without the more and more indenting cascade of ifs?:
    >
    > match = my_regex.search(line)
    > if match:
    > doSomething(line)
    > else:
    > match = my_regex2.search(line)
    > if match:
    > doSomething2(line)
    > else:
    > match = my_regex3.search(line)
    > if match:
    > doSomething3(line)
    >
    > etc.


    tbl = [(my_regex, doSomething), (my_regex2, doSomething2), (my_regex3,
    doSomething3)]
    for regex, fun in tbl:
    match = regexp.match(line)
    if match:
    fun(line)
    break

    w.
    =?ISO-8859-2?Q?Wojciech_Mu=B3a?=, Apr 2, 2007
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

    Duncan Booth Guest

    Ernesto García García <> wrote:

    > Hi experts,
    >
    > How would you do this without the more and more indenting cascade of
    > ifs?:
    >
    > match = my_regex.search(line)
    > if match:
    > doSomething(line)
    > else:
    > match = my_regex2.search(line)
    > if match:
    > doSomething2(line)
    > else:
    > match = my_regex3.search(line)
    > if match:
    > doSomething3(line)
    >
    > etc.
    >
    > Thanks in advance and regards,
    > Ernesto
    >


    PATTERNS = [
    (my_regex, doSomething),
    (my_regex2, doSomething2),
    (my_regex3, doSomething3),
    ]
    ....

    for regex, action in PATTERNS:
    match = regex.search(line)
    if match:
    action(line)
    break


    Also be aware that repeatedly calling the search method with different
    regular expressions is horribly inefficient. You would be much better to
    combine the regular expressions into one and check which groups match
    (although admittedly that behaves differently since it would find the
    regex which matches earliest in the string instead of finding the first
    regex which matches anywhere).
    Duncan Booth, Apr 2, 2007
    #3
  4. =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

    Guest

    On Apr 2, 4:20 pm, Ernesto García García
    <> wrote:
    > Hi experts,
    >
    > How would you do this without the more and more indenting cascade of ifs?:
    >
    > match = my_regex.search(line)
    > if match:
    >    doSomething(line)
    > else:
    >    match = my_regex2.search(line)
    >    if match:
    >      doSomething2(line)
    >    else:
    >      match = my_regex3.search(line)
    >      if match:
    >        doSomething3(line)
    >
    > etc.
    >
    > Thanks in advance and regards,
    > Ernesto



    You might be able to use "guard clauses":
    http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses..html


    match = my_regex.search(line)
    if match:
       doSomething(line)
    return

    match = my_regex2.search(line)
    if match:
       doSomething2(line)
    return


    But if all of the blocks contain the same code like they do in your
    example, you're better off using the solutions provided by Wojciech
    Muła and Duncan Booth, because they abstract away the repetition.
    , Apr 2, 2007
    #4
  5. > tbl = [(my_regex, doSomething), (my_regex2, doSomething2), (my_regex3,
    > doSomething3)]
    > for regex, fun in tbl:
    > match = regexp.match(line)
    > if match:
    > fun(line)
    > break


    Thank you for the idea. This is a bit more difficult when functions need
    to work with a common context, but in that case I could store the
    context in an object and use the object's methods.

    Thanks,
    Ernesto
    =?ISO-8859-2?Q?Ernesto_Garc=EDa_Garc=EDa?=, Apr 9, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,935
    Peter
    Sep 30, 2005
  2. Continental Translations

    Drop Down Ifs

    Continental Translations, Jun 9, 2004, in forum: HTML
    Replies:
    4
    Views:
    432
    Continental Translations
    Jun 10, 2004
  3. Markus Dehmann
    Replies:
    8
    Views:
    2,080
    bartek
    Jun 2, 2004
  4. Vasily

    Optimisation of nested IFs

    Vasily, Jul 27, 2006, in forum: Java
    Replies:
    4
    Views:
    301
    Vasily
    Jul 28, 2006
  5. William Pursell

    cascading ifs--style question

    William Pursell, Nov 16, 2007, in forum: C Programming
    Replies:
    13
    Views:
    477
    Charlie Gordon
    Nov 25, 2007
Loading...

Share This Page