简体   繁体   中英

Better Practice: Printing from void method OR returning value from method and printing from method caller

Which would be considered the better practice(if any) between printing a value from a void method with parameters or returning a value to the method caller and printing it in the method caller? For instance, the first code excerpt is the former and the second code excerpt is the latter:

public static void main(String[] args){
    printValue(5);
}

public static void printValue(int number){
    if(number == 10)
        System.out.println("Message A");

    else
        System.out.println("Message B");
}

and

public static void main(String[] args){
    System.out.println(getValue(5));
}

public static String getValue(int number){
    if(number == 10)
        return "Message A";

    else
        return "Message B";
}

A couple of things I would change:
1. write according to Java code conventions (regarding using brackets )
2. have only one exit point from the method:

public static String getValue(int number){
    String result = null;
    if(number == 10) {
        result = "Message A";
    } else {
        result = "Message B";
    }
    return result;
}

And last, it doesn't matter where you print it - but I would prefer printing inside the method - this way if you have more than one caller - you don't have to re-implement printing the result in different places.

The advantage of the first approach is that you have encapsulated the output method so that even if you call printValue from many places you can change a single location if you need to add conditional behavior or print the value in a different manner.

The advantage of the second approach is that you can write tests to validate your value conversion.

You might even want to combine the approaches if you need both advantages by having printValue() call getValue() so that you can write tests against getValue() as well as encapsulate the output mechanism in printValue() .

If your requirements do not dictate a most useful approach I recommend that you prioritize clarity of the code and figure you will refactor your code when your requirements change.

The second approach is going to be better in most practical applications.

  1. It is easily testable through automation instead of manually checking the console
  2. It allows you to use the value for more than just printing.

The activity being performed is to pick the correct message string. If we rename the method to something more descriptive we might select

public static String determineMessageFromValue(int number)

alternatively if you kept the print inside of the method

public static String determineAndPrintMessageFromValue(int number)

which is wordier and probably more specific than you want it to be.

It may seem a little bit like overkill if the goal of the program is always to do something to the console but it helps you think in responsibilities when you using this code in a more complex system. For instance, having a method printing to console wouldn't be as that useful if you decided to display the value on a java swing UI & also on a web page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM