3 ms·
I'm so tired of writing null checks ... Could the null checks of Java be compared to the ones used in C#? My C# code is often littered with ternary operators t
by wsc981 13y ago
I'm so tired of writing null checks ...
Could the null checks of Java be compared to the ones used in C#? My C# code is often littered with ternary operators to deal with null values. And that's still not as safe as the Objective-C approach where one can just send messages to nil[0] which is really awesome imo.
I guess I kinda wonder if I could avoid the null checks in C# somehow ... I figured using a design by contract approach might be used to reduce the null checks somewhat.
[0]: http://stackoverflow.com/questions/156395/sending-a-message-to-nil http://stackoverflow.com/questions/156395/sending-a-message-...
- jussij 13y ago> I guess I kinda wonder if I could avoid the null checks in C# somehow Without seeing your code it is hard to say, but using an object extension might work? On many occasions I've used object extensions to hide checking code deep inside the extension. C# really got that feature right.
- creatio 13y agoCan you show an example of how this can be done?
- RyanZAG 13y ago> not as safe as the Objective-C approach where one can just send messages to nil[0] which is really awesome imo. It's definitely NOT awesome. This stupid behaviour causes bugs all the time as you think you're doing an action but it's actually a no-op because something else failed and you get a nil. An NPE is awesome - it crashes immediately and you know exactly what is wrong. A silent no-op is the worst thing to debug.
- rpeden 13y agoHave you found C#'s null coalescing operator useful in these situations at all? I've found it to be quite handy. http://msdn.microsoft.com/en-us/library/ms173224.aspx http://msdn.microsoft.com/en-us/library/ms173224.aspx