When should you use a method to return a value?

I'm new to Java, so forgive me for asking what might seem like a dumb question.

I am writing a simple user login program. To verify that the username and password match, I can either use a simple boolean variable or use the boolean method and return the value as true or false.

public boolean match() { if(userField.getText().equals(testUser)&&passField.getText().equals(testPass)) return true; else return false; } 

Above is a very simple example of using a boolean method.

My question is, as a rule, what is the more profitable thing to use, a boolean method or a variable? I understand that this may be more of a personal preference for the programmer, but I don’t understand why you chose one of them.

+4
source share
8 answers
  • Use the method so that subclasses can override and define their own correspondence.
  • You should always keep private fields / members and provide access to them using methods.

Ideally, you should abstract the interface match method and implement it to override the match () method

+3
source

In this case, you should absolutely rewrite it like this:

 public boolean match() { return userField.getText().equals(testUser) && passField.getText().equals(testPass); } 
+2
source

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; } //do additional stuff here if user matches return true; } 

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)); } 
+2
source

It depends on the readability and reuse of the code you write. In your case, I would prefer to use a method. If you use chained methods and logic, as you use, use the method for readability. In any case, the JVM is likely to embed the call, so there will be no performance hit. As a result, I would change the use of the if statement and the name to better reflect the purpose of the method (and use the JavaBean naming conventions). Then your code could be easily simplified:

 public boolean isCorrectUserAndPassword() { return userField.getText().equals(testUser) && passField.getText().equals(testPass)); } 
+1
source

Depending on the business you are working on, if you need to perform a calculation, use a function, otherwise use a variable.

0
source

We usually use methods because we have done something repeatedly. In this case, you will check whether the password and username match, and you will do it again in your program. Therefore, I would use the method because accessing the boolean variable can be difficult and can become messy. Therefore, I suggest using the method in cases where you do something repeatedly (and when it is a procedure) and use variables separately from the procedure, but not as a prodcedure.

0
source

Ideally, it's actually better to reuse code if you use a boolean method,

because you can use the same matching method many times and maybe even for many purposes

0
source

A variable adds a property to a class (if it is not local), while a method adds repeating behavior. Although comparing variables and methods does not make sense, the rule of thumb is to use a variable to store a value and a method to execute a bunch of related statements.

In your case, every time you call the match method, it not only returns the boolean value, but also performs some checks before it returns either true or false . But if it were a boolean variable, it could have a boolean value of true or false , but could not execute any statements.

0
source

Source: https://habr.com/ru/post/1383653/


All Articles