Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Online Resources For Java Coding Best Practices

...

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
  • 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

  1. Assuming objects are immutable
  2. Basing security checks on untrusted sources
  3. Ignoring changes to superclasses
  4. Neglecting to validate inputs
  5. Misusing public static variables
  6. 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.