Code Formatting Flashcards
What is the recommended order of elements within a Java source file?
Beginning comments, Import statements, Class declarations, Class documentation comments, Class (static) variables (First public, then protected, then private), Constructors, and Methods.
What are beginning comments in a Java source file?
Beginning comments are a section of comments that provide an overview of the Java source file, including the purpose of the file and any other relevant information.
What is the purpose of import statements in a Java source file?
Import statements allow Java programs to access classes and methods from other Java packages or libraries.
How should class variables be ordered in a Java source file?
Class variables should be ordered by visibility, with public variables first, followed by protected, and then private.
What is the purpose of constructors in a Java class?
Constructors are special methods used to initialize objects of a class when they are created.
How should methods be ordered in a Java source file?
Methods should be ordered by functionality, with related methods grouped together.
What is the importance of following a recommended order in a Java source file?
Following a recommended order in a Java source file helps to improve the readability and maintainability of the code, making it easier for other developers to understand and modify the code in the future.
What is the recommended unit of indentation in Java source files?
The recommended unit of indentation in Java source files is four spaces.
Are tabs or white-space allowed for indentation in Java source files?
Yes, both tabs and white-space are allowed for indentation in Java source files.
What is MR’s comment on using tabs for indentation?
MR’s comment is that tabs are devilish.
What is the recommended maximum line length in Java source files?
The recommended maximum line length in Java source files is 80 characters.
Why is it recommended avoiding lines longer than 80 characters in Java source files?
It is recommended to avoid lines longer than 80 characters in Java source files to improve code readability and maintainability. Long lines can be difficult to read and understand, especially when working with code on smaller screens or devices.
Why is one declaration per line is preferred over multi-line declaration
Having one declaration per line makes the code more readable and easier to maintain. It also helps to avoid errors when modifying the code, since each declaration is clearly separated from the others. Additionally, it allows for easier tracking of changes in version control systems, as each declaration will have its own line in the commit history
What is the recommended practice for declaring variables in code?
The recommended practice is to put one declaration per line and to place declarations only at the beginning of blocks.
What is the reason for placing declarations only at the beginning of blocks?
Placing declarations only at the beginning of blocks helps improve code readability and maintainability by making it easier for developers to understand the scope of variables.