Java Source Code Organization Part-4 | Rules about declaring variable, method, class and interface

Опубликовано: 27 Октябрь 2024
на канале: skCodify
14
2

11. Declaration Per Line: One declaration per line is recommended since it encourages commenting.
1. Don't declare variables and methods in one line at all.
2. Don't declare different type declaration in same line.

For-Loop is an exception of it.

12. Placement: Put declaration at the beginning of the blocks, don't wait for it be used. It can make other developer difficult to read the code. Avoid local declaration that can hide the higher level declaration.

13. Initialization: Try to initialize local variables where they’re declared. The only reason not to initialize a variable where it’s declared is if the initial value depends on some computation occurring first.

14. Class and Interface Declarations: When coding Java classes and interfaces, the following formatting rules should be followed:
1. No space between a method name and the parenthesis “(“ starting
its parameter list.
2. Open brace “{” appears at the end of the same line as the
declaration statement
3. Closing brace “}” starts a line by itself indented to match its
corresponding opening statement, except when it is a null statement
the “}” should appear immediately after the “{“