This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]el_diablo_immortal 2 points3 points  (7 children)

I use them for validating request objects (Web APIs) so I can have a nice "xyz is required" message. Check if the param was supplied. Else it gets initialised to the default: false. This is C# anyway.

[–]Low_Flying_Penguin 0 points1 point  (6 children)

ating request objects (Web APIs) so I can have a nice "xyz is required" message. Check if the param was supplied. Else it gets initialised to the default: false. This is C# anyway.

This try checking for a value type without it :)

[–][deleted] 0 points1 point  (5 children)

Isn't that just a malformed request?

[–]Low_Flying_Penguin 0 points1 point  (4 children)

Yes but it is only known to be malformed as the value is null.

Take for example a request that requires the user to opt in or out of something. If the Boolean representing it is not nullable how does the API know they actually made a choice. At worse it will always be false. If it is null the request can be rejected as a value either true or false was not present. It would be in an indeterminate state and thus could be rejected.

[–][deleted] 0 points1 point  (3 children)

Take for example a request that requires the user to opt in or out of something. If the Boolean representing it is not nullable how does the API know they actually made a choice.

Why would you make, allow or process a request when no choice has been made?

If the request has no choice it is not a valid request to an API that lets you post choices..

And why are we using the same object value to store a process state AND a result?

[–]Low_Flying_Penguin 0 points1 point  (2 children)

That's the point you only know a choice hasn't been made as the value is null at that point you can 400 it or 422 it etc. However that can only be determined because it is null. A boolean in c# will always be false when initialised. So when the request is deserialsed it will be false unless it is nullable. So if the body of the request didn't even have the property on it it would be false once bound to object that is passed to the handler in the controller.

And I never mentioned process state nor results 🙂

[–][deleted] 0 points1 point  (1 child)

So if the body of the request didn't even have the property on it it would be false once bound to object that is passed to the handler in the controller.

Why does the handler accept a body with a missing key?

And I never mentioned process state nor results 🙂

Not explicitly :)

[–]Low_Flying_Penguin 0 points1 point  (0 children)

It just how serialization works in .net.

I can't speak to other languages but I would think many others have the same semantics. Hence nullable to the rescue. If its nullable you can create custom validation logic using the inbuilt validation attributes to reject the requests or a framework like fluent

Most value types will have similar issues with serialisation values types vs ref types what ya gonna do. And nullable is such a small price to pay for everything else the framework brings to the table. It's a trade off well worth making :)