projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.93k stars 2.4k forks source link

[FEATURE] Add option to @ToString that does not print variables whose values are null #3038

Open rob-stoecklein opened 3 years ago

rob-stoecklein commented 3 years ago

GOAL Reduce the length of toString() outputs by ignoring variables that are null.

BENEFIT Greatly improves the readability of object outputs, especially on data beans that contain many null variables.

EXAMPLE A simple data bean with three fields, two of which are null, has a toString() output of: Bean(foo=null, hee=haw, bar=null)

Propose that the output can be: Bean(hee=haw)

On data beans many fields, this can significantly simplify the toString() output.

CHANGE Add an option "includeNullValues" be added to the ToString annotation: @ToString(includeNullValues=false)

Of course, this option would default to true.

Thanks for your consideration!

thomasbeauvais commented 2 years ago

Yes, please! This would be wonderful! It's simply to implement, simple to retrofit, and is backwards compatible!

mickroll commented 7 months ago

Groovy has a similar option, see [groovy.transform.ToString#ignoreNulls](https://docs.groovy-lang.org/latest/html/api//groovy/transform/ToString.html#ignoreNulls()). I like this somewhat shorter naming "ignoreNulls". Property names cannot be null, thus the suffix 'Values' is not needed.

Please consider this option:

Janmm14 commented 6 months ago

Upvoting this!