23 ms·
While I agree on reducing the amount (especially either too long or nested) ifs in the code, I disagree with some of the proposed patterns, especially the #3.
by tbronchain 8y ago
While I agree on reducing the amount (especially either too long or nested) ifs in the code, I disagree with some of the proposed patterns, especially the #3.
Sometimes we might want to use "null" rather than an empty collection to signal something different i.e null -> error, empty collection -> no result (avoiding the debate on whether we should use some exceptions for all error management).But generally speaking, creating a new object everytime instead of passing/checking null looks like a real potential loss of performances in some (not so rare) cases.
- jerrre 8y agoInstead of passing/returning null, there are much clearer things for all your examples, depending on the situation: Optionals, Empty Containers, Errors. Just using null communicates very little.
- rini17 8y agoEmpty container isn't much better communication aid, only in some specific situations. Much better would be for VM to save the stack at the "null" value creation time and later attach that stack trace to the NullPointerException. Perhaps it's finally time to adopt this from Lisp :D
- heavenlyblue 8y agoSaid so many times. Do not ever use nulls as flags. It’s incredibly like Python exception handling: it works well until you try to work with a somewhat complex codebase: eg paramiko. The IOErrors can be raised by any layer of the stack. So how do I handle this in the code? Well, turns out I can’t definitely tell one from another. Same is with the null values: never use nulls as a flag unless you’re absolutely sure that null value will not be boxed in another “Optional”.
- tbronchain 8y agoSo your understanding of the pattern #3 is that, null was being used instead of an empty list, and in this case it's better to use an empty container. In that case, it makes sense, that's not how I understood it at first. Oh and, I agree about the not using null thing at multiple levels.