Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1661 > unrolled thread

Terse code sought

Started byRoedy Green <see_website@mindprod.com.invalid>
First post2012-03-24 12:32 -0700
Last post2012-03-26 20:32 -0300
Articles 11 — 5 participants

Back to article view | Back to comp.lang.java.help


Contents

  Terse code sought Roedy Green <see_website@mindprod.com.invalid> - 2012-03-24 12:32 -0700
    Re: Terse code sought Lew <noone@lewscanon.com> - 2012-03-24 14:50 -0700
    Re: Terse code sought Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-24 19:32 -0300
      Re: Terse code sought Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-24 21:14 -0700
        Re: Terse code sought markspace <-@.> - 2012-03-25 18:23 -0700
          Re: Terse code sought Roedy Green <see_website@mindprod.com.invalid> - 2012-03-25 20:28 -0700
            Re: Terse code sought markspace <-@.> - 2012-03-26 07:59 -0700
          Re: Terse code sought Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-26 07:04 -0300
            Re: Terse code sought markspace <-@.> - 2012-03-26 08:02 -0700
              Re: Terse code sought Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-26 09:57 -0700
              Re: Terse code sought Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-26 20:32 -0300

#1661 — Terse code sought

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-03-24 12:32 -0700
SubjectTerse code sought
Message-ID<8f7sm7hd60f72vlc47ul8793gvgv9j1a89@4ax.com>
Consider the following code, typical of the sort of thing I pepper my
code with when debugging.

out.println( "maxBraceNesting:" + maxBraceNesting + "
maxBracketNesting:" + maxBracketNesting + " maxParenthesisNesting:" +
maxParenthesisNesting );

If you had a lot of these to do, how would you crank them out?
With an amanuensis that generated source code?
With a keystroke macro of some sort?
With reflection?
with an IDE plugin?
Assume the variable are local.


Way back in the PL/1 days we had PUT DATA to handle this problem.

Perhaps someday Java might have some syntactic sugar to capture the
text of an expression passed to a method and place in a second
parameter, done at compile time.


-- 
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

[toc] | [next] | [standalone]


#1662

FromLew <noone@lewscanon.com>
Date2012-03-24 14:50 -0700
Message-ID<jklfid$pgf$1@news.albasani.net>
In reply to#1661
Roedy Green wrote:
> Consider the following code, typical of the sort of thing I pepper my
> code with when debugging.
>
> out.println( "maxBraceNesting:" + maxBraceNesting + "
> maxBracketNesting:" + maxBracketNesting + " maxParenthesisNesting:" +
> maxParenthesisNesting );
>
> If you had a lot of these to do, how would you crank them out?
> With an amanuensis that generated source code?
> With a keystroke macro of some sort?
> With reflection?
> with an IDE plugin?
> Assume the variable are local.
>
>
> Way back in the PL/1 days we had PUT DATA to handle this problem.
>
> Perhaps someday Java might have some syntactic sugar to capture the
> text of an expression passed to a method and place in a second
> parameter, done at compile time.

Modulo that I wouldn't use 'println()' for that but a logger, what about
<http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String, 
java.lang.Object...)>
?

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#1663

FromArved Sandstrom <asandstrom3minus1@eastlink.ca>
Date2012-03-24 19:32 -0300
Message-ID<EXrbr.7472$Ce4.1357@newsfe21.iad>
In reply to#1661
On 12-03-24 04:32 PM, Roedy Green wrote:
> Consider the following code, typical of the sort of thing I pepper my
> code with when debugging.
> 
> out.println( "maxBraceNesting:" + maxBraceNesting + "
> maxBracketNesting:" + maxBracketNesting + " maxParenthesisNesting:" +
> maxParenthesisNesting );
> 
> If you had a lot of these to do, how would you crank them out?
> With an amanuensis that generated source code?
> With a keystroke macro of some sort?
> With reflection?
> with an IDE plugin?
> Assume the variable are local.
> 
> 
> Way back in the PL/1 days we had PUT DATA to handle this problem.
> 
> Perhaps someday Java might have some syntactic sugar to capture the
> text of an expression passed to a method and place in a second
> parameter, done at compile time.
> 

