0% found this document useful (0 votes)
62 views3 pages

Google Java Best Practices

Uploaded by

Anish Roshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

Google Java Best Practices

Uploaded by

Anish Roshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Google Java Best Practices - Summary Guide

1. Naming Conventions

- Class & Interface: UpperCamelCase (e.g., MyServiceManager)

- Method & Variable: lowerCamelCase (e.g., getUserData)

- Constants: UPPER_UNDERSCORE (e.g., MAX_RETRY_COUNT)

- Package names: lowercase, no underscore (e.g., com.google.auth)

2. Formatting & Structure

- Use 2-space indentation, no tabs

- Braces on the same line: if (x) {

...

- No wildcard imports (use java.util.List, not java.util.*)

3. Classes and Interfaces

- One top-level class per file

- Make fields private and use getters/setters

- Use final for immutability

4. Methods

- Keep short and focused (<= 40 lines)

- Always specify visibility (public/private/etc.)

- Use descriptive names: calculateSum(), not c()

5. Control Structures

- Use switch over multiple if-else when possible

- Always have default case in switch

- Avoid deeply nested code

6. Error Handling

- Catch specific exceptions

- Log errors, don't swallow them

Page 1
Google Java Best Practices - Summary Guide

- Use try-with-resources for AutoCloseables

7. Null Handling

- Avoid returning null; use Optional

- Use Objects.requireNonNull() for input validation

8. Collections and Streams

- Use List over ArrayList in declarations

- Avoid mutation in streams; use pure functions

- Prefer for-each over index loop

9. Comments

- Use Javadoc for public APIs

- Keep comments meaningful and concise

10. Thread Safety & Concurrency

- Use concurrent utils (Executors, ConcurrentHashMap, etc.)

- Avoid synchronized unless needed

11. Code Quality

- DRY, KISS principles

- Avoid magic numbers - use constants

- Write unit tests for logic branches

12. Tools & Linters

- Use Checkstyle, Error Prone

- Use AutoValue, Guava, Truth, JUnit

Bonus: Don'ts

X Don't use raw types like List

Page 2
Google Java Best Practices - Summary Guide

X Don't catch Exception/Throwable unless needed

X Don't use System.out.println(); use logger

Page 3

You might also like