2012-09-04

Providing default values if null or empty

So, I was curious about wether there were a better way to do stuff like:


_parsedBetData["prizeLevel"] = params["prizeLevel"] == null ? "default" : params["prizeLevel"];
And posted my question on StackOverflow turns out this could be solved with the help of a regular ||=. 
So that specific code can be simplified as
_parsedBetData["prizeLevel"] = params["prizeLevel"] || "default";
If you are interested in providing a default value if _parsedBetData["prizeLevel"] is null or empty that can simply be done by:
_parsedBetData["prizeLevel"] ||= "default";
It's important to note that the check would return false for both and empty string and null.

Inga kommentarer:

Skicka en kommentar