Perhaps an IDE code template for most situations. For example, in
Eclipse I can write a Java template like

System.out.println("${var} = " + ${var});

If it's called "putdata", say, using Ctrl-Space completion, I get the
above line typed into the source, and a dropdown pops up allowing me to
select from any of the local variables (albeit non-primitives) which are
available at that point. As soon as I pick one it completes both
placeholder locations.

I doubt I'd want to get any more elaborate than this, although you can
tweak the macro and maybe use different template variables (arrays,
collections) with other handy "putdata" variants, e.g.

System.out.println("${collection} = " + ${collection});

which I just assign another template abbreviation to for Ctrl-Space
operation.

In combination with this, and the fact that in Eclipse you have assisted
toString() generation, thereby taking care of the instance variable
printout heavy-lifting, you've gone a long way towards relieving some of
the drudgery.

I wouldn't bother with System.out.printf or MessageFormat.format, not
for automated console printout statements of the type we're talking
about. I'd save that level of effort for permanent logging.

AHS
-- 
Last week I helped my friend stay put. It's a lot easier'n helpin' 'em
move. I just went over to his house and made sure that he did not start
to load shit into a truck.
-- Mitch Hedberg

[toc] | [prev] | [next] | [standalone]


#1664

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-03-24 21:14 -0700
Message-ID<KYwbr.11514$532.1969@newsfe14.iad>
In reply to#1663
On 3/24/12 3:32 PM, Arved Sandstrom wrote:
> On 12-03-24 04:32 PM, Roedy Green wrote:
>> Consider the following code, typical of the sort of thing I pepper my
>> code with when debugging.
>>
>> out.println( "maxBraceNesting:" + maxBraceNesting + "
>> maxBracketNesting:" + maxBracketNesting + " maxParenthesisNesting:" +
>> maxParenthesisNesting );
>>
>> If you had a lot of these to do, how would you crank them out?
>> With an amanuensis that generated source code?
>> With a keystroke macro of some sort?
>> With reflection?
>> with an IDE plugin?
>> Assume the variable are local.
>>
>>
>> Way back in the PL/1 days we had PUT DATA to handle this problem.
>>
>> Perhaps someday Java might have some syntactic sugar to capture the
>> text of an expression passed to a method and place in a second
>> parameter, done at compile time.
>>
>
> Perhaps an IDE code template for most situations. For example, in
> Eclipse I can write a Java template like
>
> System.out.println("${var} = " + ${var});
I'll point out that IntelliJ has a template for this by default. "soutv" 
+ Ctrl-J gives you 'System.out.println("=", |)', where | is where the 
caret is placed. It auto-suggests in-scope variables, and will fill in 
the left-side of the "=" as you type the expression.  I wouldn't be 
surprised if other programming editors have similar functionality.

> In combination with this, and the fact that in Eclipse you have assisted
> toString() generation, thereby taking care of the instance variable
> printout heavy-lifting, you've gone a long way towards relieving some of
> the drudgery.
>

IntelliJ also has a "toString" generator, which generates a labels 
toString delegating to other fields (which you can select or deselect). 
Great for if you just need extra information for debugging logs. Not 
great for much else, but a good start at least.

[toc] | [prev] | [next] | [standalone]


#1667

Frommarkspace <-@.>
Date2012-03-25 18:23 -0700
Message-ID<jkoge2$3ch$1@dont-email.me>
In reply to#1664
>> On 12-03-24 04:32 PM, Roedy Green wrote:
>>> Consider the following code, typical of the sort of thing I pepper my
>>> code with when debugging.
>>>
>>> out.println( "maxBraceNesting:" + maxBraceNesting + "
>>> maxBracketNesting:" + maxBracketNesting + " maxParenthesisNesting:" +
>>> maxParenthesisNesting );

> On 3/24/12 3:32 PM, Arved Sandstrom wrote:
>> System.out.println("${var} = " + ${var});

On 3/24/2012 9:14 PM, Daniel Pitts wrote:
> I'll point out that IntelliJ has a template for this by default. "soutv"


