Reputation
616
Top tag
Next privilege 1,000 Rep.
See votes, expandable usercard
Badges
4 12
Newest
 Yearling
Impact
~8k people reached

Feb
3
comment Is it an antipattern to use peek() to modify a stream element?
@SebastianRedl I have found it 100% reliable. I first used it to collect during processing: List<Thing> list; things.stream().peek(list::add).forEach(...); very handy. Lately. I've used it to add info for publishing: Map<Thing, Long> timestamps = ...; return things.stream().peek(t -> t.setTimestamp(timestamp.get(t))).collect(toList());. I know there are other ways of doing this example, but I am greatly over-simplifying here. Using peek() produces more compact, and more elegant code IMHO. Readability aside, this question is really about what you have raised; is it safe/reliable?
Feb
2
awarded  Yearling
Feb
2
comment Is it an antipattern to use peek() to modify a stream element?
Why do you need a comment if the method name clearly signals a mutation, eg thingMutator, or more concrete resetLastProcessed etc. Unless there's a compelling reason, the need comments such as your suggestion typically indicate poor choices of variable and/or method names. If good names are chosen, isn't that enough? Or are you saying that despite a good name, anything in peek() is like a blind spot that most programmers would "skim" (visually skip)? Also, "mainly for debugging" isn't the same thing as "only for debugging" - what usages other than the "mainly" ones were intended?
Feb
2
revised Is it an antipattern to use peek() to modify a stream element?
added 1316 characters in body
Feb
1
asked Is it an antipattern to use peek() to modify a stream element?
Aug
28
awarded  Critic
Jul
26
awarded  Scholar
Jul
26
comment Is enum order sensitivity an antipattern?
This feels right - that is, if there's an attribute about the instance use that in preference to ordinal(). Also, there's a way to implement directionForDegreesFromNorth() without looping through the values by using a Map that is populated from the constructor. Before you say "but static initialization order prevents this", there is a way (have a try at it). Also, instead of Math.floorMod() you can just (degreesFromNorth + 360) % 360
Jul
26
accepted Is enum order sensitivity an antipattern?
Jul
26
asked Is enum order sensitivity an antipattern?
Jun
3
awarded  Student
Jun
3
awarded  Commentator
Jun
3
comment Best practice for allowing mutation of a Collection type field
I never looked at it that way before - the DTO is really a builder for JSON (which it is)... I like it! I will keep that arrow in my quiver when the strict pattern nazis start asking questions.
Jun
3
comment Best practice for allowing mutation of a Collection type field
I thought so. Actually, I've already gone down the proxy road for add, but not for validation; rather to provide a consumer of a single value to hand to a stream. Previously, we were collecting to a list just so we could hand it to the DTO. btw, the DTO is mutable because it's passed through the hands of lots of enrichment objects, who each mutate the part they're responsible for. Finally, yes Jackson emits it as JSON. Could have used a builder, then created an immutable DTO, but there are just too many fields (inherited API that can't be changed)
Jun
3
asked Best practice for allowing mutation of a Collection type field
Nov
13
answered Why were punch cards used for programming
Nov
11
revised Is naming a class based on its implementation acceptable?
The class is called "TreeSet" not "SetTree". And there's no apostrophe in "its" (when used as the possessive of "it")
Nov
11
suggested approved edit on Is naming a class based on its implementation acceptable?
Nov
10
answered The source is the documentation - part 1
May
28
awarded  Nice Answer