Skip to content

Instantly share code, notes, and snippets.

@dlwyatt
Last active August 29, 2015 14:11
Show Gist options
  • Save dlwyatt/eb16f15bbf75ce30e216 to your computer and use it in GitHub Desktop.
Save dlwyatt/eb16f15bbf75ce30e216 to your computer and use it in GitHub Desktop.
Comparing brace styles with long, multi-line conditions.
if ($reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberOne -and
$reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberTwo) {
Do-Something
}
# Versus:
if ($reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberOne -and
$reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberTwo)
{
Do-Something
}
# In the first version, the second line of the conditional is indented the same amount as the code
# inside the curly braces, making it harder to spot the break. Curly brace on its own line is one way
# to address this. Some others:
# Double indent of multi-line conditions, creates visual separation between the condition
# and the beginning of the body inside the curly brace.
if ($reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberOne -and
$reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberTwo) {
Do-Something
}
# Just don't write long conditions like that in the first place. :P Place that code into its own
# function, which is a good idea anyway, for code clarity.
function ShouldDoSomething {
return $reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberOne -and
$reallyLongConditionThatIDontFeelLikeMakingUpRightNowSoIllJustUseAVariableNameNumberTwo
}
if (ShouldDoSomething) {
Do-Something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment