You are viewing bpfurtado

Previous Entry | Next Entry

"Human Interface" operations

By coincidence today I just had to implement my last post flatten operation of Array Ruby class. What is is funny about it is that when I read Fowler using it as an example of a typical operation of a human interface operation I thought to myself: "Who on earth would need such an operation?" If you don´t know what it does here is a example (Ruby code):

[1, [2, 3], 4, [5, [6, 7], 8], 9].flatten = [1, 2, 3, 4, 5, 6, 7, 8, 9]

And in the end I´ve ended up implementing it myself only a couple of days later of thinking that :)
I am doing some combinations of elements in my current project and my algorithm resulted this kind of collections, that may have elements that are other inner collections. All I needed was this kind of flatten operation, of course I could review my algorithm but this solution required much less effort. I guess the use of this operation is more common than I thought.

Here is the code:

public static Collection flatten(Collection elements)
{
    Collection flattened = new LinkedList();
    for (Object o : elements) {
        if (o instanceof Collection)
            flattened.addAll(flatten((Collection) o));
        else
            flattened.add(o);
    }
    return flattened;
}

A human interface operation is one that has a very specific usage but even though it is added to your interface against the philosophy of adding only operations of a general need.

By the way, Cedric has recently posted his opinions on human interfaces too.

Comments

( 3 comments — Leave a comment )
dserodio
Dec. 12th, 2005 12:14 pm (UTC)
No braces?!?
Oh no! An "if" statement without braces! I can see it coming down to haunt you after an all-nighter of shotgun surgeries... :-P
bpfurtado
Dec. 12th, 2005 12:30 pm (UTC)
Re: No braces?!?
Ohh that was a poetic license to make the code smaller in the post, guilty as charged :-)
bpfurtado
Dec. 12th, 2005 07:01 pm (UTC)
Re: No braces?!?
Or perhaps it´s the influence of my last weeks of python coding? Ohh, I´m such a lier ;)
( 3 comments — Leave a comment )

Latest Month

March 2010
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
28293031   

Tags

Page Summary

Powered by LiveJournal.com
Designed by Tiffany Chow