Skip to content
Advertisement

Should I do things like this?

I’m only started learning guava. So I don’t know best practices and etc. These is code (it binding some classes and put in Order by input collection):

public ImmutableList<ModelBean> toBean(Collection<Shape> model) {
    return ImmutableList.copyOf(Collections2.transform(Ordering.from(new Comparator<Shape>() {
        @Override
        public int compare(Shape o1, Shape o2) {
            return 0; //here placed some one line logic
        }
    }).sortedCopy(model), new Function<Shape, ModelBean>() {
        final ModelBeanCreator binder = new ModelBeanCreator();

        @Override
        public ModelBean apply(Shape input) {
            return binder.createModelBean(input);
        }
    }));
}

So, should I separate it in several operations?

upd what does it do? It takes collection. Sorts it. Maps each object to other one. Creates new ImmutableList and return it.

Advertisement

Answer

I think it’s generally okay to combine multiple operations in one call (it’s just a shame Java doesn’t have extension methods to make it prettier), but I’d advise you not to include the logic inline. With anonymous classes, it just gets messy.

Instead, declare your predicates, orderings, projections etc as constants:

private static Function<Shape, ModelBean> MODEL_BEAN_PROJECTION =
    new Function<Shape, ModelBean>() {
    final ModelBeanCreator binder = new ModelBeanCreator();

    @Override
    public ModelBean apply(Shape input) {
        return binder.createModelBean(input);
    }
};

then you can use MODEL_BEAN_PROJECTION in your method call later on. That way you can get code which is actually reasonably easy to read, despite doing a lot.

On the other hand, the option of using a local variable to describe what you’ve got so far at each stage of the transformation is also potentially useful. It’s often worth trying some code both ways, and seeing which you find more readable – and ask a colleague, too. Also experiment with different whitespace options – I find that the readability difference between code using whitespace sensibly and not can be massive.

4 People found this is helpful
Advertisement