Java and OO Best Practices

This is in no way a comprehensive discussion on Java and OO Best
Practices. This is a subset of the knowledge we pass around with each
other. All of this is subject to programmer opinion and there’s a lot
more that needs to be learned.

bad practices (“code smells”)

- duplicate code
Have you ever copy-pasted your code?
Follow the “Once and Only Once” rrule.

- accessible fields

Tight coupling, corruptible, inflexible.
Should have accessors and mutators.
Problems with threading.

- large class

A class should only be doing one thing. Follow the “one
responsibility rule.”

- large method
- using magic literals
- unnecessary class/instance variables

If it’s only used in one method, make it local

- Initializing strings with “new”
- Using floats and doubles for currency calculations
- Returning null
- Subclassing for functionality
- Muffling exceptions
- Unexceptional exceptions
- Excessive use of switches
- Middle man: methods that just delegate to other classes. Case of over-design.

good practices
- validate your parameters
- abstract or final
- create local copies to prevent corruption (hmm… good for multithreading, but heck slow)
- String, string buffer
- favor immutability (now, there’s a thought…)

software engineering best practices

Note to self: should have webcam-based project that tracks red laser pointer.