0

Which case is generally better to use in terms of readability, efficiency, etc.

One: repeat the same conditions multiple times (no code is repeated)

instr1
instr2
if condition:
  instr3a
else:
  instr3b
instr4
if condition:
  instr5a
else:
  instr5b

etc..

Two: Enclose the whole statement in the condition and repeat code

if condition:
  instr1
  instr2
  instr3a
  instr4
  instr5a
else:
  instr1
  instr2
  instr3b
  instr4
  instr5b

etc..

Just by looking at it, the second seems more readable and only having a single condition must be faster to run, but what is the best practice regarding this?

walnut
  • 21,629
  • 4
  • 23
  • 59
Sean
  • 117
  • 2
  • 7
  • Related: https://www.youtube.com/watch?v=z43bmaMwagI – Jonathan Hall Dec 07 '19 at 22:12
  • 1
    Since you didn't specify a language I added the `language-agnostic` tag. The answer to the performance part of the question may however depend on the language and the particular kind of instructions and conditions. In particular interpreted languages such as Python may behave differently than compiled language such as C. I suggest you change the `language-agnostic` tag to a particular language that you are interested in, at least. – walnut Dec 07 '19 at 22:16

0 Answers0