-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

5
  • 3
    There is difference between null and empty Commented Jul 14, 2017 at 6: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. Commented Jul 14, 2017 at 7:00
  • The operands of the conditional operator are expressions, not statements, and statements are not expressions in Java. Your title still says null. Commented Jul 14, 2017 at 7: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. Commented Jul 14, 2017 at 7:15
  • @Holger ha! I've tried to show the same approach... Commented Jul 14, 2017 at 11:19

4 Answers 4

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]);
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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.
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.
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).
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

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.