Search code examples
javacoding-style

Java Convention return type


I want to ask which implementation is better. I think the first method is better because when I read this method, I know this method return personId, it's more readable than the second one. is it right?

Method 1:

public String persistPerson(Person person) {
    String personId = persistService.persist(person);
    return personId;
}

Method 2:

public String persistPerson(Person person) {
    return persistService.persist(person);
}

Solution

  • Every con has two sides so as your program because both the defination has one advantage over other.

    For 1st method:-

    • Advantage: : It is more readable as the other person comes to understand very quickly that what it returns.

    • Disadvantage : :- As you formulate a new string it will use some space to create new string so wastage of memory.

    For 2nd method:-

    • Advantage :- It avoids wastage of memory.
    • Disadvantage :- Its not readable