question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for lazy evaluation of arguments in Preconditions

See original GitHub issue

A seemingly innocent Precondition that looks like this…

// Make sure 'user' is null
Preconditions.checkState(user == null, "User is not null. User id: %s", user.getId());

…actually throws an NPE when the precondition is met (user == null) since user.getId() is eagerly evaluated. The obvious workaround is to provide an argument like…

Preconditions.checkState(user == null, "User is not null. User id: %s", new Object() {
    @Override
    public String toString() {
        return user.getId();
    }
});

…but it feels clunky at best.

It would be very slick if Preconditions would support lazy evaluation of parameters, for example by special treatment of Supplier<?> arguments, so one could write something like

Preconditions.checkState(user == null, "User is not null. User id: %s", (Supplier<?>) () -> user.getId());

The toString implementation of Supplier is pretty meaningless so I don’t this would infringe on other use-cases or cause any back compat issues.

Another alternative could be to use a dedicated interface, such as LazyArgument and have a factory method in Preconditions to allow for something like

Preconditions.checkState(user == null, "User is not null. User id: %s", Preconditions.lazyArg(() -> user.getId()));

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinb9ncommented, Feb 20, 2022

(We should promote flogger more.)

I try to never say never, but the lambda approach is probably unlikely to fly.

They might not be what you want, but I’ll leave this open while we think about failState & failArgument a sec.

0reactions
kevinb9ncommented, Oct 20, 2022

Sorry I missed that.

We think that

checkState(user == null, "User is not null. User id: %s", lazy(() -> user.getId()));

is not sufficiently better code than

if (user != null) {
  throw new IllegalStateException("User is not null. User id: " + user.getId());
}

And in fact, anyone can read the latter code and know exactly what it does, which isn’t the case at top.

Guava has never wanted to be a different way to do things. We have always strived to add only features that provide better ways than the universal idioms. Preconditions provides value just by being usable most of the time; it doesn’t need to be “all of the time”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

6.5 Lazy evaluation | Advanced R
In R, function arguments are lazily evaluated: they're only evaluated if accessed. ... Lazy evaluation is powered by a data structure called a...
Read more >
Java static code analysis - SonarSource Rules
Lazy initialization of "static" fields should be "synchronized". Code Smell ... "Preconditions" and logging arguments should not require evaluation.
Read more >
Lazy Evaluation Of Function Arguments
Lazy evaluation of function arguments dramatically extends the expressive power of functions. It enables the encapsulation into functions of many common coding ...
Read more >
Scala Language Tutorial => Arguments lazy evaluation
Scala supports lazy evaluation for function arguments using notation: def func(arg: => String) . Such function argument might take regular String object or ......
Read more >
logging lazy evaluation java, logging in java stream, java 8 log4j ...
Java 8 lambda support for lazy logging In release 2.4, ... "Preconditions" and logging arguments should not require evaluation (squid:S2629) Passing message ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found