Friday, May 6, 2022

[SOLVED] Useless if statement in yum source

Issue

I was curious about how something worked in yum so I was looking at some of its score code and I found this line in the erasePkgs function in cli.py.

if False: pass
elif basecmd in ('erase-n', 'remove-n'):
   rms = self.remove(name=arg)
.
.
.

The if False: pass does nothing correct? It never gets into that branch it always just skips to the next one doesn't it?

Here is the link to the source code: https://github.com/rpm-software-management/yum/blob/master/cli.py. It's on line 1268.


Solution

This appears to be the developer's idiom for a generalized switch statement.

        if False: pass
        elif basecmd in ('erase-n', 'remove-n'):
            rms = self.remove(name=arg)
        elif basecmd in ('erase-na', 'remove-na'):
            ...
        elif basecmd in ('erase-nevra', 'remove-nevra'):
            ...
        else:
            ...

which is ever so slightly more readable than

        if basecmd in ('erase-n', 'remove-n'):
            rms = self.remove(name=arg)
        elif basecmd in ('erase-na', 'remove-na'):
            ...
        elif basecmd in ('erase-nevra', 'remove-nevra'):
            ...
        else:
            ...


Answered By - mjhm
Answer Checked By - David Marino (WPSolving Volunteer)