Code Formatting Flashcards

1
Q

What is the recommended order of elements within a Java source file?

A

Beginning comments, Import statements, Class declarations, Class documentation comments, Class (static) variables (First public, then protected, then private), Constructors, and Methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are beginning comments in a Java source file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of import statements in a Java source file?

A

Import statements allow Java programs to access classes and methods from other Java packages or libraries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How should class variables be ordered in a Java source file?

A

Class variables should be ordered by visibility, with public variables first, followed by protected, and then private.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of constructors in a Java class?

A

Constructors are special methods used to initialize objects of a class when they are created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How should methods be ordered in a Java source file?

A

Methods should be ordered by functionality, with related methods grouped together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the importance of following a recommended order in a Java source file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the recommended unit of indentation in Java source files?

A

The recommended unit of indentation in Java source files is four spaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are tabs or white-space allowed for indentation in Java source files?

A

Yes, both tabs and white-space are allowed for indentation in Java source files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is MR’s comment on using tabs for indentation?

A

MR’s comment is that tabs are devilish.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the recommended maximum line length in Java source files?

A

The recommended maximum line length in Java source files is 80 characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it recommended avoiding lines longer than 80 characters in Java source files?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is one declaration per line is preferred over multi-line declaration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the recommended practice for declaring variables in code?

A

The recommended practice is to put one declaration per line and to place declarations only at the beginning of blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the reason for placing declarations only at the beginning of blocks?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a block in code?

A

A block is any code surrounded by curly braces, “{“, and “}”. This can include loops, conditional statements, functions, and more.

17
Q

Why is it important to follow best practices for declarations?

A

Following best practices for declarations can help improve code quality, readability, and maintainability. It can also help prevent bugs and make it easier for other developers to work with the code.

18
Q

What is the recommended placement of the open brace in a Java class declaration?

A

The open brace “{“ in a Java class declaration should appear at the end of the same line as the declaration statement.

19
Q

Where should the closing brace appear in a Java class declaration?

A

The closing brace “}” in a Java class declaration should start a line by itself and be indented to match its corresponding opening statement.

20
Q

Should there be any space between a method name and the parenthesis starting its parameter list in a Java class declaration?

A

No, there should not be any space between a method name and the parenthesis “(“ starting its parameter list in a Java class declaration.

21
Q

How should methods be separated in a Java class declaration?

A

Methods in a Java class declaration should be separated by a blank line.

22
Q

What is the recommended spacing for a keyword followed by a parenthesis?

A

A space should be used to separate a keyword and its following parenthesis.

23
Q

Should there be a space between a method name and its opening parenthesis?

A

No, there should be no blank space between a method name and its opening parenthesis.

24
Q

Should there be a blank space after commas in argument lists?

A

Yes, a blank space should be used after commas in argument lists.

25
Q

Should binary operators be separated from their operands by spaces?

A

Yes, all binary operators except for the dot operator should be separated from their operands by spaces.

26
Q

Should unary operators be separated from their operands by spaces?

A

No, unary operators such as unary minus, increment, and decrement should not be separated from their operands by blank spaces.

27
Q

What is Checkstyle?

A

Checkstyle is a development tool used to check whether Java code adheres to a specified coding standard.

28
Q

Why is Checkstyle useful?

A

Checkstyle is useful because it automates the process of checking Java code for adherence to a coding standard. This can help to ensure consistency in coding practices and reduce the potential for errors.

29
Q

What does Checkstyle check for?

A

Checkstyle checks for a wide range of coding practices, including indentation, line length, variable names, and spacing.

30
Q

Can Checkstyle be customized to adhere to specific coding standards?

A

Yes, Checkstyle can be customized to adhere to specific coding standards. It provides a range of pre-defined checks, and developers can also write their own custom checks.

31
Q

Is Checkstyle limited to Java code?

A

Yes, Checkstyle is designed specifically for Java code.

32
Q

Can Checkstyle be integrated with other development tools?

A

Yes, Checkstyle can be integrated with a range of other development tools, including build systems and integrated development environments (IDEs).

33
Q

Does Checkstyle fix code issues automatically?

A

No, Checkstyle does not fix code issues automatically. Instead, it provides developers with feedback on code quality and adherence to coding standards. It is up to the developers to make any necessary changes.