Conventions and Formatting of Java Code for Introductory Computer Science Students

by mtimm2525 in Teachers > University+

22 Views, 0 Favorites, 0 Comments

Conventions and Formatting of Java Code for Introductory Computer Science Students

image_2025-03-26_212616581.png

This technical description is here to serve as a guide to describe the fundamentals of writing code for introductory classes, especially in university. There are many guidelines that may be overwhelming for new students, and this is here to make that experience more approachable.

Preparation

image_2025-03-26_211732048.png

Use clear and consistent naming conventions for variables and functions.

Keep functions short—aim for a single responsibility per function. Do not attempt to complete all requirements in one line of code, it is more difficult to trouble shoot errors and is more difficult for someone else to read your code.

Break complex logic into smaller, reusable function. If you need a variable sourced from other variables, save the new one as it's own instead of re doing the process every time you need it.

Writing the Code

image_2025-03-26_211911396.png

Leave comments in your code that explain what it does. This is both useful for yourself and others. It is helpful for others so they can know your intentions behind the code without having to guess the purpose based on the code itself. It is useful to you so that when you come back later you will know what you wanted for sure instead of just trusting your memory.

Comments should be no longer than two sentences. Comments do not need to be complex and elaborate on why you want it to do what it does, it just needs to be a simple description that gets the point of the code across.

Reviewing Your Code

image_2025-03-26_211951331.png

When reviewing your code, make sure it follows the project's formatting and style guide. Consistent formatting makes the code easier to read and maintain.

Handle errors properly instead of ignoring them. Clear error messages help with debugging and make the code more reliable.

Write unit tests for important functions to catch issues early and prevent future problems. Testing ensures your code works as expected.

Add comments and documentation to explain your code. This makes it easier for others (and your future self) to understand.

Keep your code efficient by removing unnecessary duplication. Reuse functions where possible to keep things clean and organized.