Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Coding Style

Uploaded by

louenpoubelle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Coding Style

Uploaded by

louenpoubelle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Coding style

These rules are not meant to be followed to the letter: they are provided to encourage developers to
establish and apply guidelines when writing code, especially in a team setting. The proposed rules
are commonly used standards. However, there is no absolute rule, and each person may choose to
use all or part of the suggested guidelines.

General Programming Best Practices


1. Code Readability
◦ Write self-explanatory code by choosing clear variable and method names.
◦ Avoid nested logic when possible; simplify with helper methods or clear
conditionals.
◦ Format your code consistently to improve readability.

2. DRY Principle (Don't Repeat Yourself)


◦ Avoid duplicate code by using functions or modules where applicable.
◦ Write reusable code by breaking down large functions into smaller, testable parts.

3. Modularization
◦ Break down the code into small, single-purpose functions or methods.
◦ Follow the Single Responsibility Principle (SRP), ensuring that each class or
method has one responsibility.

4. Consistent Error Handling


◦ Implement a consistent error handling approach throughout your application.
◦ Log errors in a centralized manner and ensure that user-friendly messages are
displayed to the end-user where needed.

5. Code Review and Refactoring


◦ Regularly review your code for improvements, even after it’s working.
◦ Refactor code to make it more ef cient, readable, or to remove any unused parts.
◦ Perform peer reviews to catch bugs and learn from others.

6. Version Control
◦ Use a version control system like Git to track changes in your code.
◦ Make frequent, small commits with clear messages that describe the change.

7. Documentation
◦ Keep your code well-documented, both inline and through external documentation.
◦ Update documentation whenever you change the functionality of a method or class.
fi
8. Testing
◦ Write unit tests to verify that individual pieces of code work as expected.
◦ Test edge cases and ensure that error-handling paths work properly.
◦ Regularly run tests to catch bugs early.

Java Coding Standards


1. Naming Conventions

◦ Classes: Use PascalCase (example, CustomerData, OrderProcessor).


◦ Methods: Use camelCase and describe actions (example : calculateTotal(),
updateRecord()).
◦ Variables: Use camelCase for readability (example : orderCount, userName).
◦ Constants: Use ALL_CAPS with underscores (example : MAX_LIMIT,
DEFAULT_TIMEOUT).

2. Code Structure and Formatting

◦ Use 4 spaces for indentation. Avoid using tabs for consistency.


◦ Place opening { braces on the same line as the declaration, example: public void
doSomething() {.
◦ Add spaces around operators for readability (example : a + b, x == y).
◦ Limit lines to 80-120 characters to improve readability across devices.

3. Comments and Documentation

◦ Write Javadoc comments for classes and methods to explain their purpose and
usage. Use /** ... */ syntax.
◦ Add inline comments sparingly to clarify complex logic, but avoid obvious
comments.
◦ Avoid excessive comments; instead, write clear and self-explanatory code.

4. Error Handling

◦ Always handle exceptions using try-catch blocks where appropriate.


◦ Use speci c exceptions (example : NullPointerException) rather than generic
exceptions .
◦ In the catch block, log meaningful error messages and avoid silent failures.

5. Use of « nal »

◦ Use the final keyword where applicable to mark variables, methods, or classes
that should not be modi ed.
◦ This helps in improving code safety and communicating intent to other developers.
fi
fi
fi
6. Avoid Magic Numbers

◦ Use constants instead of hard-coded numbers or strings to improve readability and


maintainability.
◦ Example: Instead of if (age > 18), declare a constant private static nal int
LEGAL_AGE = 18; and use it in the code.

fi

You might also like