4 ms·
I use null in data because Json. But otherwise I just leave undefined to play the role of "this optional argument wasn't defined" and null for everything else.
by Waterluvian 6y ago
I use null in data because Json.
But otherwise I just leave undefined to play the role of "this optional argument wasn't defined" and null for everything else. I like null because You have to specifically set something as null. I can run into undefined through typos and such. I dont think you can accidentally hit null.
P.s. This reminds me of the one time in Python I thought, "Damn I wish I had both undefined and None". I needed to know if a user was omitting an optional argument or passing an explicit None into it. Ie. This data is optional and the data might be "None". I ended up using kwargs which weakened my function as it nolonger described all the possible arguments.
- anuila 6y agoUndefined is “stored” the same way in JSON, which means it isn’t. This only becomes a “problem” when you convert a JSON string to an object and the data contains an array with a null value in it. That’s the only case where null exists in JSON, and I think it’s generally pretty rare.
- Waterluvian 6y agoNot sure I follow. JSON has null. It doesn't have undefined.
- toast0 6y agoYou said > But otherwise I just leave undefined to play the role of "this optional argument wasn't defined" And the reply is saying that's how JSON uses it too. Anything set to undefined will not be serialized, as if it were optional and not defined.
- Waterluvian 6y agoArguments belong to functions. Sorry I was wandering all over.
- Izkata 6y ago> Undefined is “stored” the same way in JSON, which means it isn’t. Null is: >> JSON.stringify({"foo": undefined}); "{}" >> JSON.stringify({"foo": null}); "{\"foo\":null}"
- anuila 6y agoThe point is, why use it at all if both values are equivalent to "missing"? I don't see a practical difference between undefined and null in the vast majority of cases, which is the the author is arguing. Again: In the vast majority of cases. I'm sure you'll find your exceptions.
- rbanffy 6y agoIn the first case I lost the information that the key exists and can contain a value. The second form makes it explicit. On the first case, the parser that's getting the data may want to calculate a sensible value. On the second one, it's clear the value is known.
- bpicolo 6y agoI've worked with a ton of APIs where these two aren't equivalent. Consider partial object updates. "null" means "explicitly set a field to null", whereas undefined means "don't change the value of the field". The distinction is meaningful.
- Buttons840 6y agoThe solution in Python is to create a sentinel value/object. You can even call it undefined: `undefined = object()`. `None is undefined` ==> false
- ZephyrBlu 6y agoIf you care about whether an optional argument is omitted or not in Python I would default it to false instead of None.
- xaedes 6y agoWhat about boolean optional arguments?
- im3w1l 6y ago> I needed to know if a user was omitting an optional argument or passing an explicit None into it. Sounds like kindof like an Optional<Optional<Value>>. An optional is just a container of at most one element, so you could emulate that with a singleton list, having None mean not specified, [value] be value, and [None] be explicit lack of value.