DecomposeLine



  •  Found this beauty when fixing some old code:

    private Vector decomposeLine( String lineString )
    {
        Vector results = new Vector();
        String newString = new String();
        lineString = lineString + ",";
        char chars[] = lineString.toCharArray();

        for( int i = 0; i < chars.length; i++ )
        {
            String tmp = String.valueOf( chars[i] );
            if( tmp.equals( "," ) )
            {
                if( !newString.equals( null ) )
                {
                    results.addElement( newString );
                    newString = new String();
                }

            }
            else
            {
                newString = newString + chars[i];
            }
        }
        return results;
    }

    As far as I can tell, this does a String.split, but in a far more elaborate, inefficient and plain wrong way. And whilst String.split was only introduced in Java 1.4, this was in 2002, and the code is from 2005.

    This method will be replaced by Arrays.asList( line.split( "," ) ), and itself will start to decompose. An honourable mention for the person who finds all the WTFs; I'm not bothering.



  • So the code traveled back in time three years?



  • The given code will ignore any null substrings. That is, the output resulting from "one,,,,two" is the same as the output resulting from "one,two". I think this differs from the behaviour of String.split().



  • @henke37 said:

    So the code traveled back in time three years?

    Um... no? The code is from 2005. Java 1.4 is from 2002.

    @Fjp said:

    The given code will ignore any null substrings. That
    is, the output resulting from "one,,,,two" is the same as the output
    resulting from "one,two". I think this differs from the behaviour of
    String.split().

    That's probably an unwanted side-effect. The input file is essentially a CSV database dump from a table.

    Personally, I find the .equals( null ) a nice touch.



  •  And it forgets the last field.



  • @Fjp said:

    The given code will ignore any null substrings. That is, the output resulting from "one,,,,two" is the same as the output resulting from "one,two". I think this differs from the behaviour of String.split().
    And these null substrings would come from where exactly?

    If you're thinking about this bit:

    if( !newString.equals( null ) )

    ...You're forgetting that newString may be an empty string, but never a null.



  • @TGV said:

     And it forgets the last field.

    I think preventing that very thing is what the appended comma near the beginning is for.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.