Best Practices for Java Variable Naming and Layout Style
30 likes | 152 Vues
This guide outlines effective Java style conventions for variable naming and code layout. Variable names should start with a lowercase letter, using camel case for multiple words (e.g., `audioSystem`). Constants should be in all caps with underscores (e.g., `MAX_ITERATIONS`). Large scope variables can have longer names, while boolean variables should follow the `isX` format (e.g., `isVisible`). Code layout should maintain clarity with proper indentation, whitespace around operators, and blank lines between logical blocks. These practices enhance readability and maintainability of Java code.
Best Practices for Java Variable Naming and Layout Style
E N D
Presentation Transcript
Java Style • Variables: • Variable names start with lower case character and separate words by raising the first letter of successive words, e.g. line, audioSystem. • Names for constants use all capital letters and underscore “_” to separate words, e.g., MAX_ITERATIONS. • Variables with larger scope use longer names and otherwise use shorter names. • Boolean variables has the format isX, e.g., isVisible, isSet. • Abbreviations of words should be avoided. • Avoid using magic numbers; define constants instead. • Variables should be declared where they are used and localized in a small scope.
Java Style • Layouts: • Use indentation to increase clarity. • Put white spaces around operators and colons; put one white space after key words and commas; • Insert blank rows between logic statement blocks. • While statements use the following two styles: while (condition) { statements; } while (condition) { statements; } or if (condition) { statements; } else if (condition) { statements; } else { statements; } 6. If statements 5. Do-while Statements do { statements; } while (condition);
Java Style 7. Do-while Statements for (initialization; condition; update) { statements; }