Regional Variation

by in CodeSOD on

Visual Studio and the .NET languages support a feature known as "regions". Enclosing a block of code between #region SomeName and #endregion creates collapsible regions in the text editor.

It can, in principle, help you organize your code. It does, in practice, make code like this, from Mark, possible.

A screenshot of Visual Studio, where 770 line function is split up using regions

Delectable Code

by in CodeSOD on

Good method names are one of the primary ways to write self-documenting code. The challenge there, is that documentation often becomes out of date.

Take this delectable PHP nugget, from Nathaniel P, who has previously been tortured by bad date handling.


ABQ is the bomb

by in Error'd on

This week we have an unusual number of submissions involving dates or timestamps. That is, the usual sorts of error'ds, but unusually many of them.

Gerald E. chuckled "I do love the back to the future movies. But now I can see Beck from the future."


Scoring the Concatenation

by in CodeSOD on

Today's a simple one. We've all seen code that relies too heavily on string concatenation and eschews useful methods like Join.

Nick L sends us this VB.Net example, written by a slew of mechanical engineers.


A Poorly Pruned Branch

by in CodeSOD on

Juliano had this very non-WTF bit of PHP code:

if (!$path) {
	//do stuff
} else {
	//do other stuff
}

Many Unhappy Returns

by in CodeSOD on

Gavin continues work on the old C++ app he inherited. Today we get a delightful smattering of bad choices.

HRESULT CExAPI::GetPageCacheForFolder( IN  CEntryID *p_cio_Folder, OUT CPageCache **pp_PC )
{
	CEntryID *p_cio_Local = new CEntryID( this, p_cio_Folder );

	switch ( p_cio_Folder->m_uiType )
	{
		case EID_TYPE_EMPTY:
			return S_OK;
			break;
		case EID_TYPE_NORMAL :
			return GetPageCacheForNormalFolder(p_cio_Folder, pp_PC );
			break;
		case EID_TYPE_ADDRESSBOOK :
			return GetPageCacheForABFolder(p_cio_Folder, 0, pp_PC );
			break;
	}

	return S_OK;

	DeleteIfNotNULL( p_cio_Local );
}

Accessed Nulls

by in CodeSOD on

"The attached class connects to an Access database," writes Nicolai. That's always a good start for a WTF. Let's take a look.

public class ResultLoader {

	private static Logger sysLog = Logger.getLogger(ResultLoader.class);
	private static String url = "somePath";

	/**
	 * get the ResultTable from the Access database
	 * 
	 * @param tableName
	 * @return
	 */
	private static Table getResultTable(String tableName) {
		try {
			// create a new file with the path to the table
			File db = new File(url);
			// let Jackcess open the file and return a table
			return Database.open(db).getTable(tableName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * load result from DB
	 */
	public static void loadResult() {
		String tableName = "Result";
		Table resultTable = getResultTable(tableName);

		if (!resultTable.equals(null)) {
			Map<Integer, Float> yearConsumption = new HashMap<Integer, Float>();

			for (Map<String, Object> row : resultTable) {
				/*
				 *  [snip] does something with the table's rows
				 */
			}
			Result result = new Result(00, new Date(), consumptions);
		} else {
			sysLog.info("There is no data object in the Access Database!");
		}
	}
}

Left Hand Right

by in Error'd on

Tim Y. is on Fire with this burn. "Competing teams inside Google? Or just the AI recognizing marketing tactics?"


Archives