Is it wrong to show something to the user?

5
Well I'm starting now in the poo area, I saw in a class where I'm watching on YouTube that it was wrong Methods write something on the screen, and how would I show if something wrong happened in a particular action that the user did .

Example Increase the volume of a television control

public void aumentar() {
 int resultado = this.getVolume() + 1;
  if(resultado > 10){
    System.out.println("Você aumentou mais do que o permitido")
  }
  else{
    this.getVolume(setVolume+1);
  }
}
    
asked by anonymous 27.06.2018 / 15:59

1 answer

5

It is "wrong" to use any type of output that demonstrates what it has on your system, such as printStackTrace for example. Because, in addition to the messy, ugly screen and the user does not understand anything, someone more understood can see the names of classes, methods, and depending on the case up version and type of server that is being used, which is a failure of security.

System.out is not recommended because it will print in the application's default output , and this output is often disregarded or even disabled. If you make a java -jar app.jar & or run the application as serviço , this output will no longer exist.

If you are doing a WEB application, or something complex, the ideal is to do a LOG with the error and alert the user with some treated message, and if possible a code, type an alert javascript itself.

For a basic% method, System.out is OK as long as the messages are handled and you do not let a main pass.

A better deal would be to use LOGS, such as log4j for example, and use functions like stackTrace or log.error("Mensagem", Exception) or log.warn() , etç, depending on what you want to log in.

    
27.06.2018 / 16:11