Skip to content

Instantly share code, notes, and snippets.

@anqit
Created February 25, 2023 03:39
Show Gist options
  • Save anqit/cab11271c6d481729b457e5a1585aa88 to your computer and use it in GitHub Desktop.
Save anqit/cab11271c6d481729b457e5a1585aa88 to your computer and use it in GitHub Desktop.
Boolean compound assignment operators should short-circuit if possible
bool longComp() {
print('doing long computation');
return true;
}
void main() {
var t = true;
var f = false;
t |= longComp(); // prints 'doing long computation'
f &= longComp(); // prints 'doing long computation'
t = t || longComp(); // short-circuits, nothing is printed
f = f && longComp(); // short-circuits, nothing is printed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment