| Start |
|---|
| Pitfall - "Making good" unexpected nulls On StackOverflow, we often see code like this in Answers: Often, this is accompanied with an assertion that is "best practice" to test for Is it best practice? In short: No. There are some underlying assumptions that need to be questioned before we can say if it is a good idea to do this in our What does it mean for "a" or "b" to be null?A Did the null come from an uninitialized variable?A Does the null represent a "don't know" or "missing value"?Sometimes a If this is a bug (or a design error) should we "make good"?One interpretation of the code is that we are "making good" an unexpected The problem with "making good" is that it is liable to either hide the problem, or make it harder to diagnose. Is this efficient / good for code quality?If the "make good" approach is used consistently, your code is going to contain a lot of "defensive" null tests. This is going to make it longer and harder to read. Furthermore, all of this testing and "making good" is liable to impact on the performance of your application. In summaryIf Otherwise, it is a better idea to treat an unexpected |
| Pitfall - Using null to represent an empty array or collection Some programmers think that it is a good idea to save space by using a The first version is how you would normally code the method: The second version is how you need to code the method if you are in the habit of using As you can see, the code is a bit more complicated. This is directly attributable to the decision to use Now consider if this array that might be a The lesson here is to use empty arrays and empty lists when that is what you mean. The space overhead is small, and there are other ways to minimize it if this this is a worthwhile thing to do. |
| Pitfall - Returning null instead of throwing an exception Some Java programmers have a general aversion to throwing or propagating exceptions. This leads to code like the following: } So what is the problem with that? The problem is that the There are actually three problems here:
In fact, assuming that the exception did need to be caught early like this, there were a couple of alternatives to returning
|
| Pitfall - Not checking if an I/O stream isn't even initialized when closing it To prevent memory leaks, one should not forget to close an input stream or an output stream whose job is done. This is usually done with a While the above code might look innocent, it has a flaw that can make debugging impossible. If the line where To prevent this, simply make sure the stream isn't An even better approach is to |
| Pitfall - Unnecessary use of Primitive Wrappers can lead to NullPointerExceptions Sometimes, programmers who are new Java will use primitive types and wrappers interchangeably. This can lead to problems. Consider this example: Our When we try to use the default initialized fields, we see that the There are a couple of ways to look at this: The lesson here is to not use primitive wrapper types unless you really need to. 1 - This class is not an example of good coding practice. For instance, a well-designed class would not have public fields. However, that is not the point of this example. |
| Pitfall - Using "Yoda notation" to avoid NullPointerException A lot of example code posted on StackOverflow includes snippets like this: This does "prevent" or "avoid" a possible is better than: (It is more concise, and in some circumstances it might be more efficient. However, as we argue below, conciseness could be a negative.) However, the real pitfall is using the Yoda test to avoid When you write This means that Yoda conditions are not "best practice"1. Unless the Yoda conditions should only be used in cases where the 1 - According to Wikipedia (opens new window): "Best coding practices are a set of informal rules that the software development community has learned over time which can help improve the quality of software.". Using Yoda notation does not achieve this. In a lot of situations, it makes the code worse. RemarksThe value
The most common root causes for NPEs:
Examples of commonly used methods that return
There are strategies for avoiding unwanted NPEs, such as explicitly testing for |
Read Article
https://develop.horje.com/learn/1434/reference