Skip to main content

Code clean up suggestions.

No replies
peter_lawrey
Offline
Joined: 2005-02-24

I have found the following useful in cleaning up code for projects in the past. This took about 2 days to do java.* and javax.*

Does anyone have any comments or futher suggestions.

- Have fields which are set in the constructor or static block and not changed as final. In the diff file I have done only private and package private only.
- Have methods and fields which can be static made static. Private and package private methods and fields only.
- Remove redundant throws clauses. Private and package private methods only.
- Have methods and field which are not used in another class made private. Package private only
- Make protected methods in final classes made package private.
- Remove redundant imports. Including importing the same class more than once, importing classes in the same package and classes from java.lang.
- Inner classes which can be made static should be static.
- Avoid using transient fields and serialVersionUID in classes which are not Serializable.
- IMHO, Initialising static variables inside a constructor is a bad practice. E.g. HorizontalSliderThumbIcon(), VerticalSliderThumbIcon(), MetalScrollButton.
- replace "transient static" with just "static"
- use asSubClass() e.g.
Class.forName(nm).asSubclass(PrinterJob.class).newInstance();