Naming conventions

Keep some basic principles in mind when writing code.

Starting Java programmers make often mistakes in the naming conventions. Which makes their code difficult to read.
Try to keep the following rules in mind when you write your first Java code:

  • Start class names with a capital letter.
  • Start interface names with a capital letter.
  • Start object and variable names with small letter, internal words start with capital letters..
  • Encapsulate fields using get and set methods. getVariable() setVariable().
  • Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
  • Only use small letters for package names.

KISS! (Keep It Simple Stupid)

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
> Martin Fowler

How to keep your code simple?

  • Use clear variable names. Do not abbreviate variable names too much! (for e.g. do not write: 'r' but write 'row')
  • Keep each method short! (guideline: maximum 15 to 20 lines for a method).
  • Keep in mind the 'naming conventions' of the Java programming language.
  • Do not fear to start all over! In the end you may gain time!
  • NEVER copy paste code. Write your code so you can re-use it.
  • Refactor, refactor, refactor. Rename and move variables, methods, classnames so it keeps clean and tidy
  • Someone that is not a programmer should be able to understand each method
  • Comments should not be needed in your code, the code should document itself!
  • 'Replace temp by query'. This means: Try not to use 'temporary' variables.
  • Do not try to write 'performant' code. Only if your code gets slow try to optimize it. But use a profiler before optimizing!

Do not re-invent the wheel

Do not loose time re-writing something that already exists.
First check the Java API's or the internet before programming.

Use of API's and libraries.

  • Browse the Java API so you know it's possibilities
  • Search for (open source) libraries that can speed up the development of your project.

Use the right tools (IDE)

I see a lot of starting programmers/students wrestle with a basic text editor.
For each change they first have to compile and run the code seperatly.
Use a good IDE like for exemple 'eclipse'. Write your code and just press 'run' to test it.

Advantages of the use of an IDE.

  • Just press 1 time 'run' to test your code
  • Code highlighting + compiler errors while you are writing the code.
  • Refactoring capabilities (rename methods, variable names automaticually)
  • To go back to your texteditor (when presenting your code at school for exemple), just copy-paste the .java files of your project. At school nobody needs to know you used eclipse. ;)
  • Use CTRL+SPACE to use 'code completion' function. (To avoid typos and misspelled variable names)