[Python-ideas] If branch merging

Cory Beutler cgbeutler at gmail.com
Sun Jun 7 05:03:38 CEST 2015


I recently(1 year ago) realized that 'if', 'elif', and 'else' provide easy
branching for code, but there is no easy way to merge branches of code back
together. One fix for this would be introduction of two new keywords:
'also' and 'alif' (also if)

Here are the definitions of if-chain keywords with the addition of 'also'
and 'alif':
*if   *- execute code if this condition is met
*else *- execute code if no previous condition is met
*elif *- execute code if no previous condition is met and this condition is
met
*also *- execute code if any previous condition is met
*alif *- execute code if any previous condition is met and this condition
is met

This would simplify some logic expressions by allowing the merging of
branched code.


*Examples of use:*
*Duplicate code in if-chains may be reduced:*
# Old Version
if a == b:
    print ('a == b')
    foo()             # <-- duplicate code
elif b == c:
    print ('b == c')
    foo()             # <-- duplicate code
elif c == d:
    print ('c == d')
    foo()             # <-- duplicate code

# New Version
if a == b:
    print ('a == b')
elif b == c:
    print ('b == c')
elif c == d:
    print ('c == d')
also:
    foo()            # <-- No longer duplicated


*Many nested 'if' statements could now be a more linear style:*
# Old Version
if a == b:
    print ('a == b')
    if b == c:
        print ('b == c')
    print ('end if')

# New Version
if a == b:
    print ('a == b')
alif b == c:
    print ('b == c')
also:
    print ('end if')


These two examples are the most common ways this will help code. I have
been writing code samples using these keywords and have found that it
simplifies many other things as well. It does take a bit of getting used
to, though. I have found that is is best to use 'also' and 'alif'
sparingly, as overuse can make some code less flexible and more confusing.


*Selective Branch merging:*
One limitation of the 'also' and 'alif' keywords is the restriction to the
"all of the above" checking. What I mean by that is that there is no way to
pick and choose which branches to merge back together. When using 'also'
and 'alif' you are catching all previous if-branches. One easy way to solve
this would be to allow for named branching. The most simple way to do this
is to save the conditions of each branch into a variable with a name. Here
is an example of merging only select branches together:
# Old Version
if a == b:
    print ('a == b')
elif a == c:
    print ('a == c')
elif a == d:
    print ('a == d')
if (a == b) or (a == d):
    print ('a == b and a == d')

# New Version using 'as' keyword
if a == b as aisb:
    print ('a == b')
elif a == c:
    print ('a == c')
elif a == d as aisd:
    print ('a == d')
alif aisd or aisb:
    print ('a == b and a == d')

NOTE: In the old version, it may be necessary to save off the boolean
expression beforehand if the variables being used are changed in the first
set of conditions. This would not be required in the new version, making
the total code written an even larger gap.

I realize that using the 'as' keyword may not be the best. Using 'as' seems
to suggest that it will only be used in the following block. An alternative
to using the 'as' keyword could be assigning the 'if' to a variable like so:
aisb = if a == b:
This looks a bit gross to me, though. If think of a better one, I would
love to see it.


*Speed:*
The next logical question to ask is that of speed. Will this slow down my
code at all? I happily submit to you that it shouldn't. If done right, this
may even speed things up by a cycle or two (yeah. I know. That is not
enough to fuss over, but I view it as a side benefit.)
When just using 'also' and 'alif', there should be no difference in speed.
The same number of jumps and checks should be done as before. Naming
branches may add an additional assignment operation, but this should be
more than made up for by not having to calculate the condition more than
once. There may be a few cases where this would be slower, but those can be
optimized to result in old-style code.


*Implementation:*
I am currently learning how the python parser and lexer work in hopes of
making a custom version containing these features. Because of my lack
of knowledge here, I cannot say how it should be implemented in python
specifically. Here is how each if code block should work in basic english:

on True:
    1. Execute code block
    2. Jump to next 'alif', 'also', or end of if-chain
on False:
    1. jump to next 'elif', 'else', or end of if-chain

Note: 'also' blocks can be useful in more places than just the end of a
chain:
if a == b:
    print ('a == b')
elif a == c:
    print ('a == c')
also:
    print ('a == b == c')
else:
    print ('a != b and a != c')

It could also be useful to have more than one 'also' in an if chain.

In contrast, having an 'else' before the end is not so useful. For example,
placing one before an also makes the also a little pointless:
if a == b:
    print ('a == b')
elif a == c:
    print ('a == c')
else:
    print ('a != b and a != c')
also:
    print ('This will always execute')

It would therefore be good to still require that 'else' be the last item in
a chain.


*The End*
Thank you for humoring my idea. I am new to this mailing list, so sorry if
this seems out of line or something. I am currently working with ansi C to
try to get something similar into the 'C' language, but it could be 20
years before anything comes of that. The gears move ofly slow over there.
Anyway, I look forward to hearing your feedback.

-Cory Beutler
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150606/f52670c2/attachment-0001.html>


More information about the Python-ideas mailing list