Code editor app

Good Programming Practices

The biggest challenge for software development companies is to create robust systems, in the shortest possible time and at a low cost. For this to happen, it is essential to adopt good practices during programming, which is presented in this article.

This is the first part of a series of articles that will address good programming practices. To start, think of an orchard of any kind of fruit; if it is planted within specific rules such as spacing between trees, fertilization, pruned correctly and at the right time, it will generate more, better and bigger fruits. This technique is passed on from generation to generation, this way, anyone will know how to proceed with the maintenance of the orchard and the result should always be the same. We can extend this thinking to software development, in which the use of rules and good practices generate better code readability, contributing significantly to the systems development cycle to occur in a more agile, practical and easy to maintain manner.

Indentation – Organizing the code

Every programmer starts his career with Algorithms and Programming Logic, and this is where he should be guided on the best practices that should accompany him for the rest of his life. The first teaching to be passed on is also the simplest of all, but if not used properly, it will make learning and reading the code very difficult, it is indentation.

It is very common to see beginners in programming creating their algorithms in a disorganized and misaligned way. We call this non-indented code, as shown in Listing 1. The difficulty of understanding it is visible, even compromising the learning process. Even a professional needs to analyze calmly and patiently to understand the algorithm, even if it has few lines. Now imagine a system with hundreds of them. Development in this way is practically impossible.


package br.com.javamagazine;
class Vendas implements InterfaceVendas{
public String nomeCliente;
protected String descProduto;
private double valor=50;
public double altera_valor(double valor){
this.valor = valor;
return valor;
}
public void imprime(double valor){
if (valor>100)
JOptionPane.showMessageDialog(null,"Valor acima do permitido");
else JOptionPane.showMessageDialog(null,"Valor="+valor);
}
}
 

In Listing 2, we see that the same code, now indented, is much more readable, clean and aesthetically more beautiful. Even if you program on your own, you should adopt this good practice, and if you share the source with other people, it is mandatory so that the project does not fall into that small great detail.


package br.com.javamagazine;

  import javax.swing.JOptionPane;

  class Vendas implements InterfaceVendas {

    public String nomeCliente;
    protected String descProduto;
    private double valor = 50;

    public double alteraValor(double valor){
      this.valor = valor;
      return valor;
    }

    public void imprimeValor(double valor){
      if (valor > 100)
        JOptionPane.showMessageDialog(null,"Valor acima do permitido");
      else
        JOptionPane.showMessageDialog(null,"Valor="+valor);
    }

  }
 

In some programming languages, indenting is so important that it is part of its development. This means that to start and close blocks of code, you don’t need to type identifiers like {and}, you just need to understand that the language compiler can interpret the beginning and end of the block.

Naming conventions for classes, methods, variables

Using the good practices already mentioned and adding naming conventions to it, you will make reading the program much easier. By placing suitable and standardized names, you will pass information that will help in understanding the code, indicating, for example, what a variable will store or what a particular method will do. If you add a good naming convention to this, it will help you a lot in the system development cycle, as we can see below:

Packages

The first naming convention that will be shown is for packages. Imagine that you will develop a system for an educational institution, for example Oxford University. During development, we must group the classes according to their objectives. For example, for classes responsible for the user interface, we can define the package co.uk.oxford.view. Looking at it, we can easily identify that it should be written in a similar way to a web address, only backwards. And at the end, we indicate a name (or a set of names, preferably separated by “.”), Which classifies the grouped classes;

Classes and Interfaces

Still analyzing Figure 1 , observe the class called Conexao and notice that the class names start with a capital letter, being simple and descriptive. When the class has a compound name, such as Main Menu, the first character of each word must always be capitalized. These rules are also applied for Interfaces;

Methods

What differs from the class name convention to method names is that for methods the first letter must be lowercase. As the methods are created to perform some procedure, try to use verbs for their names, for example: printValue ();

Variables

Use the same convention adopted for Methods, always creating short (note, firstName, mediaStudent) and meaningful names, in which by tapping the name, the programmer already knows what information the variable is going to store. a character (i, j, x, y), except for those used for indexes in repetition loops where the algorithm traverses vectors. In final (constant) variables, that is, that have a fixed value, all letters must be capitalized and with the words separated by an underscore (“_”), for example: THIS_CONSTANT;