OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Comments, Programming Style
Comments
The contents
of a comment are ignored by the
compiler. Java supports
three styles of comments.
o
Single-line comment
o
Multi-line comment
o
Documentation comment
Java supports three types of comments. Comments are non-executable lines meant for documentation and readability.
1. Single-line Comment
Used for brief notes or explanations.
2. Multi-line Comment
Used for detailed documentation or disabling a block of code.
3. Documentation Comment
Used to generate external documentation using javadoc
. Applied above classes, methods, or fields.
Programming Style
Java is a free form language. We need not have to indent any lines
to make the program work properly. Java
system does not care when on the line we begin typing. While this may be a
license for bad programming style. We should try to use this fact to
our advantage for producing
readable programs.
Although several alternate
styles are possible,
we should select one and try to use it with total consistency.
Practice for
good programming style: ü Appropriate comments ü Naming conventions ü Proper indentation and spacing lines ü Block styles
System.out.println(“Java is Wonderful!”);
can be written as System.out.println (“Java is Wonderful!”);
or, even as
System. out.println (“Java is Wonderful!”);
A clean and consistent coding style improves readability, maintainability, and collaboration.
1. Naming Conventions
Type | Convention Example |
---|---|
Class | PascalCase – StudentInfo |
Method | camelCase – getDetails() |
Variable | camelCase – studentName |
Constant | UPPER_CASE – MAX_SIZE |
2. Indentation and Braces
Use 4 spaces per indentation level (or tabs as per project standard). Always use braces, even for single-line blocks.
3. Line Length
Keep lines within 80–100 characters for readability.
4. White Space Usage
Use spaces around operators and after commas:
5. Method Structure
Keep methods small and focused. Use meaningful names.
6. Comment Wisely
Don't over-comment obvious code. Use comments to explain “why,” not “what”:
7. Avoid Magic Numbers
Use constants instead of hardcoding numbers:
8. Code Grouping
Group related methods together and follow logical order: fields → constructor → methods.
Example with Good Style and Comments:
Comments
Post a Comment