<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Basil Vandegriend: Professional Software Development &#187; design</title>
	<atom:link href="http://www.basilv.com/psd/blog/tag/design/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.basilv.com/psd</link>
	<description></description>
	<lastBuildDate>Wed, 16 May 2012 13:28:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Visualizing Java Package Dependencies</title>
		<link>http://www.basilv.com/psd/blog/2012/visualizing-java-package-dependencies</link>
		<comments>http://www.basilv.com/psd/blog/2012/visualizing-java-package-dependencies#comments</comments>
		<pubDate>Wed, 07 Mar 2012 13:16:42 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[architecture]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=781</guid>
		<description><![CDATA[I have recently been examining the overall package structure of a Java enterprise application. I discovered an easy way to visualize the dependencies between packages using two open source tools JDepend and Graphviz and a little glue code. JDepend analyzes compiled Java bytecode and determines dependencies and metrics between Java packages. These analysis results can [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been examining the overall package structure of a Java enterprise application. I discovered an easy way to visualize the dependencies between packages using two open source tools <a href="http://clarkware.com/software/JDepend.html">JDepend</a> and <a href="http://www.graphviz.org/">Graphviz</a> and a little glue code. JDepend analyzes compiled Java bytecode and determines dependencies and metrics between Java packages. These analysis results can be used to generate a graph with packages as nodes and dependencies as edges. Graphviz can then be used to layout and draw the graph as an image. Sample code is provided at the end of this article to show how to do this.</p>
<p>Visualizing package dependencies can be useful for a number of reasons:</p>
<ul>
<li>Reverse engineering the architecture of an application when documentation is limited, out-of-date, or non-existent.</li>
<li>Automatically generating diagrams, saving the time required to draw them manually.</li>
<li>To understand impacts when doing large-scale refactorings or changing implementations of components.</li>
<li>To determine how to extract functionality for reuse.</li>
<li>To support good architecture / design as espoused by Robert C. Martin in his book <a href="http://www.amazon.ca/gp/product/0135974445/ref=as_li_tf_tl?ie=UTF8&#038;tag=basilvandegri-20&#038;linkCode=as2&#038;camp=15121&#038;creative=330641&#038;creativeASIN=0135974445">Agile Software Development, Principles, Patterns, and Practices</a><img src="http://www.assoc-amazon.ca/e/ir?t=basilvandegri-20&#038;l=as2&#038;o=15&#038;a=0135974445" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, such as by eliminating cyclical dependencies or unwanted dependencies.</li>
</ul>
<p>Here is an example package dependency diagram generated by Graphviz:<br />
<a href="http://www.basilv.com/psd/wp-content/uploads/2012/03/packages.png"><img src="http://www.basilv.com/psd/wp-content/uploads/2012/03/packages.png" alt="Example Java Package Dependencies Diagram" title="Example Java Package Dependencies Diagram" width="507" height="251" class="alignnone size-full wp-image-782" /></a></p>
<p>The source code provided below that was used to generate this example diagram is available from my <a href="https://github.com/basilv/Java-Examples">Java Examples</a> project on GitHub or as a <a href="http://www.basilv.com/psd/software#JavaExamples">packaged bundle on my Software page</a>. The code can easily be reused: change the directory of compiled code scanned by JDepend ("dist/classes"), and change the root package used to limit the scope of the diagram ("com.basilv.examples.packagediagram").</p>
<pre class="prettyprint">
package com.basilv.examples.packagediagram;
import java.io.*;
import java.util.*;
import jdepend.framework.*;

public class PackageDiagramCreatorApp {

  public static void main(String[] args) {
    createPackageDependencyDiagram();
    System.exit(0);
  }

  public static void createPackageDependencyDiagram() {
    Collection<JavaPackage> packages = analyzePackages();
    StringBuilder builder = generateGraph(packages);
    generateImage("packages", builder.toString());
  }

  @SuppressWarnings("unchecked")
  private static Collection<JavaPackage> analyzePackages() {
    JDepend jdepend = new JDepend();
    try {
      jdepend.addDirectory("dist/classes");
    } catch (IOException e) {
      throw new RuntimeException("Error adding directory for JDepend to analyze.", e);
    }
    Collection<JavaPackage> packages = jdepend.analyze();
    return packages;
  }

  private static StringBuilder generateGraph(
    Collection<JavaPackage> packages) {
    StringBuilder builder = new StringBuilder();
    builder.append("digraph packages {").append("\n");
    builder.append("node [shape=box];").append("\n");
    builder.append("rankdir=BT;").append("\n");
    Set<String> drawnDependencies = new HashSet<String>();
    for (JavaPackage javaPackage : packages) {
      String packageNodeName = getGraphVizNodeForPackage(javaPackage);
      if (packageNodeName == null) {
        continue;
      }
      builder.append(packageNodeName).append("\n");

      @SuppressWarnings("unchecked")
      Collection<JavaPackage> dependencies = javaPackage.getEfferents();

      for (JavaPackage dependency : dependencies) {
        String dependencyNodeName = getGraphVizNodeForPackage(dependency);
        if (dependencyNodeName == null
          || packageNodeName.equals(dependencyNodeName)) {
          continue;
        }
        String dependencyKey = packageNodeName + "->"
          + dependencyNodeName;
        if (drawnDependencies.contains(dependencyKey)) {
          continue;
        }
        builder.append(packageNodeName).append(" -> ").append(
          dependencyNodeName).append(" [weight=4]").append("\n");
        drawnDependencies.add(dependencyKey);
      }
    }
    builder.append("}\n");
    return builder;
  }

  private static String getGraphVizNodeForPackage(
    JavaPackage javaPackage) {

    String rootPackage = "com.basilv.examples.packagediagram";
    String packageName = javaPackage.getName();
    if (!packageName.startsWith(rootPackage)) {
      return null;
    }

    return packageName.replace(".", "_");
  }

  private static void generateImage(String fileName,
    String graphVizDotFormattedGraph) {
    try {
      File graphFile = createFileWithContents(fileName
        + ".txt", graphVizDotFormattedGraph);

      // This requires the Graphviz software to be installed -
      // see http://graphviz.org/
      String imageFileLocation = fileName + ".png";
      Runtime.getRuntime().exec(
        "dot -v -Tpng " + graphFile.getName() + " -o "
          + imageFileLocation);

      System.out.println("Image file available at "
        + new File(imageFileLocation).getAbsolutePath());
    } catch (IOException e) {
      throw new RuntimeException("Error generating image " + fileName, e);
    }
  }

  private static File createFileWithContents(
    String fileName, String graphVizDotFormattedGraph)
    throws IOException {
    File graphFile = new File(fileName);
    FileWriter writer = new FileWriter(graphFile, false);
    try {
      writer.append(graphVizDotFormattedGraph);
    } finally {
      writer.close();
    }
    return graphFile;
  }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2012/visualizing-java-package-dependencies/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Streaming Data to Reduce Memory Usage</title>
		<link>http://www.basilv.com/psd/blog/2011/streaming-data-to-reduce-memory-usage</link>
		<comments>http://www.basilv.com/psd/blog/2011/streaming-data-to-reduce-memory-usage#comments</comments>
		<pubDate>Thu, 05 May 2011 13:01:52 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=641</guid>
		<description><![CDATA[I recently performed a series of optimizations to reduce an application's memory usage. After completing several of these I noticed that there was a common theme to many of my optimizations that I could explicitly apply to help identify further opportunities for improvement. As a reoccuring solution, this qualifies as a design pattern which I [...]]]></description>
			<content:encoded><![CDATA[<p>I recently performed a series of optimizations to reduce an application's memory usage. After completing several of these I noticed that there was a common theme to many of my optimizations that I could explicitly apply to help identify further opportunities for improvement. As a reoccuring solution, this qualifies as a design pattern which I refer to as <em>Streaming Data</em>.</p>
<h3>Context</h3>
<p>This pattern applies when you need to process a significant volume of data but the processing can be done incrementally on small subsets of the data. A typical example is loading a list of entities and then iterating through the list to process each one. While the results (output) of processing can be combined across all the entities, it is important that the input to the processing only requires a small subset of all the data, and not the entire list of entities. A code example illustrating this problem context is shown below:</p>
<pre class=" prettyprint">
List&lt;Entity&gt; entities = loadEntities();
List&lt;ProcessingResult&gt; results = new ArrayList&lt;ProcessingResult&gt;();
for (Entity entity : entities) {
  ProcessingResult result = processEntity(entity);
  results.add(entity);
}
</pre>
<h3>Solution</h3>
<p>Reducing the memory usage in the above example is based on the observation that loading the entire list of objects to process can consume a large amount of memory and is not necessary since we only use one object at a time. So the solution is to stream - incrementally retrieve - these objects instead of loading them all at once. For the consumer of this data the only change required is to first obtain a reference to the stream such as an <code>Iterable</code> that incrementally fetches data. Updating our prior code example results in the following (changed lines shown in green background):</p>
<pre class=" prettyprint">
<span style="background:#97FF77;">Iterable&lt;Entity&gt; entities = streamEntities();</span>
List&lt;ProcessingResult&gt; results = new ArrayList&lt;ProcessingResult&gt;();
for (Entity entity : entities) {
  ProcessingResult result = processEntity(entity);
  results.add(entity);
}
</pre>
<h3>Examples</h3>
<p>The mechanism to use for streaming objects will depend on the source of the data and may require significant changes compared to a bulk load. Here are some specific examples.</p>
<h4>Parsing XML</h4>
<p><a href="http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb">Parsing XML files using JAXB</a> is a convenient approach for converting the entire file into a tree of Java objects, but it populates the entire tree at once. To instead stream such data use the SAX parser provided as part of the JAXP API. The SAX parser is event-based, which means that it iterates over the entities (and attributes) of your XML and for each item invokes callbacks you define.</p>
<h4>Querying Databases using Hibernate</h4>
<p>When using Hibernate to query for a collection of entities it is convenient to simply ask Hibernate for the entire collection. A typical example of doing this using the query by criteria API within Hibernate is below:</p>
<pre class=" prettyprint">
public List&lt;Entity&gt; queryData() {
  Criteria criteria = session.createCriteria(Entity.class)
  // Add appropriate restrictions
  // ...
  List&lt;Entity&gt; result = criteria.list();
  return result;
}
</pre>
<p>When the criteria returns a large volume of data, however, this approach will consume a high volume of data. Instead use the <code>scroll</code> method on <code>Criteria</code> to return a <code>ScrollableResults</code> instance that can be used to iterate through the results. If you prefer to not expose the rest of the application to Hibernate classes, you can wrap the <code>ScrollableResults</code> in a special implementation of <code>Iterator</code> (which I leave as an exercise to the reader). The revision of the above example using streaming looks like the following (changed lines shown in green background):</p>
<pre class=" prettyprint">
<span style="background:#97FF77;">public Iterator&lt;Entity&gt; queryData() {</span>
  Criteria criteria = session.createCriteria(Entity.class)
  // Add appropriate restrictions
  // ...
<span style="background:#97FF77;">  ScrollableResults scrollableResults = criteria.scroll();</span>
<span style="background:#97FF77;">  Iterator&lt;Entity&gt; result = new ScrollableResultsIterator(scrollableResults);</span>
  return result;
}
</pre>
<p>This scroll approach only works when all the data can be processed within the same database transaction since the Hibernate session must remain open for the <code>ScrollableResults</code> to be able to continue fetching data. If this is not suitable then another option is to load the data using multiple queries that each return a subset of the data. One common example of this is when displaying search results to an user. Rather than showing all the results (which may number in the hundreds or thousands) show one page at a time and let the user step through the various pages of results. Due to the frequency with which this occurs I refer to this solution as <em>paging</em>. To implement this in Hibernate using the query by criteria API is fairly simple:</p>
<ol>
<li>Start by creating your criteria object and defining its restrictions as you normally would.</li>
<li>Apply an ordering to the criteria. It is best if this ordering is consistent, by which I mean that database updates or inserts between queries will not result in invalid or unexpected results being returned. This assumes each query for a page executes in a separate database transaction which provides no guarantees of transactional isolation for the group of queries as a whole. In some contexts, consistency is not required. If it is then I prefer to use an auto-incrementing surogate primary key as the field to sort by in order to achieve the highest level of consistency. </li>
<li>Apply restrictions to retrieve only the specific page. This is done using the methods <code>setFirstResult</code> and <code>setMaxResults</code> on the <code>Criteria</code> object.</li>
</ol>
<h3>Consequences</h3>
<p>One potential consequence of streaming data is a reduction in performance because data is loaded piece by piece rather than in bulk. To mitigate this, the solution is use what I call <em>loading sets</em>: define subsets of the total data volume that are small enough to not impact memory usage but large enough to minimize performance impacts. Then load the data one set at a time. The consuming API does not need to change: it can still iterate or stream over each loaded set, and then fetch the next set once the current one is exhausted.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2011/streaming-data-to-reduce-memory-usage/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Defect Prevention Practices</title>
		<link>http://www.basilv.com/psd/blog/2010/defect-prevention-practices</link>
		<comments>http://www.basilv.com/psd/blog/2010/defect-prevention-practices#comments</comments>
		<pubDate>Wed, 08 Sep 2010 13:44:40 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[productivity]]></category>
		<category><![CDATA[coding standards]]></category>
		<category><![CDATA[defects]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[lean]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[requirements]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=549</guid>
		<description><![CDATA[I have written numerous times about defect elimination practices such as code reviews, unit testing, and static code analysis tools. From the perspective of lean thinking, however, eliminating defects, no matter how soon after they are introduced, results in waste due to rework to fix the defects. The ideal as far as lean is concerned [...]]]></description>
			<content:encoded><![CDATA[<p>I have written numerous times about defect <em>elimination</em> practices such as <a href="http://www.basilv.com/psd/blog/2007/strategies-for-effective-code-reviews">code reviews</a>, <a href="http://www.basilv.com/psd/blog/category/unit-testing/">unit testing</a>, and <a href="http://www.basilv.com/psd/blog/2009/why-you-should-be-using-findbugs">static code analysis tools</a>. From the perspective of lean thinking, however, eliminating defects, no matter how soon after they are introduced, results in waste due to rework to fix the defects. The ideal as far as lean is concerned is to prevent defects from occurring in the first place. </p>
<p>You must be careful, however, that the cost of these defect prevention practices does not become excessive. That would introduce a different type of waste – non-value adding process. The waterfall method of software development is an example of this. One of the principles behind waterfall is that careful requirements analysis and design will minimize downstream defects during coding and testing. Put another way, it is a good idea to understand what you need to build before you start building it. The problems with waterfall arise from going to extremes in applying this principle. Requirements analysis is done up front for the entire project as a big batch based on the theory that it minimizes rework due to future change, but in reality the constant pace of requirement changes plus the learning that occurs throughout the project will result in increasing amounts of change the longer the time spent doing requirements. In contrast, Scrum and Kanban apply this principle using a balanced approach – project level requirements are done at a high level, and the more detailed analysis is done on individual user stories just prior to implementing them. (See for example the article <a href="http://agile2009.agilealliance.org/files/WHI0001%20ScrumCMMI%20from%20Good%20to%20Great%201_11.PDF">Scrum and CMMI – Going from Good to Great: are you ready-ready to be done-done?</a>.)</p>
<p>In order to effectively adopt a defect prevention practice two pieces of information are needed:</p>
<ol>
<li>Specific, actionable steps to apply the practice.</li>
<li>The expected benefit. What categories of defects does the practice intend to prevent? This helps determine when to apply the practice and helps to evaluate it after adoption to assess its effectiveness.</li>
</ol>
<p>If we consider the idea of careful requirements analysis and design mentioned above as a prevention practice, the benefits are fairly clear - prevent requirement and design based errors - but specific actionable steps are missing so it does not qualify. (In fact, this is one of the contributing factors why waterfall projects can end up in the <a href="http://en.wikipedia.org/wiki/Analysis_paralysis">analysis paralysis</a> anti-pattern.)</p>
<p>Now that the groundwork has been laid I can present some specific defect prevention practices. This is not a comprehensive list – many other practices are possible. The practices I have chosen to discuss are ones that I have used and am confident that they work. </p>
<h3>Use Understood Methods Rule</h3>
<p>The basic formulation of the rule is quite simple: when coding a method only invoke other methods whose behavior you clearly understand and are confident will work the way you want. I have written a separate article providing <a href="http://www.basilv.com/psd/blog/2009/use-understood-methods-rule">specific guidance on how to apply this rule</a>.</p>
<p>This practice generally aims to prevent interface errors - which I define generally as defects between two separate pieces of code. Research suggests that a significant proportion of defects are due to these kinds of errors (See for example the paper <a href="http://users.ece.utexas.edu/~perry/work/papers/isnd.pdf">An Empirical Study of Software Interface Faults</a>.)</p>
<p>I find this rule particularly valuable when applied to the invocation of methods between classes and especially between components. In this case it helps prevent integration errors which are usually not caught by unit testing.</p>
<h3>Design by Contract</h3>
<p>The idea of design by contract is to precisely specify the behavior of methods to help ensure that they are invoked correctly by callers and that the callers receive the results they are expecting. This is done by precisely specifying the preconditions and postconditions of methods. The chief proponent of design by contract is <a href="http://bertrandmeyer.com/">Bertrand Meyer</a>, whose book <a href="http://www.amazon.ca/gp/product/0136291554?ie=UTF8&#038;tag=basilvandegri-20&#038;linkCode=as2&#038;camp=15121&#038;creative=330641&#038;creativeASIN=0136291554">Object-Oriented Software Construction</a><img src="http://www.assoc-amazon.ca/e/ir?t=basilvandegri-20&#038;l=as2&#038;o=15&#038;a=0136291554" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> is a classic on the topic. </p>
<p>Method preconditions are conditions that must be true in order for the method to successfully execute and fulfill its postconditions. Preconditions are most commonly applied to method arguments. For example, a method to convert a string to a date might have the precondition that the string argument must not be null. Preconditions can also be applied to the state within the class or even external state. For example, a method to delete a particular object from the database might have the precondition that the object exists within the database.</p>
<p>Method postconditions are conditions that the method guarantees to be true after execution, assuming the preconditions are met. Postconditions are most commonly applied to the return value of methods, but like preconditions can also be applied to the state within the class or external state. Returning to the example of a method that converts a string to a date, such a method could have two postconditions. First, that the method will return a corresponding date object that is not null if the input is a string in a valid date format, and second that the method will throw a specified exception if the input does not correspond to a valid date format. </p>
<p>The combination of preconditions and postconditions forms in essence a contract between the method and its caller. The caller promises to fulfill the preconditions in exchange for the method guaranteeing that the postconditions will be met.</p>
<p>Despite the fact that the name of this practice contains the word "design", this approach does not require a separate up-front design of each method. The goal is to have a clear specification of behavior once the method is finished – how you arrive at it is not important to this practice. I tend to start with an initial idea for a method’s contract that I evolve as I write tests and implement the method’s logic using <a href="http://www.basilv.com/psd/blog/tag/test-driven-development">test-driven development</a>. </p>
<p>There are several options for specifying pre- and post- conditions. Some teams rely solely on their automated unit tests to serve as the specification, but I prefer a more concise specification provided as part of the method definition. In Java I typically use JavaDoc to document pre- and post- conditions and programmatically check argument preconditions at the start of the method. I typically formally specify pre- and post- conditions only on methods that are intended for use by other classes or components. In Java, this is typically public and protected methods of interfaces and classes.</p>
<p>This practice is very closely related to the Use Understood Methods Rule, and they go hand-in-hand. Knowing a method’s pre- and post- conditions is necessary to fully understanding it. As I stated above, I tend to only apply formal design by contract to methods intended for use outside the class in question, which means this practice is really aimed at preventing integration defects.</p>
<h3>Defensive Coding</h3>
<p>Defensive coding is named after the practice of <a href="http://en.wikipedia.org/wiki/Defensive_driving">defensive driving</a> and is based on the same mindset of expecting problems to occur and actively taking precautions to avoid them. Defensive coding is applied by adopting a language-specific set of idioms that minimize or prevent common coding errors when using the language. These idioms are often reflected in coding standards.</p>
<p>Here are some examples of defensive coding idioms for Java:</p>
<ul>
<li>When comparing if a variable is equal to a constant, put the constant first. This avoids a potential null-pointer exception (if the variable is null) by invoking the equals() method on the constant, which is never null.
<pre class="prettyprint">
public boolean isAdmin(String userId) {
  String constant = "admin";
  return constant.equals(userId); // Instead of userId.equals(constant)
}
</pre>
</li>
<li>Always use braces to define a block of code for an if, else, while, for, or do statement, even if the block contains only a single line of code. This avoids the problem of later adding a second line of code indented to the same level as the first and mistakenly thinking it will invoked as part of the block.
<pre class=" prettyprint">
public void addOptions(String userId) {
  if (isAdmin(userId)) {
    addAdminOptions();
  } else {
    addRegularUserOptions();
  }
}
</pre>
</li>
<li>Use the Java 5 for-each construct rather than using a loop index variable to manually iterate through a list. This avoids the problem of having an off-by-one error in constructing the loop.
<pre class="prettyprint">
public void processOptions(List<Option> options) {
  for (Option option : options) {
    option.process();
  }
}
</pre>
</li>
</ul>
<p>Defensive coding aims to minimize coding errors, both at the time of coding and in the future when the code is being modified by others. While these types of errors are typically easily detected by unit testing, I find that using these idioms (after the initial adoption) takes virtually no effort or thought on my part, making them literally a no-brainer to use.</p>
<h3>Example-Based Requirements</h3>
<p>The idea behind this practice is to express requirements as much as possible in terms of concrete examples rather than the generalized wording typically used in use cases and lists of business rules which is almost always ambiguous. I have written a separate article providing <a href="http://www.basilv.com/psd/blog/2010/example-based-requirements">further details on example-based requirements</a> which includes a specific example. :)</p>
<p>The practice of example-based requirements aims at minimizing requirement errors, particularly errors due to misunderstanding or misinterpreting. The examples should also be used as acceptance test cases, in which case they help detect design or coding errors (although unit tests should identify most of these first).</p>
<h3>Conclusion</h3>
<p>I encourage you to choose one or more of these practices to adopt in your current development work. There will be extra effort initially to understand and become comfortable with a given practice, but this will decline over time as you achieve mastery of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2010/defect-prevention-practices/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exploring Mental Processes behind Developing Software</title>
		<link>http://www.basilv.com/psd/blog/2010/exploring-mental-processes-behind-developing-software</link>
		<comments>http://www.basilv.com/psd/blog/2010/exploring-mental-processes-behind-developing-software#comments</comments>
		<pubDate>Wed, 14 Jul 2010 14:00:35 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=526</guid>
		<description><![CDATA[How do you go about designing and coding software? More specifically, what is your mental process for accomplishing this? Becoming more aware of the approach you use allows you to deliberately control and improve it. Mental thought processes are, however, very intangible and difficult to put into words. In the software development literature much has [...]]]></description>
			<content:encoded><![CDATA[<p>How do you go about designing and coding software? More specifically, what is your mental process for accomplishing this? Becoming more aware of the approach you use allows you to deliberately control and improve it. Mental thought processes are, however, very intangible and difficult to put into words. In the software development literature much has been written about how to go about design and coding ranging from a naive object-oriented design approach (find the nouns in the requirements) to <a href="http://www.basilv.com/psd/blog/2009/test-driven-development-benefits-limitations-and-techniques">test-driven development</a> (TDD). These approaches, however, deal with tangible actions to be performed rather than the thinking that must necessarily occur. </p>
<p>So I decided it would be an interesting challenge to write about my mental processes during design and coding that I hope will prove beneficial for you. I use a recent development task I worked on as a case study for examining my thinking. The task involved designing and coding a framework for generating a set of reports for an application. In my analysis I identify a number of mental 'stages' that I used, which together loosely comprise my mental process map. I first present a narrative of my thinking during this case study with references to these stages identified in bold and then provide some concluding thoughts. </p>
<h3>Case Study Narrative</h3>
<p>I start by <strong>uploading</strong> the problem domain into my working memory – reviewing all relevant requirements and any upfront or preexisting design. I am not trying to gain comprehensive knowledge at this point – there are many specific, incidental details that are not relevant to the big picture that I can safely ignore. I instead focus on the significant elements that will feed into my initial design work:</p>
<ul>
<li><em>Domain Model</em>: What concepts or data does the system need to manipulate or store?</li>
<li><em>Process Model</em>: What operations or events occur? How do they make use of the data?</li>
<li><em>Constraints</em>: What are the main constraints with respect to what I need to build? What do I need to consider or watch out for?</li>
</ul>
<p>The initial upload raises a number of questions, points requiring clarification, and ideas for improvement (of the requirements and existing design). I consult with the business analyst and business team, using face-to-face conversation if possible, to gain clarity. Simultaneously I am thinking about the key <strong>concepts</strong> that I will use in the solution to address the required functionality. I use these concepts to assemble an initial domain model and process model – mostly in my head initially, perhaps supported by some sketches or doodles on a few pieces of paper. </p>
<p>As I iterate between understanding and clarifying the requirements and developing the concepts and models, I begin to balance competing requirements through a series of <strong>trade-offs</strong>. The most frequent trade-off is between minimizing design complexity and development cost versus fully providing the requested functionality. Having face-to-face meetings with the business helps me identify soft versus hard constraints and requirements. Soft ones can potentially be discarded through negotiation while hard ones are mandatory and must be addressed. </p>
<p>At this point I feel comfortable about certain parts of the design and feel fairly confident it will work, while for other parts I am still left with an uncomfortable feeling that further thinking does not help alleviate. This pushes me into an <strong>exploration</strong> mode in which I start writing code for the pieces I am uncertain about. I do not try to write complete, production-quality code. I instead do what I call 'design-level' coding where I define interfaces or classes with important method signatures, but with no real implementations for the methods – perhaps just some pseudo-code. At this point I find it hard to do TDD on non-trivial methods as method signatures and even the classes and interfaces can change dramatically. I leave lots of <a href="http://www.basilv.com/psd/blog/2010/using-to-do-comments-in-code">to-do comments</a> in the code about specific questions or issues regarding specific functionality or design elements that are not relevant for the big picture I am working on. What I am looking for is significant gaps in my solution that may require additional clarification of requirements, or further refinement of the concepts and models I came up with earlier.</p>
<p>Throughout the entire process and especially during these initial stages there are times when I need a <strong>mental shift</strong>. This is usually when I am undecided how to resolve a particular design issue or when I feel mentally fatigued. I use a number of different strategies. One is to simply change location – get out of my cubicle and walk around. Another is to change activities to work on something unrelated and mentally less taxing in order to recharge. For thorny design issues I find that sleeping on them is a great way of letting the subconscious work on the problem and help arrive at a resolution.</p>
<p>At some point I feel that I have resolved all of the big uncertainties so I begin converting my separate chunks of design-level code into a unified set of working production-quality code. I call this <strong>consolidation</strong>. I usually begin by doing a sweep through the design-level code to resolve the outstanding to-dos. This helps identify any outstanding requirements clarifications that I need. I then switch to coding the functionality class by class and method by method using mostly strict TDD. Development feels slow at first because I need to write many utility methods or helper methods (for either the production code or for the test code), or refactor them into existence out of duplicate code that I introduce. But using TDD gives me that satisfying sense of progress as I slam out one fully-tested method after another. </p>
<p>Once the first draft of the code is written I <strong>polish</strong> it to ensure a high level of <a href="http://manifesto.softwarecraftsmanship.org/">craftsmanship</a>. This involves aspects such as renaming classes, methods and variables to ensure good readability, refactoring to eliminate duplication, and commenting when appropriate to ensure good maintainability. For more information on why and how to polish code see my article <a href="http://www.basilv.com/psd/blog/2007/why-you-should-polish-your-code">Why you should polish your code</a>.</p>
<p>After reaching code-complete on the functionality I switch to <strong>feedback</strong> mode. I have two goals. The first goal is to pass the code through as many quality checks as I can to identify and eliminate defects. This includes asking for a peer code review, reviewing the results of static code analysis tools, verifying sufficient coverage by automated tests, adding automated integration tests, and performing manual functional testing. This list does not include automated unit testing because I have already done this concurrent with the coding. The second goal is to put the code to use to identify functional gaps, usability issues, and operational issues relating to non-functional attributes such as monitoring / logging, error handling, and performance. This second goal is especially relevant for infrastructure code, where putting the code to use generally means coding business functionality that exercises the infrastructure. Although I go into the feedback stage with usually ~95% code coverage from my automated unit tests, I do expect to discover and fix a few issues. As I progress through the stage, the code gradually stabilizes. At the end, it has reached <a href="http://www.basilv.com/psd/blog/2009/my-definition-of-done">feature-done</a> status which means I consider it production-quality code ready for final testing.</p>
<h3>Discussion</h3>
<p>The process I have described may seem like it consists of discrete steps with a linear transition from start to finish, but it is anything but that.  Each 'step' is fuzzy, blurring from one to the next. There are multiple transitions going back and forth between steps. Different portions of the functionality can simultaneously be in drastically different steps – I might be polishing one class while in the midst of consolidating a second and in exploration mode for a third. I like to characterize the actual process flow as <a href=" http://en.wikipedia.org/wiki/Chaordic ">chaordic</a> - a blend of chaos and order based on balancing creativity with discipline. </p>
<p>The process may give an impression of a big-design-up-front approach, which is inaccurate for two reasons. First, I consider <a href=" http://www.basilv.com/psd/blog/2008/the-source-code-is-the-design">coding to be an act of design</a>, so I am really designing throughout the entire process. Second, I do believe in doing an appropriate amount of thinking and analysis (what some call design) prior to starting coding. The amount needed depends on the size and complexity of the problem to be solved and my current understanding of it. For simple, straightforward problems I may only spend a few minutes doing this, but those few minutes will include upload, trade-off, and exploration activities prior to diving into the coding </p>
<p>In conversations with experienced developers I have noticed some correlations between how they describe the way they develop and my process, but there are also differences. So I am interested to hear what you think of this process and how it may match or differ from yours.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2010/exploring-mental-processes-behind-developing-software/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoiding Caching To Improve Hibernate Performance</title>
		<link>http://www.basilv.com/psd/blog/2010/avoiding-caching-to-improve-hibernate-performance</link>
		<comments>http://www.basilv.com/psd/blog/2010/avoiding-caching-to-improve-hibernate-performance#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:18:54 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=486</guid>
		<description><![CDATA[I was recently doing some performance tuning and made the surprising discovery that doing less caching in Hibernate actually improved performance in a particular scenario. When I discovered the problem this seemed very counter-intuitive. In fact, my original design maximized the use of caching in order to improve performance, but the opposite happened in practice. [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing some performance tuning and made the surprising discovery that doing less caching in <a href="http://www.hibernate.org/">Hibernate</a> actually improved performance in a particular scenario. When I discovered the problem this seemed very counter-intuitive. In fact, my original design maximized the use of caching in order to improve performance, but the opposite happened in practice. In hindsight, naturally, the reason for this was fairly obvious. So I thought I would share the details of this situation so help you avoid making the same mistake.</p>
<p>I was tuning a batch processing application that received XML input data sets, each consisting of thousands of separate input records. The processing logic converted each input record into multiple Hibernate entities – as many as several hundred. This logic required a number of queries to implement - some to load related, preexisting entities, and others to verify consistency with existing data. This queried data would often be needed for multiple input records in the same data set. Based on this, I decided to use a single Hibernate session to process the entire data set, committing after each input record but keeping the session open to be able to make use of cached entities for subsequent processing.</p>
<p>When initial performance tests were carried out, they showed a disturbing trend: the processing time required per input record in the data set increased linearly. This meant that the total time required to process a data set increased exponentially with the size of the set! This is illustrated by the diagrams below.<br />
<img src="http://www.basilv.com/psd/wp-content/uploads/2010/02/RecordProcessingPerformance.png" alt="Record Processing Performance" title="Record Processing Performance" width="559" height="670" class="size-full wp-image-487" /></p>
<p>An analysis of where the time was being spent showed that the majority of the processing logic required only constant time per record. Where was the extra time going? The culprit seemed to be the call to commit the transaction to the database. I knew that even a few hundred database insert/update statements would execute quickly in nearly constant time (databases are built to scale, after all). The actual database commit was equally speedy. Normally by default I assume that network calls will be the source of performance delays. But in this case this assumption appeared to be incorrect.</p>
<p>So what exactly was happening when I committed the Hibernate transaction, before the calls to the database? Hibernate's first step is to perform a flush to write all entities with changes (called dirty entities) to the database via insert/update/delete calls. How exactly does Hibernate determine which entities are dirty? For loaded entities Hibernate uses byte-code instrumentation to add logic to track when entities become dirty. But my scenario involved new entities, for which Hibernate could not work its magic. So on each flush Hibernate scanned the fields of each entity to see if there were changes. A linearly-increasing number of entities naturally led to a linearly-increasing time per flush. To make matters worse, Hibernate's flush algorithm apparently has a <a href="https://jira.jboss.org/jira/browse/EJBTHREE-649">performance problem</a> when dealing with cascaded collections, which I was using in my scenario.</p>
<p>The solution to my performance problem was to evict all the entities from the session after committing, thus making all entities detached, and then reattaching to the session the few entities I reused in subsequent processing.</p>
<p>This article is one of a series on <a href="http://www.basilv.com/psd/blog/2008/hibernate-tips-and-tricks">Hibernate Tips &#038; Tricks</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2010/avoiding-caching-to-improve-hibernate-performance/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Exposing Mutable Objects as Public Properties</title>
		<link>http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties</link>
		<comments>http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties#comments</comments>
		<pubDate>Mon, 11 Aug 2008 14:09:25 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=129</guid>
		<description><![CDATA[I recently had an interesting design discussion with a coworker in which we discussed the pros and cons of exposing mutable objects as public properties of a class. This article provides my thoughts on the subject. An immutable class (or object) is one whose state cannot be changed once the instance is constructed. Mutable objects [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had an interesting design discussion with a coworker in which we discussed the pros and cons of exposing mutable objects as public properties of a class. This article provides my thoughts on the subject.</p>
<p>An immutable class (or object) is one whose state cannot be changed once the instance is constructed. Mutable objects do allow state changes, typically via setter methods. </p>
<p>Below is an example of such a mutable class:</p>
<pre class="prettyprint">
import java.util.Calendar;

public class Order
{
  private Calendar date = Calendar.getInstance();

  public Calendar getDate() {
    return date;
  }

  public void setDate(Calendar calendar) {
    this.date = calendar;
  }
}
</pre>
<p>The public property <code>date</code> uses Calendar as its type. Calendar is itself a mutable class – it has methods such as setters that modify the state of the class. So what is the issue?  Consider the following code:</p>
<pre class="prettyprint">
  public static void exampleOne(Order order) {
    Calendar cal = order.getDate();
    // Is order past due?
    cal.add(Calendar.DAY_OF_YEAR, -10);
    if (cal.before(Calendar.getInstance())) {
      // Order past due logic...
    }
  }
</pre>
<p>Can you spot the problem? I must admit that I failed to notice anything wrong at first glance. Here is the same example instrumented with print statements. Can you predict what will happen when you run it?</p>
<pre class="prettyprint">
import java.text.DateFormat;
import java.util.*;

public class ExampleTwo
{
  public static void exampleTwo(Order order) {
    print("Original order date = "
      + convertToText(order.getDate()));
    Calendar cal = order.getDate();

    // Is order past due?
    cal.add(Calendar.DAY_OF_YEAR, -10);
    if (cal.before(Calendar.getInstance())) {
      // Order past due logic...
    }
    print("Ending order date = "
      + convertToText(order.getDate()));
  }

  private static void print(String message) {
    System.out.println(message);
  }

  private static String convertToText(Calendar calendar) {
    return DateFormat.getDateInstance().format(
      new Date(calendar.getTimeInMillis()));
  }

  public static void main(String[] args) {
    Order order = new Order();
    exampleTwo(order);
  }
}
</pre>
<p>Below is the console output from executing this code:</p>
<pre class="box">
Original order date = 8-Aug-2008
Ending order date = 29-Jul-2008
</pre>
<p>This clearly shows the problem: the date contained in the <code>Order</code> instance is changed within the <code>exampleTwo</code> method, which is likely incorrect behavior. The trap for a developer using the <code>Order</code> class (i.e. to write <code>exampleTwo()</code>) is that they would not necessarily consider or realize that changing the <code>Calendar</code> object returned by <code>order.getDate()</code> would change the value within the instance. The <code>Order</code> class is correct (no defects) but the implementation is unsafe because a mutable object is returned from the getter. How can this be addressed?</p>
<p>One solution for this particular example is to take the order-past-due check and turn it into a method on the <code>Order</code> class that does not change the date. While I would likely employ this solution, it unfortunately does not address the underlying issue. Even if the <code>getDate()</code> method is removed, the underlying problem can occur with the <code>setDate()</code> method. The following example demonstrates this:</p>
<pre class="prettyprint">
import java.text.DateFormat;
import java.util.*;

public class ExampleThree
{
  public static void exampleThree() {
    Calendar calendar = Calendar.getInstance();
    Order firstOrder = new Order();
    Order secondOrder = new Order();
    firstOrder.setDate(calendar);

    calendar.add(Calendar.DAY_OF_YEAR, 10);
    secondOrder.setDate(calendar);

    print("First date = "
      + convertToText(firstOrder.getDate()));
    print("Second date = "
      + convertToText(secondOrder.getDate()));
  }

  private static void print(String message) {
    System.out.println(message);
  }

  private static String convertToText(Calendar calendar) {
    return DateFormat.getDateInstance().format(
      new Date(calendar.getTimeInMillis()));
  }

  public static void main(String[] args) {
    exampleThree();
  }
}
</pre>
<p>The console output is:</p>
<pre class="box">
First date = 19-Aug-2008
Second date = 19-Aug-2008
</pre>
<p>The same instance of <code>Calendar</code> is added to both <code>Order</code> instances, so when the calendar's date value is changed for assignment to the <code>secondOrder</code> instance, it also changes within the <code>firstOrder</code> instance. This is likely incorrect behavior which occurs because the setter directly stores the provided mutable object. So we end up with two references to <code>Calendar</code> in each of the <code>Order</code> instances that point to the same instance. This is called <a href="http://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a>. </p>
<p>By now you are perhaps convinced that getters and setters should never deal with mutable objects. It is unfortunately not that simple. Consider the following example:</p>
<pre class="prettyprint">
public class Order
{
  private Customer customer;

  public Customer getCustomer() {
    return customer;
  }

  public void setCustomer(Customer customer) {
    this.customer = customer;
  }
}

public class Customer
{
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

public class ExampleFour
{
  public static void exampleFour(Order order) {

    print("Original order customer name = "
      + order.getCustomer().getName());

    String newName = "New name";
    Customer customer = order.getCustomer();
    customer.setName(newName);

    print("Final order customer name = "
      + order.getCustomer().getName());
  }

  private static void print(String message) {
    System.out.println(message);
  }

  public static void main(String[] args) {
    Customer customer = new Customer();
    customer.setName("Starting name");
    Order order = new Order();
    order.setCustomer(customer);
    exampleFour(order);
  }
}
</pre>
<p>The console output is:</p>
<pre class="box">
Original order customer name = Starting name
Final order customer name = New name
</pre>
<p>In this case it seems perfectly reasonable to update the customer's name and have this change remembered within the <code>Order</code> instance. Before I explain the difference between these scenarios, I will show one more example involving a collection property:</p>
<pre class="prettyprint">
import java.util.*;

public class Order
{
  private Customer customer;

  public Customer getCustomer() {
    return customer;
  }

  public void setCustomer(Customer customer) {
    this.customer = customer;
  }
}

public class Customer
{
  private List<Order> orders = new ArrayList<Order>();

  public List<Order> getOrders() {
    return orders;
  }

  public void addOrder(Order order) {
    if (order == null) {
      return;
    }
    orders.add(order);
    order.setCustomer(this);
  }
}
</pre>
<p>In this case, calling <code>customer.getOrders().add(order)</code> might seem reasonable but is not correct as this will not invoke the extra logic in <code>customer.addOrder(order)</code> to call <code>order.setCustomer()</code>. </p>
<p>The following table summarizes the above discussion.</p>
<table class="fancy" cellspacing="0">
<tr>
<th>Property Type</th>
<th>Expect Modification of Original Object When Getter Result is Changed?</th>
<th>Is Actual Behavior Correct?</th>
</tr>
<tr>
<td>Calendar</td>
<td>No</td>
<td>No</td>
</tr>
<tr>
<td>Customer</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>List</td>
<td>Maybe</td>
<td>No</td>
</tr>
</table>
<p>Given that the code for the <code>Calendar</code> property compared to the <code>Customer</code> property is basically identical, why should we have different expectations of their behavior? The reason has to do with the nature of the two types. <code>Calendar</code> is a <a href="http://martinfowler.com/eaaCatalog/valueObject.html">Value Object</a> - a simple object whose equality is based on its value rather than its identity. Changing a calendar means ending up with a new date that is not equal to the original. <code>Customer</code> is a Reference Object – an object with business meaning whose identity is the basis for equality. Two customers can have the same values but be distinct. Changing a customer is just that – a change to that customer that should propagate throughout the system. For a fuller discussion of value objects see page 486 of the book <a href="http://www.amazon.ca/gp/product/0321127420?ie=UTF8&#038;tag=basilvandegri-20&#038;linkCode=as2&#038;camp=15121&#038;creative=330641&#038;creativeASIN=0321127420">Patterns of Enterprise Application Architecture</a><img src="http://www.assoc-amazon.ca/e/ir?t=basilvandegri-20&#038;l=as2&#038;o=15&#038;a=0321127420" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Martin Fowler. This discussion includes a short section on the risk of encountering aliasing defects if a value object is mutable and recommends that they be immutable.</p>
<p>I agree with this recommendation and I think it should be a design principle for classes: class properties that are value objects should be immutable. Many typical value object types in Java such as <code>String</code> and <code>Long</code> are already immutable, but we have already seen that others such as <code>Calendar</code> and <code>List</code> are not. What are the options in such circumstances? I will address this question in a future article.</p>
<p>The source code listed in this article is provided in the <em>Java Examples</em> project which can be downloaded from the <a href="http://www.basilv.com/psd/software">Software</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/exposing-mutable-objects-as-public-properties/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Source Code is the Design</title>
		<link>http://www.basilv.com/psd/blog/2008/the-source-code-is-the-design</link>
		<comments>http://www.basilv.com/psd/blog/2008/the-source-code-is-the-design#comments</comments>
		<pubDate>Fri, 01 Aug 2008 14:00:53 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/?p=122</guid>
		<description><![CDATA[I first came across the thought-provoking article What Is Software Design? by Jack Reeves as an appendix titled "The Source Code Is the Design" in the book Agile Software Development: Principles, Patterns, and Practices The article was written in 1992 so ignore the references to C++ (I mentally translated them to Java) and instead focus [...]]]></description>
			<content:encoded><![CDATA[<p>I first came across the thought-provoking article <a href="http://www.developerdotstar.com/mag/articles/reeves_design.htm">What Is Software Design?</a> by Jack Reeves as an appendix titled "The Source Code Is the Design" in the book <a href="http://www.amazon.ca/exec/obidos/redirect?link_code=as2&#038;path=ASIN/0135974445&#038;tag=basilvandegri-20&#038;camp=15121&#038;creative=330641">Agile Software Development: Principles, Patterns, and Practices</a><img src="http://www.assoc-amazon.ca/e/ir?t=basilvandegri-20&#038;l=as2&#038;o=15&#038;a=0135974445" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> The article was written in 1992 so ignore the references to C++ (I mentally translated them to Java) and instead focus on what Jack is saying about the nature of design in software development. I recommend starting with the summary at the end of the article.</p>
<p>In traditional software development methodologies such as the waterfall methodology, software design is an explicit phase that produces a design document as an output of that activity. The design is usually completed before coding starts. Jack takes a contrary view: the main thesis of his article is that "final source code is the real software design", and most of the article is dedicated to exploring the ramifications of this thesis. The main implication is that "programming is a design activity—a good software design process recognizes this and does not hesitate to code when coding makes sense. " And it is not just coding that is a design activity. "Coding is design, testing and debugging are part of design, and what we typically call software design is still part of design. " One may get the impression that Jack is just a cowboy coder who disdains the traditional notion of design. The following quote, however, shows that Jack values all types of design. "In software engineering, we desperately need good design at all levels. In particular, we need good top level design. The better the early design, the easier detailed design will be."</p>
<p>As Jack expands on the implications of the source code being the design, he clearly demolishes the assumptions behind a traditional waterfall approach to development by emphasizing the need for the design to evolve and be refined. "Eventually, we have to create the real software design, and it will be in some programming language. Therefore, we should not be afraid to code our designs as we derive them. We simply must be willing to refine them as necessary. ... All design activities interact. A good software design process recognizes this and allows the design to change, sometimes radically, as various design steps reveal the need."</p>
<p><a href="http://www.developerdotstar.com/mag/articles/reeves_13yearslater.html">Jack's follow-up article to his original</a> 13 years later clarified his stance on design documentation beyond that of the code. "The source code may be the master design document, but it is seldom the only one necessary." But he also pointed out the flaw in insisting that this separate design documentation be a formal deliverable: "What approach they choose doesn’t matter; until someone starts insisting that these intermediate designs should be products in their own right. It’s the code that matters. If you get good code, does it really matter how it came about? If you don’t get good code, does it really matter how much other garbage you made people do before they wrote the bad code?" </p>
<p>These points I've quoted and almost of the articles ring true for me. I especially love the last quote – what truly matters is the code. When it is deployed into production, it does not matter how polished the design document was. In some of the formal, document-heavy client projects I have been involved with, the focus placed upon the project documentation is extreme, with the client in many cases critiquing even the grammar or punctuation within these documents. Meanwhile the code is produced with limited if any code reviews and inadequate testing. Recently on a project, after all the effort I and others put into the design document to obtain the formal sign-off by the client, I had to change part of my design after only a few days of coding. (And that is after I 'cheated' and coded a prototype during the design phase.) I discovered that another part of the design I was not involved with changed at the end of the design phase and invalidated one rather key assumption I made within my own design.</p>
<p>I find myself agreeing with Jack not just on the basis of his arguments, but also on the basis of my own experience. So why then are significant portions of I.T. still focused on the traditional waterfall / document-heavy approaches to developing software? Are there counter arguments I am missing? Please let me know by posting a comment, especially if you disagree with Jack and me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/the-source-code-is-the-design/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Inspiring Great Design</title>
		<link>http://www.basilv.com/psd/blog/2008/inspiring-great-design</link>
		<comments>http://www.basilv.com/psd/blog/2008/inspiring-great-design#comments</comments>
		<pubDate>Sun, 13 Jan 2008 21:35:02 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[requirements]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2008/inspiring-great-design</guid>
		<description><![CDATA[I recently acquired a design tool – a set of IDEO method cards, where each card presents a design approach or a method of gaining inspiration. IDEO's design philosophy is to keep people at the center of the design process, and the four categories they divide the cards into reflect this: Ask people to help. [...]]]></description>
			<content:encoded><![CDATA[<p>I recently acquired a design tool – a set of <a href="http://www.ideo.com">IDEO</a> method cards, where each card presents a design approach or a method of gaining inspiration. IDEO's design philosophy is to keep people at the center of the design process, and the four categories they divide the cards into reflect this:</p>
<ul>
<li><strong>Ask</strong> people to help.</li>
<li><strong>Look</strong> at what people do.</li>
<li><strong>Learn</strong> from the facts you gather.</li>
<li><strong>Try</strong> it yourself.</li>
</ul>
<p>IDEO designs an incredibly wide variety of products – corporate websites, hand-held electronics,  clothing, business services, furniture, and more. With such diversity, it is no surprise that not all of the cards appear applicable to software development. I found that going through the cards and thinking about how they relate to I.T. to be interesting. I ended up classifying the relevant cards into three groups: </p>
<ul>
<li><strong>Requirements</strong>: These cards provide ideas on how to gather or analyze requirements.  This was the largest group by far – over one third of the total number of cards, and they spanned the four categories above. If you work in I.T., it may seem that calling tips for requirements design approaches is inappropriate. In software development there is often a divide between requirements and design. You do need to understand the client's needs and determine a solution that meets those needs, but an explicit separation in role between these activities can often hurt the final product. IDEO takes a holistic approach: determining requirements and understand the user is part of the design process and not a precursor to it.
</li>
<li><strong>User Interface Design</strong>: About one-sixth of the cards presented ideas related to user interface design. These mostly fell into the Try category, with a few in Learn and Ask. Prototyping and testing of various sorts were reoccurring themes in over half of these cards.
</li>
<li><strong>Software Design</strong>: Only a few cards seemed relevant to application architecture and software design. I initially found this surprising since I was expecting more. After further reflection, I realized that the commonly held understanding of design in the context of software development is very technical and narrowly focused. This 'technical' design activity (for lack of a better term) is necessary but not sufficient for creating a great piece of software. Other activities within the course of a development project that we in I.T. do not call design – activities such as requirements gathering, analysis, and usability testing – are all part of IDEO's holistic view of design.
</li>
</ul>
<p>In order to provide a concrete example of what the cards are like, I list in the table below a few of the cards I found particularly interesting. Each card explains not only a design method (the how), but also the reason for using it (the why). (Each card also briefly describes an IDEO project that used this method, but I do not list that below.)</p>
<table class="fancy" cellspacing="0">
<tr>
<th>Title</th>
<th>How</th>
<th>Why</th>
</tr>
<tr>
<td>Five Whys?</td>
<td>Ask "Why?" questions in response to five consecutive answers.</td>
<td>This exercise forces people to examine and express the underlying reasons for their behaviors and attitudes.</td>
</tr>
<tr>
<td>Rapid Ethnography</td>
<td>Spend as much time as you can with people relevant to the design topic. Establish their trust in order to visit and/or participate in their natural habitat and witness specific activities.</td>
<td>This is a good way to achieve a deep firsthand understanding of habits, rituals, natural language, and meanings around relevant activities and artifacts.</td>
</tr>
<tr>
<td>Error Analysis</td>
<td>List all the things that can go wrong when using a product and determine the various possible causes.</td>
<td>This is a good way to understand how design features mitigate or contribute to inevitable human errors and other failures.</td>
</tr>
</table>
<p>Looking at these design methods and the many others listed in the set of IDEO cards makes me appreciate all that goes into a well-designed product, and inspires me to think more carefully about how I think about and approach design.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2008/inspiring-great-design/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Organizing Information: Using Tags versus Categories</title>
		<link>http://www.basilv.com/psd/blog/2007/organizing-information-using-tags-versus-categories</link>
		<comments>http://www.basilv.com/psd/blog/2007/organizing-information-using-tags-versus-categories#comments</comments>
		<pubDate>Sat, 13 Oct 2007 19:52:59 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[web]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/organizing-information-using-tags-versus-categories</guid>
		<description><![CDATA[I recently upgraded WordPress - the software that runs this site - to version 2.3. New in this latest version is support for tags. Each post can be associated with any number of keywords or key phrases called tags, and navigation elements can be added to display the set of tags. Probably the most commonly [...]]]></description>
			<content:encoded><![CDATA[<p>I recently upgraded <a href="http://wordpress.org/">WordPress</a> - the software that runs this site - to version 2.3. New in this latest version is support for tags. Each post can be associated with any number of keywords or key phrases called tags, and navigation elements can be added to display the set of tags. Probably the most commonly used tag navigation element is a tag cloud which displays the list of tags in paragraph format with more frequently-used tags displayed in a proportionally larger font. See the image below.<br />
<a href='http://www.basilv.com/psd/wp-content/uploads/2007/10/tagcloud.png' title='Tag Cloud'><img src='http://www.basilv.com/psd/wp-content/uploads/2007/10/tagcloud.png' alt='Tag Cloud' /></a></p>
<p>WordPress previously only supported categories, which I used on this site. WordPress supports associating a post with any number of categories, just like tags. So how do they differ? The first major difference is that WordPress expects categories to be predefined, whereas it allows new tags to be added without restriction. The second major difference is that the navigation element for categories is most frequently a list. Since a category list takes up much more vertical space than a tag cloud for the same number of elements, there is the expectation that the total number of categories will remain small. See the image below.<br />
<a href='http://www.basilv.com/psd/wp-content/uploads/2007/10/categorylist.png' title='Category List'><img src='http://www.basilv.com/psd/wp-content/uploads/2007/10/categorylist.png' alt='Category List' /></a></p>
<p>Comparing the two images, we see that the tag cloud actually takes less vertical space despite showing four times as many items. The tag cloud does use more horizontal space, but this is actually an advantage: the format of the tag cloud allows it to use almost all of the available horizontal space, unlike the category list. The tag cloud, therefore, uses the screen real estate more effectively. </p>
<p>Which format is more usable? The main goals of both the category list and tag cloud are to (1) provide an overview of what the site is about, and (2) help users navigate to articles of interest. I believe that the tag cloud does a better job of achieving both goals. Displaying so many more tags than the category list gives a much more complete overview of the content available on the site. There is a risk that too many tags will cause the more important content to be lost in the crowd, but this is offset by the use of larger fonts for more frequently-used tags. I suspect that navigation is generally easier for users looking for a particular topic because it is much more likely there will be a tag that corresponds to their topic. If there isn't, they do have more tags to navigate through compared to a category list, which is a disadvantage. But on average there are less articles per tag then there are per category, so they can try several tags fairly quickly. For myself, I often look up old articles while writing new ones, and I find it much easier to navigate to a specific old article via a tag than via a category. Why? I often have difficulties remembering exact what 'vague' category I filed an article under, while there is usually at least one tag I know is linked to the article.</p>
<p>This brings me to what I believe is the most significant advantage of tags over categories: tags correspond more closer to how our minds store and retrieve information. Categories imply a hierarchical, structured way of organizing information, with each post usually being filed in a single category. Tags imply a more arbitrary network of relationships between articles which supports multiple ways of categorizing the content. I have often struggled in the past trying to assign a post to a single category, especially when part of the post is about another category. Assigning multiple tags is much more natural in this situation.</p>
<p>I haven't seen any consensus on the web regarding whether tags or categories are better. Among popular social bookmarking sites <a href="http://www.digg.com">Digg</a> uses categories and <a href="http://technorati.com/">Technorati</a> uses tags. An <a href="http://www.problogger.net/archives/2007/09/27/using-categories-and-tags-effectively-on-your-blog/">article on Problogger about tags and categories</a> and the resulting discussion revealed a diverse set of opinions; the article itself stated that tags compliment categories and both can co-exist on a site. </p>
<p>I myself am leaning towards eliminating the use of categories on my site and just using tags with the tag cloud. I would be interested in hearing your opinions on the matter. Do you prefer the tag cloud or the category list? Should I keep both? Should the tag cloud display less tags, or have a smaller size for the largest font? Please leave a comment and let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2007/organizing-information-using-tags-versus-categories/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Designing for Deployability</title>
		<link>http://www.basilv.com/psd/blog/2007/designing-for-deployability</link>
		<comments>http://www.basilv.com/psd/blog/2007/designing-for-deployability#comments</comments>
		<pubDate>Mon, 12 Feb 2007 04:25:13 +0000</pubDate>
		<dc:creator>Basil Vandegriend</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[EnvGen]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.basilv.com/psd/blog/2007/designing-for-deployability</guid>
		<description><![CDATA[In my previous article Architecting for Deployability, I wrote about the importance of deployability - how reliably and easily software can be deployed from development into the production environment. To accomplish this, one approach I recommended was to encapsulate differences between environments to isolate them from the majority of the application, and thus simplify deployment. [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous article <a href="http://www.basilv.com/psd/blog/2007/architecting-for-deployability">Architecting for Deployability</a>, I wrote about the importance of deployability - how reliably and easily software can be deployed from development into the production environment. To accomplish this, one approach I recommended was to encapsulate differences between environments to isolate them from the majority of the application, and thus simplify deployment. This is assuming, of course, that these differences cannot be eliminated. The technology (language and platform) you are using and the type of environmental difference you need to deal with will influence the specific techniques available to manage the difference. </p>
<p>Differences between environments that you are likely to encounter are:</p>
<ul>
<li>Database connections</li>
<li>Third-party service connections (i.e. for a web service, or naming service)</li>
<li>Logging settings</li>
<li>Performance tuning settings (i.e. database storage options)</li>
<li>Security settings (i.e. user ids, passwords)</li>
<li>Directory paths (i.e. for installed programs or libraries)</li>
</ul>
<p>Based on my experience, there are three design approaches to dealing with these environmental differences. They are discussed below in the order in which I feel they should be used: i.e. only use the second approach if the first does not work out, and only use the third when the first two approaches are not appropriate.</p>
<h3>Use support provided by the platform - but only when it makes sense</h3>
<p>The platform you are basing your development on - whether an application server, operating system, or set of language libraries - may have built-in support for dealing with certain environmental differences. Rather than building your own solution (which the other two approaches cover), it is often easiest to use the provided functionality. I have a number of examples involving a variety of platforms, including an example that shows why you should not necessarily use the built-in support if it has been badly designed.</p>
<p>The <a href="http://java.sun.com/javaee/index.jsp">Java Enterprise Edition</a> (Java EE) platform provides several options for dealing with environmental differences. One of the essential pieces is <a href="http://java.sun.com/products/jndi/tutorial/getStarted/index.html">JNDI - the Java Naming and Directory Interface</a>, which provides an environmentally-neutral way to lookup basically anything, ranging from simple strings to fully configured services. JNDI works great for looking up a <code>DataSource</code> as per the following sample code:</p>
<pre class="prettyprint">
Context rootContext = new InitialContext();

String jndiDataSourcePrefix = "java:/"; // Varies by application server
String dataSourceName = "myDataSource";
DataSource dataSource = (DataSource) rootContext.lookup(
  jndiDataSourcePrefix + dataSourceName);
</pre>
<p>This not only hides the environment-specific details concerning the actual database connection, but also hides details concerning connection pooling which often vary between environments as well. The specifics concerning the database connection and connection pooling are specified within the application server for each environment. The code can therefore be promoted between environments without change.</p>
<p>Java EE also allows for simple strings to be stored in JNDI essentially like environment variables. The retrieval of these values is much like the retrieval of a <code>DataSource</code> as the following code shows:</p>
<pre class="prettyprint">
Context rootContext = new InitialContext();
Context envContext = (Context) rootContext.lookup("java:comp/env");
String supportEmail = (String) envContext.lookup("support.email");
</pre>
<p>Unfortunately, this approach is not well suited to handling environmental differences. The values for such variables are specified in the deployment descriptor - <code>ejb-jar.xml</code> for EJBs or <code>web.xml</code> for web applications - as shown below:</p>
<pre class="prettyprint">
&lt;env-entry&gt;
  &lt;description&gt;Support Email Address&lt;/description&gt;
  &lt;env-entry-name&gt;support.email&lt;/env-entry-name&gt;
  &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
  &lt;env-entry-value&gt;support@company.com&lt;/env-entry-value&gt;
&lt;/env-entry&gt;
</pre>
<p>The problem is that the deployment descriptor also contains important configuration information that does not vary per environment. Worse, the deployment descriptor file is bundled into the EJB jar file or WAR file for deployment. So how exactly can you specify different values for environment variables, without complicating your deployment process? You cannot. </p>
<p>The deployment descriptor represents a good idea but a flawed design. The creators of the Java EE specification envisioned multiple roles, including not just a developer role, but also an application assembler role and a deployer role. The idea was that the developer could specify the default value for the environment variable, and it could be overridden by either the assembler or deployer. However, the specification does not specify an easy way to do this override, and implies that the assembler or deployer would have to directly modify the deployment descriptor or use the proprietary administration interface of the application server as is necessary for configuring datasources and connection pools. The other flaw with this design is the idea of these separate roles. In practice, the developers are also the assemblers and often the deployers as well. When there are separate individuals doing the deployment, they typically know nothing about the application and could only override settings based on instructions provided by the developers. </p>
<p>The Java EE platform provides good support for data sources, but other types of environmental differences are really not supported well. I recommend not using the environment variable mechanism provided in Java EE, as it does not address the requirement of easily deployable software.</p>
<p>My next example involves <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, a web application framework for the <a href="http://www.ruby-lang.org/en/">Ruby language</a> that has been receiving a lot of attention and hype in the last few years. One of the attractions of Rails is that it provides built-in support for many of the common features of web applications, including <a href="http://wiki.rubyonrails.com/rails/pages/Environments">handling of environmental differences</a>. Rails explicitly defines the notion of an environment via the <code>RAILS_ENV</code> environment variable, and even comes pre-configured with three: development, test, and production. Specifying environment-specific settings is extremely simple. There is a common file shared between environments (<code>config/environment.rb</code>), and one configuration file per environment (<code>config/environments/&lt;env&gt;.rb</code>). Since the configuration files are Ruby scripts, any type of setup can be done - you are not limited as in the case of Java EE deployment descriptor XML files. Rails is a clear winner over Java EE when it comes to deployability for multiple environments.</p>
<h3>Parameterize and lookup differences by environment</h3>
<p>If the platform you are using does not provide the support you need for multiple environments, then the next design approach is to implement your own support for multiple environments within the platform. While the specific mechanisms can vary, conceptually such support requires a parameter representing the environment (like the <code>RAILS_ENV</code> environment variable), and a lookup mechanism to retrieve environment-specific values based on this parameter (like the <code>config/environments/*.rb</code> files in Rails).  The lookup mechanism can be as simple as settings in a property file named after the environment, or can involve properties in a database table keyed by the environment. A more sophisticated mechanism is to use a configuration interface with a factory to create the appropriate environment-specific subclass based on the environment. This allows for any type of setup to be implemented, rather than being limited to strings or other primitive types.</p>
<p>The catch with using this approach is that the parameter representing the environment must be handled using whatever support the platform provides for dealing with environmental differences. For a Java EE application this is not a big deal - there are several different options I have used. One approach is to store the environment in a special database table. Since Java EE handles the data source fine, the code can retrieve the data source and then query the environment table to determine the environment. Another approach is to use a Java system property or even an environment variable like Rails does. A third approach I have used is to have an environment specific directory (i.e. <code>config/prod/</code>) holding one or more property files or other resources. The classpath is defined within each application server to include the appropriate environment-specific directory. The code simply loads the property files or other resources from the classpath, which resolves correctly to the desired version of the files. This works especially well for log4j configuration files.</p>
<p>For platforms that provide absolutely no support for handling environmental differences, this design approach will not work. That is when the third approach becomes useful.</p>
<h3>Generate per environment</h3>
<p>This design approach is most appropriate when the platform provides no support for handling environmental differences. The most common example of this I have encountered is database DDL statements, which can have environment-specific storage settings but do not support variables that would allow one to parameterize these settings. If you want to fully script database changes, then it is necessary to have these DDL scripts be environmentally neutral. The solution is to use file generation to produce a different version of the script for each environment from a template using the appropriate values to substitute into the template for each environment. </p>
<p>I provide a software utility called <strong>EnvGen</strong> on my <a href="http://www.basilv.com/psd/software">Software</a> page that performs environment-specific file generation. EnvGen is an <a href="http://ant.apache.org/">Ant</a> task for generating different versions of the same file parameterized for different environments (i.e. development, test, and production). File generation is done using <a href="http://freemarker.sourceforge.net/">FreeMarker</a>, a template engine with a full-featured templating language. You specify environment-specific properties in a CSV file (comma separated value spreadsheet). You can read more about EnvGen in the <a href="http://www.basilv.com/psd/software-files/EnvGen/index.html">EnvGen Release Documentation</a>.</p>
<p>For all of these approaches, no matter what language or platform you are using, the underlying concept remains the same: separate the settings and code that changes across environments from that which remains the same to achieve the goal of reliably and easily promoting the code into the production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.basilv.com/psd/blog/2007/designing-for-deployability/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

