Crystal: Pretty printing et al.

Created on 27 Apr 2018  Â·  24Comments  Â·  Source: crystal-lang/crystal

Right now we have:

p value # pretty print value
pp value # pretty print value while also showing what are you printing

I think it's a bit confusing. In Ruby we have:

p value # inspect value
pp value # pretty print value
# (and there's no way to pretty print while also showing what are you printing)

I propose we:

  • change p to the old meaning of "inspect"
  • change pp to just pretty print a value
  • add two new methods to inspect or pretty print a value and show what you are inspecting (like the current pp, but one using inspect, and one using pretty print)

What do you think? I have no idea for the names.

My main issue with pp is that it's both "pretty print" and "show what we are pretty printing". And somtimes just p leads to a lot of lines being printed, while puts value.inspect (so Ruby's p) is sometimes more clear.

Thoughts?

discussion draft stdlib

Most helpful comment

I'd love to go back to p value means puts value.inspect, at least.

All 24 comments

I'd love to go back to p value means puts value.inspect, at least.

I'm sticked to pp because it allows to debug many variables at once. Compare

pp foo
pp bar

and

p "foo # => " + foo.inspect
p "bar # => " + bar.inspect

Please don't remove this functionality.

@vladfaust it won't go away, it would just have another name, it's the 3rd point (add two new methods to inspect or pretty print and show what you are inspecting ...)

I like pp. It's just two finger moves. pp is used for debugging and when debugging you need to write throw-away debug code fast. The only thing faster than pp is p. I totally love it, leave it as is.

Any examples of where pretty printing isn't just a better inspect?

I've never had a complaint with p or pp, so I'm struggling to understand where the :+1:'s come from.

@RX14 Try this:

a = (1..30).to_a

p a
puts a.inspect

Output:

[1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9,
 10,
 11,
 12,
 13,
 14,
 15,
 16,
 17,
 18,
 19,
 20,
 21,
 22,
 23,
 24,
 25,
 26,
 27,
 28,
 29,
 30]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

Pretty printing always comes in the way for me. I may be unlucky with what I inspect, or used to Ruby, which I prefer, because I can choose p (inspect) or pp (pretty print). I don't need the debug thing of "show what's being inspected" which comes in the way, but I can understand that someone does.

Here's an idea:

  • p does what Ruby does (inspect)
  • pp does what Ruby does (pretty print)
  • p! does what Ruby does (inspect) plus it shows what's being inspected (like what the current pp does)
  • pp! does what the current pp in Crystal does

The only controversial bit is the !, which usually in Ruby means "mutates" or "dangerous", but for a case like showing debug output I think I don't mind the nuance.

I do really like pp printing the expression and it's value. I use it a lot for debugging, often with multiple arguments which are nicely aligned. I think this is much more commonly useful than the alternative, only pretty print but don't show the expression itself. At least I wouldn't need this as a much as the current meaning of pp. And I wouldn't like if that changes.

Changing p to inspect looks fine, though.

I could probably live with pp!, although it's not much intuitive. Would be better if it stayed pp. There is value.pretty_print if you don't need the print the expression...

@asterite I like your idea. Another character than ! may be chosen, e.g. i?
We'll have:
pi print including inspected object.
ppi pretty print including inspected object.
Small (negligible?) bonus for QWERTY and lots of other keyboards: i is one key away from p.

I like it. Another alternative is pe and ppe, as i means "inspect" and inspect already has another meaning (and "e" would stand for showing the expression that's being printed).

yeah lets do the ! thing

This, please:

p value  # inspect value
pp value  # pretty print value while also showing what are you printing
puts value.pretty_inspect  # pretty print value

How often does one really need to pretty print something while caring a lot about whether it has a prefix?

@oprypin's suggestion seems the most reasonable. It's only a slight change but should cover all relevant use cases without making the naming completely arbitrary.

It's inconsistent. The one with a bang is more consistent: bang makes the expression appear.

From perspective of the most, bang makes a method “dangerous” or “explicit” - the one you’re should rescue or be cautious with. I clearly don’t want debug with ‘p!’ - it doesn’t feel comfortable. And it’s ugly.

Also, as @RX14 likes to say - does it bring us nearer to the release - no, it doesn’t. It also would be a breaking change, forcing to update some code...

👎

My main issue is that I want pp's behavior but with inspect, that is, it sometimes bothers me that the output of each variable spans multiple lines. So I'd like to have the four alternatives, not just fix one of them and make some very hard to write.

I don't think a bang is a bad idea. This is solely for debugging purposes, nothing bad will happen if we use a bang, and it's just one more char to write.

To sum up the idea of @asterite :

| | inspect | pretty print |
|---|---------|---------------------|
| only output | p | pp |
| include origin | pe | ppe |

NOTE: I've taken e letter to express the idea, but at the end whatever character can be chosen.

@asterite I particularly like the current semantics of pp and use it quite often and quickly. If I have to type pp!, it's an extra key stroke, and - depending on keyboard layout - more effort to type. It's often Shift + 1, which means I need both hands on the keyboard.

What about i and ii for the variants using inspect? i is often used as local variable as index, but as the method requires at least one argument it should be semantically unambiguous.

@vladfaust

forcing to update some code...

Which serious production ready code relies on pretty printing an object to STDOUT?

guess we can simply make p call inspect for now. I'll send a PR

Hm, actually, no. I want to have the current pp but use inspect instead of pretty_print in that case.

I'm fine with ppe or ppi, whichever you decide. But as a compiler developer I use pp a lot, and it bothers me that it spans multiple lines. It makes it hard to understand what's going on in some cases.

OK so does anyone even want the pretty printing behavior?

I do! 😁

But maybe we can just make PrettyPrint smarter to avoid more unncessary line breaks?

@asterite let's go with the table in https://github.com/crystal-lang/crystal/issues/6019#issuecomment-385397247 it's consistent with Ruby (p, pp) and improves on it with the addition of a single char (pi, ppi). Let's argument on more important topics.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgphp picture lgphp  Â·  3Comments

asterite picture asterite  Â·  3Comments

oprypin picture oprypin  Â·  3Comments

ArthurZ picture ArthurZ  Â·  3Comments

will picture will  Â·  3Comments