Contents:

Python Basics: A Free Course for All Skill Levels ➞ A unique opportunity for beginners and experienced programmers. Four impressive projects for your portfolio and the opportunity to communicate with an expert. Find out what skills you can acquire!
Learn MoreJava, like a number of other programming languages, has informal but widely accepted conventions regarding coding style. Let's try to understand what exactly these conventions are, how they work, and why following them matters.
Why Following a Consistent Coding Style Is Important
Java Code Style is a set of principles and conventions regarding code formatting. For example:
- Indentation can be used to create visual space between elements of text or graphics, which helps improve readability and comprehension. It is often used in paragraphs to separate one thought from another, as well as in lists, tables, and other visual components.
There are several things to keep in mind when working with indentation. First, maintain consistency: if you decide to use a certain indentation amount, try to use it throughout the entire document or project. This will create a harmonious and neat look. Secondly, it's worth keeping in mind the context: for example, scientific papers or business correspondence may require a more strict approach to formatting, while literary texts can be more free-form.
It is usually recommended to start indenting with the first paragraph, which can be highlighted by setting a standard indent at the beginning. It is also useful to use additional indents for subheadings or key points to draw attention to them. Keep in mind that indents may vary depending on the type of document and its purposes, so it is important to adapt them to specific tasks.
- The rules for using different types of brackets depend on the context and the purposes we pursue. Brackets can be used to add clarifications or additional information to the text, as well as to indicate the sequence of actions in mathematical expressions.
1. Round brackets ( ) are most often used to add comments, clarifying details that are not basic, but can supplement the information. For example: "Paris (the capital of France) is a city with a rich history." They are also used in mathematics to indicate the order of operations.
2. Square brackets [ ] are used to indicate insertions in quotations, as well as in cases where it is necessary to clarify information. For example: "As the author [in the book] notes, this event is of great significance." They can also be used in scientific and technical writing to indicate changes or additions to the original text.
3. Curly braces { } are less common in ordinary writing, but are widely used in programming and mathematics, where they are used to group elements or define the scope of variables.
It is important to remember that the placement of brackets must be logical and follow the rules of the language, so as not to confuse the reader and not distort the meaning of the expression.
- How to write comments correctly.
- Choosing a naming style for classes, variables, and constants is an important aspect of programming that affects the readability and maintainability of code.
For classes, the CamelCase style is often used, in which each word begins with a capital letter, which helps the class name stand out from other elements. For example, you could use names like `UserAccount` or `OrderProcessor`.
Variables are typically named in camelCase, where the first word starts with a lowercase letter and each subsequent word is capitalized. This makes it easy to recognize that you are talking about a variable. Examples include `userName` or `totalAmount`.
As for constants, it is common to name them in «UPPER_SNAKE_CASE». This style capitalizes all letters and separates words with underscores, making constants easily recognizable. Examples include `MAX_CONNECTIONS` and `DEFAULT_TIMEOUT`.
Following these guidelines helps create code that is easy to read and understand, and simplifies its maintenance and modification.
- Optimal line length of code is an important aspect when writing software. It is generally recommended that line length not exceed 80-120 characters. This is because longer lines can make code more difficult to read and understand, especially on small screens or in situations where multiple lines need to be compared. Furthermore, adhering to these guidelines helps improve the formatting and organization of code, which in turn makes it easier to maintain and modify in the future.
There are many norms and rules regarding the formatting of program source code, as well as requirements that should be taken into account when doing so. But where did these standards come from?
The reasons why programmers have adopted such agreements are clear and obvious:
- In software development, code maintenance and further development account for 70 to 80% of all costs.
- Coding is typically performed by teams of programmers, not individual specialists. The composition of these teams can change over time, and having a uniform coding style makes it easier for new developers to integrate into the project.
- Code is read much more often than it is written. Before making even a minor change, which may consist of just a few new lines, it is necessary to analyze hundreds, and sometimes thousands, of lines of existing code.
- Correct code formatting makes it easier to understand. Even if different teams work on the program, it will remain understandable for any developer, which will ensure quick understanding.
- Software maintenance and comparison of different versions of code are greatly simplified when files are formatted in a consistent style.
Using standard style conventions helps keep your code organized and helps avoid errors that arise due to different writing approaches when several people work on the same project.
Automatic code formatting in the integrated development environment
Every well-known development tool offers automatic code formatting features. The table below lists the keyboard shortcuts for formatting the current file in widely used IDEs.
To format a specific package or the entire project in IntelliJ IDEA, first select the desired package in the project tree. Then run the Reformat command using the keyboard shortcut. You can also right-click on the selected package and select "Reformat Code." The dialog box that appears will not only offer formatting but also optimize imports—removing unnecessary ones and organizing existing ones according to established code formatting rules. It is recommended to enable this option and then click OK.
The result of the auto-formatting team's work:

When choosing names, you should avoid abbreviations. This is due both to the fact that it is not always clear what they mean, and to the difficulties in finding classes and methods. Colleagues who work with the code may not recognize such names and decide that they need to be re-created.
Incorrect: class Btn, integer lstId, class CurPage, string lName.
Correct: class Button, variable int lastId, class CurrentPage, string lastName.
Represent abbreviations in their full form. This makes the text easier to read, especially when the name includes several words.
Incorrect: class HTMLPage, integer userID, class HTTPRequest, string URL, class XML.
✅ Correct: class HtmlPage, integer userId, class HttpRequest, string url, class Xml
Follow these simple guidelines and keep in mind that your code should be understandable not only to you but also to your colleagues. This will simplify their task, allowing them to quickly understand the code and focus on solving the problem, rather than wandering in the complexity of obscure class names, methods, and variables.