I just want to point out these each of these is somewhat of a rube 
solution.  A better approach is to use the logger method with parameters:

<http://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String,%20java.lang.Object[]%29>

All of the other ideas mentioned above call the toString() method of 
every object concerned, and also use a StringBuilder to construct a new 
string.  Both of these can have considerable overhead.

Whereas the log method with parameters doesn't invoke any of these if 
the log level test does not pass, thus being much "lighter weight" at 
run time.

The parameter substitution is "{n}" where n is a number encoded as a 
string >= 0.  Example:

   logger.log( Level.FINEST, "myVar = {0}, otherVar = {1}", myVar,
                otherVar );

Read the bit here (at the TOP of the page) where it talks about "msg 
argument".

<http://docs.oracle.com/javase/7/docs/api/index.html?java/util/logging/Logger.html>

Back on topic: no I don't know of a macro that auto generates those for 
you. :)


[toc] | [prev] | [next] | [standalone]


#1668

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-03-25 20:28 -0700
Message-ID<jfovm71otu310ucnrqfi4e5l9uauac17jp@4ax.com>
In reply to#1667
On Sun, 25 Mar 2012 18:23:13 -0700, markspace <-@.> wrote, quoted or
indirectly quoted someone who said :

>I just want to point out these each of these is somewhat of a rube 
>solution.  A better approach is to use the logger method with parameters:

1. these are temporary pieces of code that will soon be discarded.

2. What I am trying to do is avoid having to name the name twice.  It
is not obvious that logging does anything to help.
There are two reasons for not wanting to type the name twice:
a) avoiding error. If you type it only once, it can't be out of sync
b) save keystrokes.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

[toc] | [prev] | [next] | [standalone]


#1673

Frommarkspace <-@.>
Date2012-03-26 07:59 -0700
Message-ID<jkq092$1up$1@dont-email.me>
In reply to#1668
On 3/25/2012 8:28 PM, Roedy Green wrote:
> On Sun, 25 Mar 2012 18:23:13 -0700, markspace<-@.>  wrote, quoted or
> indirectly quoted someone who said :
>
>> I just want to point out these each of these is somewhat of a rube
>> solution.  A better approach is to use the logger method with parameters:
>
> 1. these are temporary pieces of code that will soon be discarded.


This is not best practice.  Use the logger, turn those statements off 
and back on when you need them again (you will) at a higher level. 
That's the reason the logger works the way it does.

[toc] | [prev] | [next] | [standalone]


#1671

FromArved Sandstrom <asandstrom3minus1@eastlink.ca>
Date2012-03-26 07:04 -0300
Message-ID<fbXbr.22788$pc1.18103@newsfe11.iad>
In reply to#1667
On 12-03-25 10:23 PM, markspace wrote:
>>> On 12-03-24 04:32 PM, Roedy Green wrote:
>>>> Consider the following code, typical of the sort of thing I pepper my
>>>> code with when debugging.
>>>>
>>>> out.println( "maxBraceNesting:" + maxBraceNesting + "
>>>> maxBracketNesting:" + maxBracketNesting + " maxParenthesisNesting:" +
>>>> maxParenthesisNesting );
> 
>> On 3/24/12 3:32 PM, Arved Sandstrom wrote:
>>> System.out.println("${var} = " + ${var});
> 
> On 3/24/2012 9:14 PM, Daniel Pitts wrote:
>> I'll point out that IntelliJ has a template for this by default. "soutv"
> 
> 
> I just want to point out these each of these is somewhat of a rube
> solution.  A better approach is to use the logger method with parameters:
> 
[ SNIP ]
> 
> Back on topic: no I don't know of a macro that auto generates those for
> you. :)

Point being, without thinking of a macro or some other way to automate
the generation of the logger statement - or *any* approach - you're not
answering Roedy's question.

It should not escape your notice that I could just as easily have
written an Eclipse code template called "logdata" that looks like

logger.log(Level.FINEST, "${var} = {0}", ${var});

