At my day job, I'm in software development management. From time to time my teams do code reviews, which I've always found to be enlightening. I see code reviews not only as a chance to help developers grow, but also as a chance for managers and senior engineers to better understand what trips developers up. It's important for technical leadership to understand that, since one of our responsibilities is to improve our teams.
I've observed that many developers struggle with the fundamentals. For various reasons, people put a lot more energy into learning new frameworks, APIs, technologies and so forth, and less into understanding core software engineering concepts and principles. People think it's more impressive to say that they know ActionScript and Hibernate than to say that they understand the conceptual (not merely textbook) difference between protected and package protected visibility in Java. No doubt that emphasis is reinforced by the behavior of technical recruiters and hiring managers.
Recently we had a production issue resulting from some buggy code. Instead of immediately telling you about the issue itself, let me show you a simplified version of the code so you can try to find the bug.
Try to find the bug in the following code. I'm not talking about matters of style such as not using dependency injection. There's an honest-to-goodness bug in the following code, one that will lead to production issues with users calling tech support and all that.
First, here's our servlet:
MyServlet.java
package demoapp;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static MyService myService = new MyService();
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
Long userId = (Long) req.getSession().getAttribute("userId");
List userData = myService.getUserData(userId);
req.setAttribute("userData", userData);
req.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(req, res);
}
}
Here's our service object:
MyService.java
package demoapp;
import java.util.List;
public class MyService {
private MyDao myDao = new MyDao();
public List getUserData(Long userId) {
return myDao.readUserData(userId);
}
}
And finally here's our simple data access object:
MyDao.java
package demoapp;
import java.util.List;
public class MyDao {
private List myData;
public List readUserData(Long userId) {
this.myData =
...do some JDBC, process the result set, get the data...
return this.myData;
}
}
See the bug? If not, that's OK. You'll have a second chance since I'm going to tell you about the production issue before I reveal the bug.
On Friday we released the software to lots of users who hadn't been previously using it. Over the weekend users started calling tech support reporting that wrong data was showing up on their screens. The CIO himself observed the issue.
See it now?