-1

I am working on a basic ATM project. I have an account class that has several methods including a getBalance method. The getBalance method returns a double value. However, I want it to print some text prior to returning the value. This is how I have it set up:

public double getBalance() {
        System.out.print("Your balance is: ");
        return balance;
    }

In my main method I just call it as:

System.out.println(account1.getBalance());

Is it bad practice to add System.out.print within my getBalance() method in this way?

It outputs everything correctly ex.: Your balance is: 1000.0

Victorb37
  • 7
  • 2
  • 2
    Bad practice, separate UI and logic. Plus, retrieving balance doesn't *always* meant you want to display them, sometimes you just want to check if a transfer is possible etc – Martheen Aug 12 '23 at 05:34
  • 2
    Its okay for initial local testing . For later use ,you can use logger. Idea is to keep what absolutely necessary for production, to keep the source bundle as light as possible ,hence any sys-outs ,verbose code & even too much logging is not appreciated. – Vidz Here Aug 12 '23 at 06:06
  • @Martheen excellent point - logging seems to be the suggested route for something like this from other replies. – Victorb37 Aug 12 '23 at 06:33
  • No, that's a great way to build a program. _Logger_ is also a good idea, there are many third-party applications that let you view and archive log events. – Reilas Aug 12 '23 at 07:30
  • 1
    'Within a non-void method' has nothing to do with it. The question here is why would you want to print the balance twice? And what does returning the balance have to do with producing output? You are already printing the balance in `main()`. That's sufficient, surely? And it doesn't output what you claim. It outputs more. Look again. – user207421 Aug 12 '23 at 08:40
  • @user207421 I didn't want to print the balance twice. I wanted to add some text prior to the balance being printed out. Hence, why I asked if it's bad practice to add the print statement. I added the non-void part to my question since this method returns a double, but I'm adding a print statement in it. I'm trying to polish up my fundamentals instead of just writing code that executes but isn't necessarily correct. – Victorb37 Aug 12 '23 at 16:59
  • I'm just telling you what your code actually does. 'Bad practice' doesn't have as much to do with it as you think. Your first problem was a simple *bug*, due to the method printing something before the print method that called it could print anything at all. – user207421 Aug 13 '23 at 04:32
  • I would get kind of confused seeing such an output when *only* calling a method called `get...()` - I would expect such output from a method called `print...()` or similar – user16320675 Aug 13 '23 at 06:47

2 Answers2

1

Is adding System.out.print() bad practice within a non-void method?

What you are doing goes against the "Separation of Concerns" design principle:

"In Computer Science, separation of concerns is a design principle for separating a computer program into distinct sections. Each section addresses a separate concern, a set of information that affects the code of a computer program."

(The above is from Wikipedia. It is difficult to come up with clear and inclusive definition of what a "concern" actually is. But with experience you will learn to recognize where different concerns are not being properly separated.)

In this case, the two concerns are "providing a way to get the balance" and "showing the balance to the user".

Here are a couple of scenarios that illustrate why that this is a bad idea:

  1. Suppose that you want to use your getBalance() method in a transfer(...) operation that transfer money from one account to another. But now you will find that an inappropriate message is sent to the user when they perform a transfer.

  2. Suppose that you want to use your getBalance() method in a statement() method that operation that generates a bank statement for the user. You may want the balance to be output as part of the bank statement, but the println is likely to be formatting it the wrong way, and / or printing it in the wrong place. (You might not even want it printed to standard output.)

Note: if the print statement was for logging purposes, then doing that in a getter is not a separation of concerns issue. But if that is its purpose, you should be using a logging framework rather than System.out.print* calls.


In summary: yes it is bad practice for a getter to also print / display information for the user.

It is also a bad idea (and incorrect per the rules of accounting) to represent money using floating point types; see Why not use Double or Float to represent currency?.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This is the exact answer I was looking for. So, per the Separation of Concerns I should have the getBalance() method that returns the balance. But, I should create separate method that prints the balance or logs it? – Victorb37 Aug 12 '23 at 17:02
  • Probably not. For logging you don't write a separate method. You just call a method provided by the logging framework. If you are going to log info about a getter call, then that is part of the "get the balance" concern. For printing the balance, >>I<< would inline the print statement in the method that you call when the user requests the balance; e.g. `balance = account.getBalance(); println("Your balance is " + balance);` Note that this is in the `ATM` class, not the `Account` class. Interacting with the user is not the concern of an account. That's the ATM's job. – Stephen C Aug 13 '23 at 03:38
0

If you want to print some text as a log then good practice will be to use a logger instead of a print statement. You can do something like this assuming you have a separate UI and separate backend:

import java.util.logging.Level;
import java.util.logging.Logger;
Logger logger = Logger.getLogger(MyClass.class.getName());

public double getBalance() {
    logger.log(Level.INFO, "Your balance is: " + balance);
    return balance;
}
Ankush Chavan
  • 1,043
  • 1
  • 6
  • 20