InfoQ Homepage Presentations 3 Patterns for Cleaner Code
3 Patterns for Cleaner Code
Summary
Cory Maksymchuk introduces 3 patterns for writing cleaner code: Predicates, Classifiers, and Transformer.
Bio
Cory Maksymchuk is a software developer at Protegra and has been a regular speaker at SDEC. He has spent most of the last 7 years working in the Java stack as part of large software development initiatives. His true passion in life is finding elegant solutions to difficult problems and truly gets excited seeing great ideas come to life.
About the conference
SDEC is an annual conference where industry professionals share their own real-world experiences gained through delivering solutions.
Community comments
This is basic programming pratice
by Wouter Vos,
Re: This is basic programming pratice
by Olmo del Corral,
The points have existed in Functional Programming very long time.
by cai chao,
Still a valuable and necessary talk apparently
by Johanna Belanger,
Re: Still a valuable and necessary talk apparently
by rajesh parab,
Excellent presentation
by rajesh parab,
Re: Excellent presentation
by Sanjeev Karani,
Re: Excellent presentation
by dharmi sarkar,
Re: Excellent presentation
by Cory Maksymchuk,
Google Guava
by Nick Talbot,
Re: Google Guava
by Johan B,
Unnecessary third type parameter?
by Søren Palmund,
Re: Unnecessary third type parameter?
by Cory Maksymchuk,
Re: Unnecessary third type parameter?
by Søren Palmund,
Re: Unnecessary third type parameter?
by Cory Maksymchuk,
Guava
by Johan B,
Re: Guava
by Cory Maksymchuk,
This is basic programming pratice
by Wouter Vos,
Your message is awaiting moderation. Thank you for participating in the discussion.
At my first internship (after 2 years of programming education) it was demanded that I would generalize any code possible. Is this presentation meant for 2nd year programming students? Generalization can be applied to tons of stuff, not just the 3 examples given.
Re: This is basic programming pratice
by Olmo del Corral,
Your message is awaiting moderation. Thank you for participating in the discussion.
Predicates -> .Where()
Transformer -> .Select()
Classifiers -> .GrupBy()
...
msdn.microsoft.com/en-us/library/bb394939.aspx
The points have existed in Functional Programming very long time.
by cai chao,
Your message is awaiting moderation. Thank you for participating in the discussion.
These points have existed in FP, they are the generic operations in FP, filter, map, reduce.
it seems we can always get the lights from our ancestor.
Still a valuable and necessary talk apparently
by Johanna Belanger,
Your message is awaiting moderation. Thank you for participating in the discussion.
Very few people in the audience to whom he was speaking had used these patterns before. Good job spreading good practice to people who haven't seen it before.
Re: Still a valuable and necessary talk apparently
by rajesh parab,
Your message is awaiting moderation. Thank you for participating in the discussion.
I agree with Johanna Belanger
This is exactly what developers needs,
Many new generations of developers don't know this or not taking any pain to learn that.
I often see during code review people are not following this rules.
Good and very helpful presentation.
Excellent presentation
by rajesh parab,
Your message is awaiting moderation. Thank you for participating in the discussion.
Even though I know these 3 pattern and use it while refactoring the my juniors
code. I enjoy this presentation. It helps me to know I am on right track.
Even apache collections has library for predicates which was not mentioned in presentation.
But this is how presentation should be.
many presentation has nice title but I found them boring.
or some presentation might be for certain class.
Re: Excellent presentation
by Sanjeev Karani,
Your message is awaiting moderation. Thank you for participating in the discussion.
Does someone have the code samples for this presentation.
Please share these code samples as I am interested in implementing the predicate approach for filtering objects
Re: Excellent presentation
by dharmi sarkar,
Your message is awaiting moderation. Thank you for participating in the discussion.
The slides link at the top of this page downloads the pdf.
-Dharmi
Re: Excellent presentation
by Cory Maksymchuk,
Your message is awaiting moderation. Thank you for participating in the discussion.
Feel free to email me and I'll send you the source from the presentation. My email is in the slides.
Cheers,
Cory Maksymchuk
Google Guava
by Nick Talbot,
Your message is awaiting moderation. Thank you for participating in the discussion.
I have been doing filtering, transformation and classification this way for a while using Google's Guava library. See the Predicate and Function (like Transformer) interfaces. It also has new collection types to better handle classification and also immutable collections. See the Multimap interface and the Multimaps.index() method which returns an ImmutableListMultimap for simple classification. FluentIterable was a nice addition to a recent release for writing readable code.
Unnecessary third type parameter?
by Søren Palmund,
Your message is awaiting moderation. Thank you for participating in the discussion.
In the generic implementation at 28:48 you declare three generic types... wouldn't two suffice?
T3 is only ever used to declare Collection<T3>. Could you not write the declaration as Collection<T1> which would basically be the same since T3 extends T1.</t1></t3>
Re: Unnecessary third type parameter?
by Cory Maksymchuk,
Your message is awaiting moderation. Thank you for participating in the discussion.
Great Question. Imagine if you wanted to take a collection of dogs and get a collection of their names. I.e. Turn a list of Dog (T1) into a list of String (T2). This would work great with only 2 type parameters. However, if you had subclasses of dogs like GermanShepard and Poodle, and wanted to use the same Transformer/CollectionUtil, it wouldn't compile. Having the third type parameter (T3) allows you to pass in a Poodle or German Shepard (which of course 'are' dogs) into this method and have the compiler not bark at you... because they 'are' dogs.
The code below uses Objects and Integers (which are Objects) to keep the code small. Without the T3 parameter, the code below will not compile.
Guava
by Johan B,
Your message is awaiting moderation. Thank you for participating in the discussion.
The corresponding concepts in Guava are:
Predicate
Function (Transformer)
Multimaps.index (Classifier)
The classifier interface is superfluous since a Function does the same thing. It takes an object of type A and return an object of type B.
Re: Google Guava
by Johan B,
Your message is awaiting moderation. Thank you for participating in the discussion.
FluentIterable is a nice temporary solution until Java 8 is here, thanks.
Re: Unnecessary third type parameter?
by Søren Palmund,
Your message is awaiting moderation. Thank you for participating in the discussion.
Oh! Yeah, that makes sense now. I just wasn't thinking ahead.
I found your presentation very engaging. Have you done other presentations like this?
Re: Unnecessary third type parameter?
by Cory Maksymchuk,
Your message is awaiting moderation. Thank you for participating in the discussion.
Thanks Soren. I have done other more technology specific (AOP, Android, Guice) presentations in the past but this one got quite a bit more exposure to being on InfoQ. I am a big fan of clean simple code so it was a pleasure to present.
Cheers,
Cory
Re: Guava
by Cory Maksymchuk,
Your message is awaiting moderation. Thank you for participating in the discussion.
To that point a Predicate also turns an object of type A into an object of type B... with type B always being a Boolean. All three can be considered to be transformers.