0

To check a string input before using it, I often use code as shown in the first block. The second block does the same, but is there a difference in performance? (for example, is the textfield read twice in the first code block?) Also is one considered as bad coding or can I use both of them?

if (!textField.getText().equals("")) {
    name = textField.getText();
}

or

String ipt = textField.getText();
if (!ipt.equals("")) {
    name = ipt;
Lucas
  • 3
  • 3
  • 2
    I wouldn't worry about it, I would do what's readable until you have a performance problem – Harrichael Jul 28 '17 at 21:54
  • unless you're calling it *billions* of times, it is probably negligible if `getText()` is a simple field accessor. I wouldn't worry about it unless you are worried about multithreading and the value changing between the two calls. – John Gardner Jul 28 '17 at 22:01
  • 1
    @JohnGardner that's true, but I actually wonder - wouldn't JIT optimize it in that case? – Mateusz Chrzaszcz Jul 28 '17 at 22:03
  • *possibly*, if the getText method is trivial? but given it is a method call, it *really* depends on what the JIT implementation wants to do. – John Gardner Jul 28 '17 at 22:41
  • @JohnGardner Trivial method calls executed billions of times get practically always inlined. The funny thing is that this time it's a [tiny bit more than a simple getter](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/awt/TextComponent.java#TextComponent.getText%28%29). – maaartinus Jul 30 '17 at 01:42

2 Answers2

1

In terms of performance, the second is faster as it spares a duplicated processing.
But it is a so cheap task that you should execute it million times to see a difference.
Besides, modern JVMs performs many optimizations. Which could result in similar performance in both cases.

But in these two ways, I notice bad smells :

This way :

if (!textField.getText().equals("")) {
    name = textField.getText();
}

introduces duplication.
It is generally unadvised as if we forget to change the duplicate everywhere we may introduce issues.
Duplicating one line of code is often not a serious issue but generally textfields are not single. So you perform very probably this task for multiple textfields. Duplication becomes so a problem.

This way :

String ipt = textField.getText();
if (!ipt.equals("")) {
    name = ipt;

introduces a local variable that has a scope broader than required. It is also discouraged to make the code less error prone as we may reuse the variable after the processing where it is required while we should not be able to.
For your requirement, I dislike both solutions.

A better quality and efficient way would be introduce a method :

private String getValueOrDefault(TextField textField, String defaultValue) {
  String value = textField.getText();
  return !value.equals("") ? value : defaultValue;
}

You can invoke it in this way :

name = getValueOrDefault(textField, name);

You don't have any longer a variable that lives beyond its requirement and you don't have duplication either.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

The second one is likely faster. That is because the first one calls getText() twice. The second one only stores it once, then can be accessed. However, since it is defined in a variable, it might be the same. This is likely the fastest way to do it:

if (!(String value=textField.getText().equals(""))
name = value;

EDIT: As said by someone in the comments, the above is NOT faster. Sorry for the answer, I didn't run it through a timer.

Rigidity
  • 169
  • 10
  • When you fixed your code so that it compiles, it'd exactly as fast as the second variant by the OP (and probably also exactly like the first variant). Just less readable. Such hacks aren't worth doing, the compiler is smart enough. Btw., replacing `.equals("")` by `.isEmpty()` is surely good for readability and probably also for speed. – maaartinus Jul 30 '17 at 01:48
  • No, it really doesn't work like this. The source code says little about the performance. Even looking at the generated bytecode is pretty pointless. All real optimizations happen at runtime and look at [this question](https://stackoverflow.com/q/21738690/581205) to see how complicated it may get. The generated machine code is what counts. And counting instructions is [not helpful either](https://stackoverflow.com/a/12770820/581205). Benchmarking is [not simple either](https://stackoverflow.com/q/504103/581205). It's a fascinating subject, but we needn't care, we have a damn good compiler. – maaartinus Jul 30 '17 at 22:18