Smelly Code

Phoebe in that TV-show called Friends could have written a song about smelly computer code.

At its best it can smell like flowers. But a bunch of code does not smell remotely like flowers. At all. Most of it stink.

I have browsed around the web looking for some not-so-nice-looking code. I didn’t have to look that hard. It fell into my lap.

The Daily Dosage

A daily dosage of it can be had over at the popular site The daily WTF.

Some good ones:

File Processing the Hard Way

The snippet below looks effective.

Looks like Visual Basic, but it’s hard to say, as it hurts a little too much to look at it for more than a millisecond. But then, if you buy some expensive hard drives, why not making them work for the money.

s

Private Sub ProcessFile()

    ' prepare to do stuff ...

    Do Until blnLastTime = True
        
        Set fileReader = fso.OpenTextFile(strFileName)
        
         If fileReader.AtEndOfStream = True Then
            blnLastTime = True
         Else
            strTextLine = fileReader.ReadLine
         End If

        ' actually do stuff
         
        fileReader.Close
        Delete_Line (strFileName)
    Loop
    fileReader.Close

End Sub

Private Sub Delete_Line(strFile)

    Set fileReader = fso.OpenTextFile(strFile)
    
    If fso.FileExists(strFile & "2") Then
        fso.DeleteFile (strFile & "2")
    End If
    
    Set fileWriter = fso.CreateTextFile(strFile & 2)
    
    If fileReader.AtEndOfStream = False Then
        fileReader.ReadLine
    End If
    
    If fileReader.AtEndOfStream = False Then
            strLine = fileReader.ReadAll
            fileWriter.Write (strLine)
    End If
    
    
    fileReader.Close
    fileWriter.Close

    fso.DeleteFile strFile, True
    fso.CopyFile strFile & "2", strFile, True
    fso.DeleteFile strFile & 2, True
    
End Sub

A Shaky Connection Pool

There is lot to say about the class below. It’s an attempt to implement a connection pool of such. Good thing that getNextIndex() is public and that it doesn’t have any side-effects like updating any internal state.

You’re getting a connection, maybe, and it’s most likely connected, but you may have to share it with someone.

public class RoundRobinConnectionContainer { 
  private static final Logger LOG = Logger.getLogger( RoundRobinConnectionContainer.class ); 
 
  private int useNext = 0; 
  private List<BehaviorEngineConnection> connections = 
               Collections.synchronizedList(new ArrayList<BehaviorEngineConnection>()); 
 
  // Find the index of the next connection to try

  public int getNextIndex() { 
    if (useNext > this.connections.size() - 1) { 
       useNext = 0; 
    } 
    return( this.useNext++ ); 
  } 
 
  public void addConnection(BehaviorEngineConnection connection) { 
    connections.add(connection); 
  } 
 
  public BehaviorEngineConnection getConnectedConnection() { 
    int size = this.connections.size(); 
    while (size-- > 0) { 
      BehaviorEngineConnection conn = connections.get(getNextIndex()); 
      if (conn.isConnected()) {
         return conn; 
      } 
      LOG.error("No connected connection available!"); 
      return null; 
    } 
  }
}



Please follow me on Twitter and let my my feed sit idle in your RSS reader.