and this also addresses Roedy's question. The exact form of the
statement isn't interesting here; it's how you avoid repeated keying of
the same string and hence possible typos.

AHS
-- 
Last week I helped my friend stay put. It's a lot easier'n helpin' 'em
move. I just went over to his house and made sure that he did not start
to load shit into a truck.
-- Mitch Hedberg

[toc] | [prev] | [next] | [standalone]


#1674

Frommarkspace <-@.>
Date2012-03-26 08:02 -0700
Message-ID<jkq0eh$4jl$1@dont-email.me>
In reply to#1671
On 3/26/2012 3:04 AM, Arved Sandstrom wrote:

> Point being, without thinking of a macro or some other way to automate
> the generation of the logger statement - or *any* approach - you're not
> answering Roedy's question.
>
> logger.log(Level.FINEST, "${var} = {0}", ${var});


I don't like this because to print out multiple variables, you'd have to 
use multiple calls to the logger, and that kind of defeats the purpose 
of "one quick check."

Do you know of a way to actually do what Roedy asked?  Multiple 
variables and their names in a single statement?

[toc] | [prev] | [next] | [standalone]


#1676

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-03-26 09:57 -0700
Message-ID<Yd1cr.16268$GV1.3117@newsfe12.iad>
In reply to#1674
On 3/26/12 8:02 AM, markspace wrote:
> On 3/26/2012 3:04 AM, Arved Sandstrom wrote:
>
>> Point being, without thinking of a macro or some other way to automate
>> the generation of the logger statement - or *any* approach - you're not
>> answering Roedy's question.
>>
>> logger.log(Level.FINEST, "${var} = {0}", ${var});
>
>
> I don't like this because to print out multiple variables, you'd have to
> use multiple calls to the logger, and that kind of defeats the purpose
> of "one quick check."
>
> Do you know of a way to actually do what Roedy asked? Multiple variables
> and their names in a single statement?
>
I believe that is still possible using templates in modern IDEs, but 
requires perhaps some more esoteric knowledge of the particulars of that 
IDEs template language. Frankly I'm not so worried about typos in the 
literal part of that debugging statement, and the IDE will auto-suggest 
for the other parts, so I don't personally worry about efficiently 
generating an accurate log statement here.

[toc] | [prev] | [next] | [standalone]


#1678

FromArved Sandstrom <asandstrom3minus1@eastlink.ca>
Date2012-03-26 20:32 -0300
Message-ID<W%6cr.43850$M%7.21026@newsfe10.iad>
In reply to#1674
On 12-03-26 12:02 PM, markspace wrote:
> On 3/26/2012 3:04 AM, Arved Sandstrom wrote:
> 
>> Point being, without thinking of a macro or some other way to automate
>> the generation of the logger statement - or *any* approach - you're not
>> answering Roedy's question.
>>
>> logger.log(Level.FINEST, "${var} = {0}", ${var});
> 
> 
> I don't like this because to print out multiple variables, you'd have to
> use multiple calls to the logger, and that kind of defeats the purpose
> of "one quick check."
> 
> Do you know of a way to actually do what Roedy asked?  Multiple
> variables and their names in a single statement?
> 
I don't know of a way to do multiple variables in one statement using
this technique, no. Mind you, I haven't ever bothered to learn Eclipse
code templates past the level of complexity I've shown - I figure if
*that* exercise is turning into serious coding in its own right then I
am defeating the purpose.

FWIW *I* wouldn't be using this technique to generate logging
statements. I could definitely see using it to generate quick println's,
which I happen to think have some usefulness in throwaway situations,
which is what Roedy characterized the context as.

And let's be real: for 3 or 4 local variables using a code template of
this sort 3 or 4 times might take you 15 seconds. For me it's more
readable if it's 3 or 4 lines even, rather than being scrunched into one
line. YMMV. In any case it's hardly a major problem any way you look at
it. It's throwaway, meant to be removed at some point.

AHS
-- 
Last week I helped my friend stay put. It's a lot easier'n helpin' 'em
move. I just went over to his house and made sure that he did not start
to load shit into a truck.
-- Mitch Hedberg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web