ALT 1: return a boolean
PRO: Itβs good to skip additional parts of the code that are not needed to run.
CON: This adds multiple return points and should only be used in methods with multiple lines.
public boolean match() { if(!userField.getText().equals(testUser) && !passField.getText().equals(testPass)){ return false; }
ALT 2: returns a variable
PRO: This is good in a longer method where the return variable can be changed in several places.
CON: Usually you do this when you have methods that do more than one, and your code does not share the problems.
public boolean match() { boolean result; if(...){ if(userField.getText().equals(testUser)&&passField.getText().equals(testPass)) result = true; else if (...) result = false; else if (...) result = true; } //do something if (...) result = true; else result = false; } return result; }
ALT 3: return function output or function set
PRO: This is useful for storing code lines.
CON: For readability, you should rename the method to indicate what this method actually does.
public boolean authenticate(user) { return userField.getText().equals(testUser)&&passField.getText().equals(testPass)); }
user920041
source share