Is it wrong Methods to show something to the user?

Asked

Viewed 73 times

3

Well I’m starting now in the area of Opoo, I saw in a class I’m watching on youtube that it was wrong Methods write something on the screen, and how I would show if something bad happened in a certain action that the user did.

Example Increasing 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);
  }
}
  • 2

    It’s not wrong, but showing off somehow might be. This case of yours is pretty shallow to suggest what is "correct", but if you look at the current televisions, they show a volume indicator when Oce increases or reduces, some simply show the empty bar when no volume or the full bar when it is loud. No textual message was necessary.

  • 2

    What I mean is that not every kind of message needs to be a text or an alert. Sometimes signs or figures work better.

  • 2

    I think a better idea is to say which methods should restrict to the minimum messages to the user, only what is necessary. To say that is wrong... is wrong. Nothing is absolute.

  • In my example I quoted, instead of showing something to the user I could do this,setVolume(10) ,10 in case as was the maximum volume ?

  • 2

    Instead of you telling the user that it has increased more than allowed, why not simply set a maximum volume value and once that value is reached, just don’t increase the volume anymore? That’s what happens in real life with Tvs, right?

  • Thinking specifically of Tvs, I think most simply do nothing when it reaches maximum volume. You could then do if (this.getVolume() < 10) { aumenta } - when the volume is >= 10, it does nothing. But it all depends on what is defined, will some product manager want to be shown some message, I don’t know

  • Got it, Thanks for the answers.

  • 3

    Without knowing the context of the statement, what we can do is speculate. I, for example, believe that the error lies in the fact of method to do more than one thing than your name indicates. I would expect a method called aumentar only increased the sound. Maybe at most return a value or cast an exception indicating that the operation failed. From then on, if visual feedback is required, it is no longer the responsibility of this method.

Show 3 more comments

1 answer

3


IS "wrong" use any type of output that demonstrates what you have on your system, such as a printStackTrace for example. Because, besides the screen getting messy, ugly and the user does not understand anything, someone more knowledgeable can see the names of classes, methods, and depending on the case up version and type of server you are using, which is a security flaw.

System.out is not recommended because it will be printed on standard output application, and this output is often disregarded or even disabled. If you make a java -jar app.jar & or run the application as serviço, that exit will no longer exist.

If you are making a WEB application, or something complex, the ideal is to log with the error and alert the user with some message handled, and if possible a code, type a javascript Alert same.

For a method main basic, System.out is OK, as long as the messages are handled and you don’t leave a stackTrace pass by.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.