Created
February 4, 2020 20:15
-
-
Save bobjackman/ee27cf845d40cb9af03494f83bcf4f6c to your computer and use it in GitHub Desktop.
Set value only if we don't already have one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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