David Weng
David Weng

Reputation: 4235

Ideas to write better if statements

In this question I just want to ask for some ideas. I sometimes run into situations where I end up writing such if statements, however, I feel like there's a better way to write this as func1() is written in two places, I believe it should be only in one place.

if (cond1) {
    func1();
} else {
    if (cond2) {
        func1();
    } else {
        func2();
    }
}

How would you write this in a better, and of course readable way?

Upvotes: 0

Views: 83

Answers (2)

ChrisBD
ChrisBD

Reputation: 9209

How about:

if ((cond1)||(cond2))
{
  func1();
}
else
{
  func2();
}

Upvotes: 1

Callie J
Callie J

Reputation: 31296

You've not said what language, but it looks C/Java/C# based...

if (cond1 || cond2) {
    func1();
} else {
    func2();
}

or similar should work?

Upvotes: 3

Related Questions