BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage Presentations 3 Patterns for Cleaner Code

3 Patterns for Cleaner Code

Bookmarks
46:34

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.

Recorded at:

Dec 22, 2012

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • 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.


    public class Test
    {
    public static void main(String[] args)
    {
    List<Object> objectList = new ArrayList<Object>();
    List<String> stringList = CollectionUtil.transform(objectList, OBJECT_STRING_TRANSFORMER);

    List<Integer> integerList = new ArrayList<Integer>();
    stringList = CollectionUtil.transform(integerList, OBJECT_STRING_TRANSFORMER);
    }

    public static Transformer<Object, String> OBJECT_STRING_TRANSFORMER = new Transformer<Object, String>() {
    @Override
    public String transform(Object input) {
    return input.toString();
    }
    };
    }
    public final class CollectionUtil
    {
    public static <T1, T2, T3 extends T1> List<T2> transform(Collection<T3> input, Transformer<T1, T2> transformer)
    {
    List<T2> output = new ArrayList<T2>();

    // for some reason infoq wont allow double equals here so wrote in the word instead
    if (input doubleequals null)
    {
    return null;
    }

    for (T1 t1 : input)
    {
    output.add(transformer.transform(t1));
    }
    return output;
    }
    ...
    }
    </t2></t2></t1,></t3></t2></t1,></object,></object,></integer></integer></string></object></object>

  • 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.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT