Online Resources For Java Coding Best Practices
- Java: Code Conventions for the Java Programming Language
- Java Practices
- Writing Robust Java Code
...
SAKAI
Common Java coding problems
Solution 1:
Code Block |
---|
someString.equals("");
|
- Pros:
- readable
- Cons:
- possible null pointer exception if someString is null
- create extra String object (""). Overhead can be reduced by using static final String EMPTY_STRING = "";
- not very efficient because equals first does a string length comparison
Solution 2:
Code Block |
---|
public boolean isNotNullAndEmpty(String str) {
if((null != str) && (str.length() == 0)) {
return true;
}
else {
return false;
}
// ... or just
// return (str == null) ? false : str.length() == 0;
}
|
- Pros:
- tests for null string
- efficient
- abstraction
- Cons:
- a bit more code to write
Solution 3:
Code Block |
---|
"".equals(someString);
|
- Pros:
- avoids the null pointer exception issue that Solution 1 has
- readable but awkward at first. Maybe use EMPTY_STRING.equals(someString)
- Cons:
- create extra String object (""). Overhead can be reduced by using static final String EMPTY_STRING = "";
- not very efficient because equals first does a string length comparison
Conclusion
...
Common Java Security Anti-patterns
Common Java Platform Antipatterns
- Assuming objects are immutable
- Basing security checks on untrusted sources
- Ignoring changes to superclasses
- Neglecting to validate inputs
- Misusing public static variables
- Believing a constructor exception destroys
the object
This powerpoint presentation comes from the JavaOne 2006 presentation entitled 'Common Java Security Anti-patterns'. It contains code examples of each of the above antipatterns.