-2

I have to repeat code like this many times:

if (!StringUtils.isEmpty(line[9])) {
    tbl.setVal(line[9].charAt(0));
}

Can I clean this code up, make it inline, like a conditional operator? I've seen examples with C# but am having trouble finding a similar java example.

Or can I abstract this out into a function? A generic function that could handle multiple types?

My goal here is if the value of line[x] is empty, I do not want to call setVal

john cs
  • 2,220
  • 5
  • 32
  • 50
  • 3
    There is difference between `null` and `empty` – Ravi Jul 14 '17 at 06:59
  • @Ravi i just updated the post...if the value of the string `line` is empty, I want the object in `tbl` to not be set. – john cs Jul 14 '17 at 07:00
  • The operands of the conditional operator are expressions, not statements, and statements are not expressions in Java. Your title still says `null`. – user207421 Jul 14 '17 at 07:05
  • 1
    Sounds like an XY Problem. If you're repeating those lines many times, restructuring them individually is not going to help much. You need to approach the code from a higher level and see what you can do to avoid repetition in the first place. – shmosel Jul 14 '17 at 07:15
  • @Holger ha! I've tried to show the same approach... – Eugene Jul 14 '17 at 11:19

4 Answers4

3

You can create method like this:

public static void applyIfNotEmpty(Consumer<String> consumer, String value) {
    if (!StringUtils.isEmpty(value)) {
        consumer.accept(value);
    }
}

And then use it in such way:

applyIfNotEmpty(v -> tbl.setVal(v.charAt(0)), line[9]);
Artem Petrov
  • 772
  • 4
  • 17
2

You could have a function like

private void setValueInTbl(String input) {
  if (!StringUtils.isEmpty(input) {
    tbl.setVal(input.charAt(0));
  }
}

Then you could call it like this

setValueInTbl(line[9]);

Also, the ternary operator is not ideal because you need two values to give it after evaluation.

  • As you are probably aware, a return is not required for a void method, but it can have it. Personally, I think it improves readability even though it is not necessary. [More on return in a void method is explained by Oracle here](https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html). –  Jul 14 '17 at 07:30
  • Sure I am aware, but sorry, have to contradict. It does *not* improve readability. It is just obsolete code without any value. Why don't you write `if(true) return; else return;` instead? Same useless code... By the way: The link you provided does not recommend to place a return at the very end either. – Aconcagua Jul 14 '17 at 07:36
  • No need to be sorry, seems I made a mistake. I will remove the line from the original answer. For clarification, I let a matter of opinion enter into an answer and in so doing disregarded the rules for posting an answer (no opinions). –  Jul 14 '17 at 07:38
0

The clever way to go about this is to create a generic method like you said. However, there is a difference between a null variable and an empty variable.

To check null variables you are better off throwing an NPE.

Also, have a look at this link if you are checking elements in a list, it is an elegant solution. Null check in an enhanced for loop

Other methods that you could use to achieve this are:

Validate.notNull(variable)
Validate.notEmpty(variable) - Depending on what you really want to check for.

These validations will throw an IllegalArgumentException based on their condition.

Found this as well... worth a read! Java: How to check for null pointers efficiently

David
  • 1,192
  • 5
  • 13
  • 30
0

Why not create a method to deal with that generally, not just for String and notEmpty:

public static <T> void consumeIf(Consumer<T> consumer, T value, Predicate<T> predicate) {
    Optional.ofNullable(value).filter(predicate).ifPresent(consumer);
}
Eugene
  • 117,005
  • 15
  • 201
  • 306