![]() ![]() Those kinds of null checks cause some code logic to not trigger, without the ability to know about it. “ I think this cannot be null but just to make sure, I don’t want it to blow up production”.“I know I should check for null but I don’t know what it means when the function returns null and I don’t know what to do with it,” or.I encounter null checks which seems like the developer was thinking: Real-life code can be much more complicated. But the NullPointerException is raised inside a function of class B. ![]() In the following code example, there is a bug, somewhere in class A, causing entity to be null. I am happy now but I will blow up eventually. It makes tracing the real problem harder. The code in which the NullPointerException is raised can be very far from where the bug is. What are the problems with using nulls? It will blow up. When we want to find an entity by criteria in a collection, we return null as a way to say the entity was not found. When we are at the top, we need a way to say so, usually it is by returning null. In hierarchical models, we usually can navigate up and down. When there is no data for an optional property, it returns null. Missing optional data for entitiesĪn entity’s property can be optional. ![]() I think it is an old school programming style, originating in the time when exceptions didn’t exist. This is one way of returning error codes. Returning null when the input is invalid. Here are some patterns I noticed in functions returning null: Error handling
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |