0

Is it bad practise to assign variables in an if statement and not use each variable if that part of the if statement is called?

For example in the following code I have two code option. Option 1 if trigger == ‘1’ or if trigger == ‘2’ then variable y will be unused as it is only needed if trigger == ‘4’ or if trigger == ‘5’. Option 2 is longer, but fixes this.

# option 1

if trigger == '1' or trigger == '4':
    x = int(start)
    y = int(s1)
elif trigger == '2'or trigger == '5':
    x = int(s1)
    y = int(s2)
elif trigger == '3':
    x = int(s2)

# option 2

if trigger == '1' or trigger == '4':
    x = int(start)
elif trigger == '2'or trigger == '5':
    x = int(s1)
elif trigger == '3':
    x = int(s2)

if trigger == '4':
    y = int(s1)
elif trigger == '5':
    y = int(s2)

My question is which one is correct and which one should I use?

Thanks.

Update –

So is it ok to set a variable and not use it if that part of the if function (in this case) is called? It is not bad practice or lead to memory issues etc. Thanks.

7
  • 3
    Looks fine to me. I would do if trigger in ('1', '4'):, however, it's more pythonic. Commented Oct 11, 2012 at 9:39
  • @JoelCornett: if trigger in '14' does the same thing and requires fewer keystrokes Commented Oct 11, 2012 at 9:42
  • 3
    @inspectorG4dget - but that fails in the case that trigger == "14". Commented Oct 11, 2012 at 9:46
  • @detly: trigger=='14' is not a requirement in this problem; and if were, I would suggest if trigger in '1 4 14'.split(). It still takes fewer keystrokes and makes it easier to add more trigger values to check against. Commented Oct 11, 2012 at 9:49
  • 4
    Optimizing for keystrokes is hardly in the spirit of Python, or this question. Commented Oct 11, 2012 at 9:53

4 Answers 4

2

If you really want to have them defined only if used, why not make it totally explicit and obvious both to read and change?

if   trigger == '1':
    x = int(start)

elif trigger == '2':
    x = int(s1)

elif trigger == '3':
    x = int(s2)

elif trigger == '4':
    x = int(start)
    y = int(s1)

elif trigger == '5':
    x = int(s1)
    y = int(s2)
Sign up to request clarification or add additional context in comments.

1 Comment

Well I don’t necessarily need them to be defined only if used. ATM I am trying to reduce my already existing code and am now wondering if there are any side effects or if it is bad programming practice to define a variable and not use them every time a function is called. I have updated by original post to reflect this.
1

Why not have one more condition inside your if.

This is a better way to write your option 2. Which I think you can follow: -

if trigger in ('1', '4'): // You can better use `in` to check for more values
    x = int(start)

    if trigger == '4': 
        y = int(s1)

elif trigger in ('2', '5'):  
    x = int(s1)

    if trigger == '5': 
        y = int(s2)            

elif trigger == '3':
    x = int(s2)

Comments

1

In analogy to the optimizations in the KV diagram where Xed fields are considered or not depending if it is useful in optimizations (cf. 7-segment display with BCD values where the values 10..15 are undefined), I would tend to keep it as simple as possible and assign values even if they are not used later.

2 Comments

@ glglgl So there are no issus with assign values even if they are not used later?
No, they are just left unused.
0

If all of s1, s2 etc will always exist and be valid integers, then option 1 is fine.

However, it does make your code depend unnecessarily on the format of values you don't use. Sometimes that's fine. But if e.g. s1 and friends come from options passed on the command line or entered in an input file, your users would not like you for throwing a ValueError for leaving those values undefined, forcing them to pass meaningless integers in parameters they have no use for.

So really, the answer is that it all depends on the context! It's hard to give a general answer to these kinds of question.

1 Comment

Thanks your answer has cleared up reasons for and against using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.