Re: Lazy construction of parameters



On Tue, 14 Mar 2006 01:16:05 -0800, anders.johanse wrote:

Hi,

I would like to convert code such at this:

if (entry == null) {
log.warning("No entry found in storeEntries for
arcfilename: "
+ arcfileName);
}

...to this:

LogWarning.ifNull(entry, "No entry found in storeEntries for
arcfilename: " + arcFileName);

In effect consolidating common logging situations to one line of code.

Unfortunately, this means that the cost of constructing the message must
be paid always, and not just when the test fails. This has been raised
as a potential concern by some of my collaborators. In particular, some
like to construct lengthy messages in case of fails, involving DB
lookups etc.

Is there any easy way to deal with this, that retains the simple syntax
for the programmer?

Simpler than if() {} else {} which already solves your problem?

Your case seems to be rather special. Apparently, your test result does
not change the control flow of your program other than the need for
logging it.

Such cases are rarely the focal point of development in my experience.

I wouldn't invest too much time into creating an obscure logging
"framework" which side effect is testing.

You could look into the ternary operator. It appears to use short circuit
evaluation.

.



Relevant Pages

  • Re: Lazy construction of parameters
    ... log.warning("No entry found in storeEntries for ... "No entry found in storeEntries for ... supposes that the Object construction is cheaper than the ...
    (comp.lang.java.programmer)
  • Re: Lazy construction of parameters
    ... log.warning("No entry found in storeEntries for ... "No entry found in storeEntries for ... supposes that the Object construction is cheaper than the ...
    (comp.lang.java.programmer)
  • Re: Lazy construction of parameters
    ... Piotr Kobzda wrote: ... log.warning("No entry found in storeEntries for ... "No entry found in storeEntries for ...
    (comp.lang.java.programmer)
  • Re: Lazy construction of parameters
    ... log.warning("No entry found in storeEntries for ... "No entry found in storeEntries for ... public boolean setMessage(String msg) { ...
    (comp.lang.java.programmer)
  • .bash_history question
    ... timestamps and hostnames to each entry in the server .bash_history file? ... to show like, this, and same from other people logging in... ...
    (RedHat)