I just came accross the following code in an existent project, which I’m working on:
if True:
x = 5
y = 6
return x+y
else:
return 'Something
Inside the if True are lots of conditions and some will also return the function already.
Why would somebody write in that way? The code contained some other bugs also, but was just wondering about the if True: statement as it didn’t make any sense to me. Probably also pretty stupid to ask it, but was wondering hehe.
It might be a remnant of debugging or refactoring. It may be that instead of
True, there was orginally a condition or variable there but it has now been replaced byTrue. The developer perhaps left it there without refactoring or cleaning it up all the way.If you’re free to edit the code as you wish and you’re sure that the else is no longer needed, then you can remove it. It indeed makes no sense to have code in your codebase that will never run.