Skip to content

Instantly share code, notes, and snippets.

@bobjackman
Created February 4, 2020 20:15
Show Gist options
  • Save bobjackman/ee27cf845d40cb9af03494f83bcf4f6c to your computer and use it in GitHub Desktop.
Save bobjackman/ee27cf845d40cb9af03494f83bcf4f6c to your computer and use it in GitHub Desktop.
Set value only if we don't already have one
class SomeClass {
String foobar;
void someMethod(String newValue) {
if (foobar == null) {
foobar = newValue;
}
}
// OR //
void someMethod(String newValue) {
foobar ??= newValue;
}
// OR //
void someMethod([String optionalNewValue]) {
foobar ??= (optionalNewValue??"defaultValue");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment