Elegant Java code is a thing. Ask any straight-A coding student who has tried to help a non-performing class mate. “Bad” code is difficult to understand. By contrast elegant code is easy, quick and a breeze to troubleshoot.
So what makes elegant code?
It starts with following Java coding conventions. Only classes start with a capital letter – variables and methods start with a small letter.
![]() |
![]() |
Layout conventions like indentation must be used to group coding ideas together. Pairs of curly brackets are kept one above the other.
Blocks of code that do the same thing, but with different variables, are laid out in the same way. Here block 2 is a mirror image of block 3 below.

When declaring a large amount of variables group them together into logical families and name them in a consistent manner. The variable names must be structured and descriptive without being too long. Comments are used to describe the functions of blocks of code.

Classes start with your name and a brief description of what the program does. This is especially important for your PAT projects.

OOP programming makes provision for input and output to be mainly handled by the UI class. The manager class with its various methods does not generally print to the monitor. The output report of a program should be contained within one method, not scattered around the manager class.

The “wholeReport” method above in the manager class passes its report to the UI for printing to the monitor – see block 3 below. Note that the layout of a large method like this is predictable, structured and easy to understand. The methods in block 2 below only determine values but output is left to a different method.

The use of whitespace is used to group like code together. Block 2 below gets input from the keyboard and block 3 parses the same input from String to double.

A method should only accomplish one task. The constructor method can be longer than other methods because its ONE job is to initialise and setup the program for use.

There is no redundant code or variables. All code is needed and all variables are used.
Lines of code are writtent to be understood. Long lines of code with nested and multiple commands all chained together are not encouraged because at some point a third party has to read, understand, edit or update the code.
