Skip to content

Instantly share code, notes, and snippets.

@natanrolnik
Created December 18, 2019 14:08
Show Gist options
  • Save natanrolnik/26318bdb252049d20dfc25d1ffefc3df to your computer and use it in GitHub Desktop.
Save natanrolnik/26318bdb252049d20dfc25d1ffefc3df to your computer and use it in GitHub Desktop.
Examples of redundant else usage
------ ⭣ Redundant Else ⭣ ------
if someCondition {
return meh
} else {
let someValue = callSomeFunction()
return someValue
}
----------- ⭣ Fine ⭣ -----------
if someCondition {
return meh
}
let someValue = callSomeFunction()
return someValue
----------- ⭣ Fine ⭣ -----------
if someCondition {
doSomething()
} else {
doSomethingElse()
}
------ ⭣ Redundant Else ⭣ ------
if someCondition {
return
} else {
callSomeFunction()
}
------ ⭣ Redundant Else ⭣ ------
if someCondition {
return meh
} else if someOtherCondition {
let someValue = callSomeFunction()
return someValue
} else {
let someOtherValue = callSomeOtherFunction()
return someOtherValue
}
----------- ⭣ Fine ⭣ -----------
if someCondition {
return meh
}
if someOtherCondition {
let someValue = callSomeFunction()
return someValue
}
let someOtherValue = callSomeOtherFunction()
return someOtherValue
------ ⭣ Redundant Else ⭣ ------
for string in stringsArray {
if string.isEmpty {
continue
} else {
doSomething(with: string)
}
}
----------- ⭣ Fine ⭣ -----------
for string in stringsArray {
if string.isEmpty {
continue
}
doSomething(with: string)
}
------ ⭣ Redundant Else ⭣ ------
for string in stringsArray {
if string.isEmpty {
break
} else {
doSomething(with: string)
}
}
----------- ⭣ Fine ⭣ -----------
for string in stringsArray {
if string.isEmpty {
break
}
doSomething(with: string)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment