Thursday, September 27, 2012

Let Them Eat Vegetables (Not Cake)


I always believed mental models are important in communicating ideas. We talk about running out of gas to describe how tired we are. So I’ve always tried to come up with an effective model to explain code quality. How do you explain that an application that works and meets requirements should be rewritten? The end user doesn’t see the cost to change the code or the possibility of code defects. On the other hand not every piece of code needs to be rewritten if its not 100% clean. How do you strike a balance between doing the right thing and just getting things done.

So I came up with a mental model of eating cake versus vegetables. If I offered you cake (or your favorite dessert) or vegetables, with no other context, most people would choose cake. However, if you think about your weight, or if you have health problems you might choose differently. Also, if you have had a steady diet of cake, your weight and health will suffer eventually.  So there is a cost to eating cake, you are going to have to exercise more, eat less other foods and/or be very disciplined going forward.  However, if you eat vegetables most of the time, an occasional slice of cake is a nice treat. 

So if you maintain code quality most of the time, an occasional slip to just get something into production won't hurt. However if you have a steady diet of fast paced, deadline driven development your code will be the equivalent of morbidly obese and it take lots of time and money to make the slightest change.

Sunday, September 23, 2012

JSP Tag for Debugging


I was developing jsp tags and I thought a good use would be to print out variables in the different scopes to use when I am debugging so here they are.
I have another post that talks about developing jsp tag files so I won't go into that here

Here are the includes for the tags, that go at the top of the page.

 <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>  
And put this somewhere in the page
 <tags:jspDebug showApplication="true" showPageContext="true" showRequest="true" showSession="true"></tags:jspDebug>  
And here are the actual tag files
<%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%-- Attributes to show/hide different implicit variable scopes --%>
<%@ attribute name="showApplication" required="false" %>
<%@ attribute name="showSession" required="false" %>
<%@ attribute name="showRequest" required="false" %>
<%@ attribute name="showPageContext" required="false" %>

<%-- 
pageScope - a Map that maps page-scoped attribute names to their values
requestScope - a Map that maps request-scoped attribute names to their values
sessionScope - a Map that maps session-scoped attribute names to their values
applicationScope - a Map that maps application-scoped attribute names to their values
--%>

<c:if test="${not empty showApplication}">
 <h2>Application Scope</h2>
 <c:forEach items="${applicationScope}" var="entry">
  <div><span class="label">KEY:</span>${entry.key}</div>
  <div><span class="label">KEY:</span>${entry.value}</div>
 </c:forEach>
</c:if>

<c:if test="${not empty showSession}">
 <h2>Session Scope</h2>
 <c:forEach items="${sessionScope}" var="entry">
  <div><span class="label">KEY:</span>${entry.key}</div>
  <div><span class="label">KEY:</span>${entry.value}</div>
 </c:forEach>
</c:if>
<c:if test="${not empty requestScope}">
 <h2>Request Scope</h2>
 <c:forEach items="${requestScope}" var="entry">
  <div><span class="label">KEY:</span>${entry.key}</div>
  <div><span class="label">KEY:</span>${entry.value}</div>
 </c:forEach>
</c:if>
<c:if test="${not empty pageScope}">
 <h2>Page Scope</h2>
 <c:forEach items="${pageScope}" var="entry">
  <div><span class="label">KEY:</span>${entry.key}</div>
  <div><span class="label">KEY:</span>${entry.value}</div>
 </c:forEach>
</c:if>

<%-- Add any other objects to display here --%>