Do not break portability
Portability across different operating systems is one of the principal advantages of using Java.
Here are some ways to ensure your application remains portable:
- don't use hard coded file names and paths - use the File class, and allow file paths to be configured during deployment.
- remember that some systems have case sensitive file names (Unix), while others do not (Windows)
- be very wary of Runtime.exec and Method.invoke
- don't rely on thread scheduling and thread priorities to define program logic
- don't use native methods
- don't hard code sizes and positions of GUI elements - use a LayoutManager instead
- don't rely on a specific screen resolution - use java.awt.Toolkit.getScreenResolution instead
- don't hard code colors using numeric values - use the symbolic constants in java.awt.Color and java.awt.SystemColor instead
- don't hard code text sizes
- use the System.getProperty(String) method to refer to items which depend on the system, such as line terminators and path separators
Common System.getProperty(String) items can be placed in a general purpose
constants class :
/** * Collected constants of very general utility. * * All constants must be immutable. * No instances of this class can be constructed. */ public final class Consts { /** * Prevent object construction outside of this class. */ private Consts(){ //empty } /** * Only refer to primitives and immutable objects. * * Arrays present a problem since arrays are always mutable. * DO NOT USE public static final array fields. * One style is to instead use an unmodifiable List, built in a * static initializer block. * * Another style is to use a private array and wrap it up like so: * <pre> * private static final Vehicle[] PRIVATE_VEHICLES = {...}; * public static final List VEHICLES = * Collections.unmodifiableList(Arrays.asList(PRIVATE_VEHICLES)); * </pre> */ //characters public static final String NEW_LINE = System.getProperty("line.separator"); public static final String FILE_SEPARATOR = System.getProperty("file.separator"); public static final String PATH_SEPARATOR = System.getProperty("path.separator"); public static final String EMPTY_STRING = ""; public static final String SPACE = " "; public static final String PERIOD = "."; public static final String TAB = "\t"; //algebraic signs public static final int POSITIVE = 1; public static final int NEGATIVE = -1; public static final String PLUS_SIGN = "+"; public static final String NEGATIVE_SIGN = "-"; }
See Also :
Would you use this technique?
|
|