7 ms·
There are patterns that experienced developers will use to avoid issues with nulls. Here are a couple off the top of my head. 1) Reversing string comparisons:
by justin_oaks 4y ago
There are patterns that experienced developers will use to avoid issues with nulls. Here are a couple off the top of my head.
1) Reversing string comparisons:
"literal".equals(variable)
instead of
variable.equals("literal")
2) Always initializing lists instead of leaving them to default to null
public class SomeClass{
private List someList=new ArrayList(); //or Collections.emptyList()
...
}
The problems with NullPointerExceptions can mostly be solved by teaching programmers to use these strategies and teaching them to look for boundaries where nulls can slip in.
The real problem happens when things get scaled up. If you have thousands of developers then it makes sense to take steps to automate the problem away. Also, if the code is used by billions of users then even rare NullPointerExceptions will happen frequently. So it makes perfect sense for Facebook to be the one to work on this.
- ben7799 4y agoThe funny thing is these techniques were introduced in books like "Effective Java" before many of the developers who complain were even born. Everyone should know these things by now. They are good things to ask about in interviews.