#assumptions
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
In general, it is a good practice to always check for null when a getter can possibly return null, regardless of the context. Null checks help to prevent NullPointerException errors and ensure that your code is more robust and defensive.
Even if in a certain context a field is expected to be non-null, it is still possible for the getter method to return null due to unforeseen circumstances or changes in the code. Therefore, it is safer to always perform a null check before using the returned value to avoid unexpected bugs or issues.
Assuming that a getter will never return null without explicitly checking can lead to potential bugs and errors in your code. It is better to be cautious and include null checks when necessary, even if it may seem unnecessary in certain contexts.
In conclusion, it is recommended to always check for null when using a getter that can possibly return null, as this practice helps improve the reliability and stability of your code.
You should make so such case never happen, or rather, put this into compile time
For example, if your object A can return a foo and a bar, and both can be null, but they are always null at the same time, so if foo is null, bar is null. Instead, create an object B which has a foo and a bar non null and this B can be nullable. So here, you cannot do a mistake, because if you get this B, then you know foo and bar are not null, it's impossible to do the mistake