Paul was having a good morning.  It was a beautiful day outside, he managed to shave 15 minutes off his commute, and even the local coffee shop had his favorite donut in stock.  All of that changed when he got his first support call of the day.  It was from a client running "the beast" product.  "The beast", as Paul and his coworkers nicknamed it, was a legacy version of their application developed somewhere overseas years before Paul was hired, by hundreds of poorly trained, and probably poorly paid, developers.  The company's sales team actively encouraged clients to upgrade from the legacy application, but a select few had resisted.

"The beast" had a reputation for containing some of the worst code Paul had ever seen and every time he had to support it, he felt like he needed a shower afterwards.  It was during his fourth hour of debugging that Paul came across a previously unvisited function shown below. 

public boolean isWidgetReferenced(int widgetId) {
	boolean available = false;

	try {
		String query;
		ResultSet rs = null;
		int count = 0;

		Connection connection = getConnection();
		query = "SELECT COUNT(*) FROM WIDGET_REGION WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}
		if (0 < count) return false;

		query = "SELECT COUNT(*) FROM WIDGET_OFFERING WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}
		if (0 < count) return false;

		query = "SELECT COUNT(*) FROM WIDGET_ORDERS WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_NEW WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_HISTORY WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_STATS_RECORDED WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_VIEWED WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_SETS WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_INSTALLED WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_PENDING WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;
		query = "SELECT COUNT(*) FROM WIDGET_JOURNAL WHERE widgetId=" + widgetId;
		rs = connection.createStatement().executeQuery(query);
		for (; rs.next();) {
			count = rs.getInt(1);
			break;
		}

		if (0 < count) return false;

		available = true;
	} catch (Exception e) {
		logger.error("isWidgetAvailable", e);
	}

	return available;
}

Paul wasn't sure what bothered him the most: that the developer had used a strangely formed for() loop on a structure that always returned exactly one record; that the integer count could have been implemented as a boolean; or that the pattern had been repeated 10 times in this file and dozens of times in other files.  As it turns out, the root cause of the issue was unrelated to the sheer amount of repetitive code.  Had the developer taken the time to clean up this function, they might have noticed that they never closed any of the JDBC connection resources defined in the method.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!