<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0">
    <title>CodeSlick</title>
    <link rel="self" type="application/atom+xml" href="http://www.codeslick.com/atom.xml" />
    <link rel="hub" href="http://hubbub.api.typepad.com/" />
    <link rel="alternate" type="text/html" href="http://www.codeslick.com/" />
    <id>tag:typepad.com,2003:weblog-1566912</id>
    <updated>2010-01-26T16:38:15-06:00</updated>
    <subtitle>The business and craft of software development</subtitle>
    <generator uri="http://www.typepad.com/">TypePad</generator>
    <entry>
        <title>Simple yet styleable form rendering in Django</title>
        <link rel="alternate" type="text/html" href="http://www.codeslick.com/2010/01/simple-yet-styleable-form-rendering-in-django.html" />
        <link rel="replies" type="text/html" href="http://www.codeslick.com/2010/01/simple-yet-styleable-form-rendering-in-django.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a00e54fe53ddf8833012876c82d14970c</id>
        <published>2010-01-26T16:38:15-06:00</published>
        <updated>2010-01-12T10:52:10-06:00</updated>
        <summary>The problem: simplicity vs. flexibility The Django forms documentation tells us that the &quot;as_p(), as_ul() and as_table() methods are simply shortcuts for lazy developers.&quot; For the software developer, sometimes laziness is a virtue. However, there are times when we need...</summary>
        <author>
            <name>Scott Bradley</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Django" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://www.codeslick.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><h2>The problem: simplicity vs. flexibility</h2>

<p>The Django forms documentation tells us that the "as_p(), as_ul() and as_table() methods are simply shortcuts for lazy developers." For the software developer, sometimes laziness is a virtue. However, there are times when we need greater flexibility in our form rendering to enable layout and styling. Here, we will look at an approach that improves form styling flexibility while keeping form rendering in your templates relatively clean and simple.</p>

<h2>The Goal: looped form fields with custom field wrappers</h2>

<p>The resulting rendering I will be aiming for is similar to that shown in the Django documentation which uses the concept of field wrappers. An example from the docs which wraps all the fields in a form looks like this:</p>

<pre><code>{% for field in form %}
  &lt;div class="fieldWrapper"&gt;
    {{ field.errors }}
    {{ field.label_tag }}: {{ field }}
  &lt;/div&gt;
{% endfor %}</code></pre>

<p>(source: <a href="http://docs.djangoproject.com/en/1.1/topics/forms/#topics-forms-index">Django docs</a>)</p>

<p>This looped approach to rendering a form is a bit more verbose than the "as_x" shortcuts that Django gives us, but much more succinct than explicitly rendering each field. The problem with this approach is that it does not allow us to style wrappers for different field types differently. As a thought experiment, we could consider basing the field wrapper class on the field's widget input type. Something like so:</p>

<pre><code>{% for field in form %}
  &lt;div class="{{ field.widget.input_type }}"&gt;
    {{ field.errors }}
    {{ field.label_tag }}: {{ field }}
  &lt;/div&gt;
{% endfor %}</code></pre>

<p>However, not all widgets have an input type, which makes this approach not broadly applicable. The solution, however, is not far from this approach: base the class on the type name of the widget itself.</p>

<h2>The steps</h2>
<p>To accomplish this solution, we will need a custom tag that provides the class name for the widget of a field. The following steps give us what we need:</p>

<ol>
 <li>
  <p>In your project, create a custom_tags application, if you do not already have a place defined for custom template tags:</p>

  <blockquote class="codeblock">projectdir&gt; python manage.py startapp custom_tags</blockquote>

  <p>Add this application to your INSTALLED_APPLICATIONS in settings.py</p>
 </li>
 <li>
  <p>In the custom_tags application directory, create a <strong>templatetags</strong> directory. Inside templatetags, touch __init__.py and create a file called fieldtypes.py with the following content:</p>

<pre><code>from django import template
register = template.Library()

@register.simple_tag
def field_type(field):
    return field.field.widget.__class__.__name__</code></pre>

<p>Note: If you don't like camel-cased class names in your HTML, you could do some fancy manipulation of the class name before returning it. You could also do some exception handling here, in case you accidentally pass a non-field object during development. My preference is just to let Django throw the default exception that happens when the field.field or field.field.widget does not exist.</p>

<p>For more details about creating custom template tags, go to the <a href="http://docs.djangoproject.com/en/1.1/howto/custom-template-tags/#howto-custom-template-tags">Django docs</a></p>

 </li>
 <li>
  <p>Now in your template, you can do something like this:</p>

<pre><code>{% load fieldtypes %}

&lt;form method="POST" action="."&gt;
  {% for field in form %}
    {% if field.is_hidden %}
      {{ field }}
    {% else %}
      &lt;div class="{% field_type field %}"&gt;
        {{ field.errors }}
        {{ field.label_tag }}: {{ field }}
      &lt;/div&gt;
    {% endif %}
  {% endfor %}
  &lt;input type="submit" name="myform" value="submit"/&gt;
&lt;/form&gt;</code></pre>

<p>The check for hidden fields allows us to render such fields as just hidden fields without errors or labels. In Django 1.1, we could make this a little nicer, by iterating form.visible_fields and form.hidden_fields independently.</p>
  </li>
</ol>

<h2>Conclusion</h2>
<p>The result, by simply exposing the name of the widget type for a form field, is a simple generalized approach to rendering forms with the following field-wrapper classes (plus any custom widget types you have created) applied appropriately to the form fields:</p>

<ul>
 <li>TextInput</li>
 <li>PasswordInput</li>
 <li>HiddenInput</li>
 <li>MultipleHiddenInput</li>
 <li>FileInput</li>
 <li>DateInput</li>
 <li>DateTimeInput</li>
 <li>TimeInput</li>
 <li>Textarea</li>
 <li>CheckboxInput</li>
 <li>Select</li>
 <li>NullBooleanSelect</li>
 <li>SelectMultiple</li>
 <li>RadioSelect</li>
 <li>CheckboxSelectMultiple</li>
 <li>MultiWidget</li>
 <li>SplitDateTimeWidget</li>
 <li>SelectDateWidget</li>
</ul>
<p>This approach is certainly not without its limitations. There is not a simple way, for example, to deal with fieldsets -- in which case explicit rendering of fields is probably in order. But, for most of your forms, this should provide a simple means for rendering even large forms in very few lines while maintaining style-ability. With a sub-form, we could simplify things even further, so that rendering a custom field-wrapped form is as succinct as using one of Django's "as_x" shortcuts. I will leave this as an exercise for you and will include that simplification in my next post.</p>

<h2>Coming next</h2>
<p>In the next post, we will look at how this approach, combined with using the YAML CSS framework, can make quick work of form styling. As mentioned, we will also clean up this approach a bit by taking advantage of include templates to simplify form declaration even more.</p>

-sB</div>
</content>



    </entry>
    <entry>
        <title>KeePass on Karmic Koala</title>
        <link rel="alternate" type="text/html" href="http://www.codeslick.com/2009/10/keepass-on-karmic-koala.html" />
        <link rel="replies" type="text/html" href="http://www.codeslick.com/2009/10/keepass-on-karmic-koala.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a00e54fe53ddf88330120a692e130970c</id>
        <published>2009-10-30T10:29:43-05:00</published>
        <updated>2009-10-30T11:39:49-05:00</updated>
        <summary>Here are the steps I took to get KeePass installed on Ubuntu 9.10, Karmic Koala.</summary>
        <author>
            <name>Scott Bradley</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Applications" />
        
        <category scheme="http://sixapart.com/ns/types#tag" term="install" />
        <category scheme="http://sixapart.com/ns/types#tag" term="karmic" />
        <category scheme="http://sixapart.com/ns/types#tag" term="karmic koala" />
        <category scheme="http://sixapart.com/ns/types#tag" term="keepass" />
        <category scheme="http://sixapart.com/ns/types#tag" term="keepassx" />
        <category scheme="http://sixapart.com/ns/types#tag" term="libraries" />
        <category scheme="http://sixapart.com/ns/types#tag" term="qmake" />
        <category scheme="http://sixapart.com/ns/types#tag" term="ubuntu" />
        <category scheme="http://sixapart.com/ns/types#tag" term="ubuntu 9.10" />
        <category scheme="http://sixapart.com/ns/types#tag" term="x11" />
        
<content type="xhtml" xml:lang="en-US" xml:base="http://www.codeslick.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p><a href="http://www.keepassx.org/" title="KeePassX official home page">KeePassX</a>, the cross-platform port of <a href="http://keepass.info/" title="KeePass Password Safe home page">KeePass Password Safe</a>, the excellent password manager, requires a few libraries that do not come in the standard Ubuntu 9.10 installation, which just officially released yesterday.</p>

<p>Here are the steps I took to get KeePass installed:</p>

<ol>
<li>sudo apt-get install build-essential</li>
<li>sudo apt-get install libqt4-dev</li>
<li>sudo apt-get install xorg-dev</li>
<li>download and extract the <a href="http://sourceforge.net/projects/keepassx/files/" title="KeePassX files on SourceForge">KeePassX tarball from SourceForge</a></li>
<li>in the extracted directory: </li>
 <ol>
 <li>qmake</li>
<li>make</li>
<li>sudo make install</li>
 </ol>

</ol>
<p>KeePass should now be available in the Accessories</p><p></p><ul>
</ul>
<ul>
</ul>
<p></p>

<p></p><blockquote>
	
</blockquote></div>
</content>



    </entry>
    <entry>
        <title>The gant clean target</title>
        <link rel="alternate" type="text/html" href="http://www.codeslick.com/2008/03/the-gant-clean.html" />
        <link rel="replies" type="text/html" href="http://www.codeslick.com/2008/03/the-gant-clean.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-47622708</id>
        <published>2008-03-27T14:08:49-05:00</published>
        <updated>2008-03-27T14:08:49-05:00</updated>
        <summary>I just had an &quot;oh crap, I don&#39;t have a backup for that&quot; moment when playing with Gant. Gant is an Ant scripting tool which you can checkout at Codehaus. I am looking for a way to make my Ant...</summary>
        <author>
            <name>Scott Bradley</name>
        </author>
        
        
<content type="html" xml:lang="en-US" xml:base="http://www.codeslick.com/">
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;p&gt;I just had an &amp;quot;oh crap, I don&#39;t have a backup for that&amp;quot; moment when playing with Gant.&amp;nbsp; Gant is an Ant scripting tool which you can checkout at &lt;a href=&quot;http://gant.codehaus.org/&quot;&gt;Codehaus&lt;/a&gt;.&amp;nbsp; I am looking for a way to make my Ant builds a little more dynamic, and seeing how I am already using Groovy in some of my projects anyway, Gant looks like a great fit.&lt;/p&gt;

&lt;p&gt;Here&#39;s a caveat though: the Gant clean target does not seem to work as advertised.&amp;nbsp; I included something like the following in my build.gant file:&lt;/p&gt;&lt;blockquote class=&quot;codeblock&quot;&gt;&lt;p&gt;includeTargets &amp;lt;&amp;lt; gant.targets.Clean&lt;br /&gt;cleanPattern&amp;nbsp; &amp;nbsp;&amp;lt;&amp;lt; &#39;**/*.class&#39;&lt;br /&gt;cleanDirectory &amp;lt;&amp;lt; &#39;${build.dir}&#39;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;expecting to see the .class files in my build directory disappear with an invocation of gant clean.&amp;nbsp; Well, not only my .class files are gone ... the whole directory structure was deleted.&amp;nbsp; So, be careful when using the gant clean target.&amp;nbsp; I&#39;m not sure if I am doing something wrong here, this is virtually identical to other examples I have seen, including the documentation on the official Gant page.&amp;nbsp; I suggest building your own clean task.&amp;nbsp; The following is working for me:&lt;/p&gt;&lt;blockquote class=&quot;codeblock&quot;&gt;&lt;p&gt;target ( clean : &#39;Delete generated files&#39; ) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; delete {&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; fileset (dir: &#39;${build.dir}&#39;) {&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;include (name: &#39;**/*.class&#39;)&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;}&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;More on Gant after I recover those files.&lt;/p&gt;

&lt;p&gt;sB&lt;/p&gt;&lt;/div&gt;
</content>



    </entry>
    <entry>
        <title>Rediscovering my roots</title>
        <link rel="alternate" type="text/html" href="http://www.codeslick.com/2008/03/rediscovering-m.html" />
        <link rel="replies" type="text/html" href="http://www.codeslick.com/2008/03/rediscovering-m.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-47508352</id>
        <published>2008-03-25T10:23:24-05:00</published>
        <updated>2008-03-25T10:23:24-05:00</updated>
        <summary>I have been wanting to try out video podcasts on my Nano for a while now. So, during my down time between quarters this week, I have decided to check out the available OpenCourseWare videos at M.I.T. Last night I...</summary>
        <author>
            <name>Scott Bradley</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Education" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Engineering" />
        
        
<content type="html" xml:lang="en-US" xml:base="http://www.codeslick.com/">
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;p&gt;I have been wanting to try out video podcasts on my Nano for a while now.&amp;nbsp; So, during my down time between quarters this week, I have decided to check out the available OpenCourseWare videos at M.I.T.&amp;nbsp; Last night I watched the first lecture for &lt;a href=&quot;http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-002Spring-2007/CourseHome/index.htm&quot;&gt;6.002 Circuits and Electronics&lt;/a&gt;.&amp;nbsp; Wow, this lecture got me so wired I couldn&#39;t sleep last night.&amp;nbsp; In one fell swoop, Professor Agarwal managed to provide&amp;nbsp; a stimulating introduction to the realm of electrical engineering while simultaneously providing me with a justification for my own progression from electrical engineer to computer scientist.&amp;nbsp; Sure, I already knew that there is a continuity between these disciplines - but to see it drawn out in one big picture was still enlightening.&lt;/p&gt;

&lt;p&gt;Basically, the gist of the lecture was this:&amp;nbsp; &lt;/p&gt;

&lt;ul&gt;&lt;li&gt;As electrical engineers, we start out with observable principles in nature -- the electrical properties of the physical things around us -- and we create layer upon layer of abstraction from that starting point.&lt;/li&gt;&lt;/ul&gt;

&lt;ul&gt;&lt;li&gt;Each layer of abstraction provides a simplified understanding of the complexities of the previous layer, and provides us with a means for more simply creating interesting systems.&amp;nbsp; &lt;/li&gt;&lt;/ul&gt;

&lt;ul&gt;&lt;li&gt;Along this line of thought, we move from real electrical characteristics of physical things to simplified versions of those characteristics.&amp;nbsp; We build upon that to create various types of electrical systems, eventually finding ourselves in the digitial abstraction, and then the clocked digital abstraction which of course leads us to computer systems, which we further abstract with operating systems and programming languages.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;Ok, that is the very simple version.&amp;nbsp; I highly recommend the lecture for non-techies who want to get a grasp on the big picture of electrical/computer/software engineering.&amp;nbsp; The few real technical details he throws out should not be a impediment to understanding the overall lecture.&amp;nbsp; Techies too -- I realize this is freshman level material -- but I can say that if I would have had a lecture like this way back in my early undergraduate days at Purdue, I think my whole perspective going into engineering would have been different.&amp;nbsp; Even now, [mumble]-teen years later, I found Professor Agarwal&#39;s lecture to be a stimulating re-introduction to the field.&lt;/p&gt;&lt;/div&gt;
</content>



    </entry>
    <entry>
        <title>Losing your marbles over the equals method, or: when equals is not the same</title>
        <link rel="alternate" type="text/html" href="http://www.codeslick.com/2008/03/losing-your-mar.html" />
        <link rel="replies" type="text/html" href="http://www.codeslick.com/2008/03/losing-your-mar.html" thr:count="1" thr:updated="2008-03-02T12:22:44-06:00" />
        <id>tag:typepad.com,2003:post-46327346</id>
        <published>2008-03-01T18:17:49-06:00</published>
        <updated>2008-03-01T18:17:49-06:00</updated>
        <summary>The Problem This is fairly fundamental, but easy enough to forget if you are not used to overriding the equals method in Java. Recite this mantra every morning as you grind your coffee beans: When you override the equals method,...</summary>
        <author>
            <name>Scott Bradley</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="CS101" />
        
        
<content type="html" xml:lang="en-US" xml:base="http://www.codeslick.com/">
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;p&gt;&lt;span style=&quot;font-size: 1.2em;&quot;&gt;The Problem&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This is fairly fundamental, but easy enough to forget if you are not used to overriding the equals method in Java.&amp;nbsp; Recite this mantra every morning as you grind your coffee beans: &lt;strong&gt;When you override the equals method, override hashCode too.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Equals who?&amp;nbsp; Hash code wha?&amp;nbsp; Let&#39;s see the code.&lt;/p&gt;

&lt;p&gt;Let&#39;s say you have a bag o&#39; marbles.&amp;nbsp; Marbles are pretty simple, they have a color.&amp;nbsp; We instantiate a few and throw them into the bag (implemented here as a Set):&lt;/p&gt;

&lt;blockquote class=&quot;codeblock&quot;&gt;&lt;p&gt;import java.util.HashSet;&lt;br /&gt;import java.util.Set;&lt;/p&gt;

&lt;p&gt;public class Marble {&lt;/p&gt;

&lt;p&gt;&amp;nbsp; &amp;nbsp; private String color;&lt;/p&gt;

&lt;p&gt;&amp;nbsp; &amp;nbsp; public Marble(String color) { this.color = color; }&lt;/p&gt;

&lt;p&gt;&amp;nbsp; &amp;nbsp; public String getColor() { return color; }&lt;/p&gt;

&lt;p&gt;&amp;nbsp; &amp;nbsp; public static void main(String[] args) {&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; Set&amp;lt;Marble&amp;gt; bagOMarbles = new HashSet&amp;lt;Marble&amp;gt;();&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; bagOMarbles.add(new Marble(&amp;quot;red&amp;quot;));&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; bagOMarbles.add(new Marble(&amp;quot;red&amp;quot;));&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; bagOMarbles.add(new Marble(&amp;quot;blue&amp;quot;));&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; bagOMarbles.add(new Marble(&amp;quot;blue&amp;quot;));&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; System.out.println( bagOMarbles.size() &lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;+ &amp;quot; marbles in the bag.&amp;quot; );&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;}&lt;/p&gt;&lt;/blockquote&gt;





















&lt;p&gt;Compile and run.&amp;nbsp; How many marbles are in the bag?&amp;nbsp; If you don&#39;t understand why there are 4, stop here until you&#39;ve got it.&amp;nbsp; Got it?&amp;nbsp; 4 marbles in the bag, 2 are red, 2 are blue.&amp;nbsp; But let&#39;s say we have a magic bag that only allows us to carry one of any given color.&amp;nbsp; No matter how many red marbles you put in this magic bag, there will be no more than one red marble when you reach back into the bag.&amp;nbsp; Same with any other color of marble.&amp;nbsp; How do you implement that magic bag?&amp;nbsp; &amp;quot;Easy,&amp;quot; you say, &amp;quot;the Set only allows unique objects to be contained, so we define equality of marbles to be based on color.&amp;quot;&amp;nbsp; Sounds good to me.&amp;nbsp; And so we override the equals method:&lt;/p&gt;

&lt;blockquote class=&quot;codeblock&quot;&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; public boolean equals(Object obj) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; if (this == obj) return true;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; if (obj != null &amp;amp;&amp;amp; getClass() == obj.getClass()) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;Marble otherMarble = (Marble)obj;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if (color.equals(otherMarble.getColor())) return true;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Compile and run.&amp;nbsp; How many marbles are in the bag?&amp;nbsp; If you&#39;re thinking 2, then recite the mantra, take a sip of coffee and read on.&amp;nbsp; The answer is 4.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Why is it 4?&amp;nbsp; Why does one red marble not equal another? Why are the two blue marbles not considered to be the same? I&#39;ve clearly defined them to be equal.&amp;nbsp; In the mantra is the answer: &lt;strong&gt;When you override the equals method, override hashCode too&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;font-size: 1.2em;&quot;&gt;What is hashCode?&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Simply put, hashCode provides a means for organizing your objects into groups, or hash buckets.&amp;nbsp; You will generally not use the hash code of an object directly, but Java will use it in order to more efficiently do an equality check.&amp;nbsp; The check goes something like this:&amp;nbsp; Are the hash codes the same?&amp;nbsp; If yes: check the equals method.&amp;nbsp; If no, don&#39;t bother checking the equals method since &lt;strong&gt;objects with differing hash codes are assumed not to be equal&lt;/strong&gt;.&amp;nbsp; That last part in bold is the assumption that Java makes when checking for equality when adding an object to a Set.&amp;nbsp; And therein lies the problem:&amp;nbsp; it is an assumption that may not be true since nothing, not even the compiler is enforcing this rule.&amp;nbsp; It is your job as a programmer to be sure this assumption is correct, so once again, &lt;strong&gt;When you override the equals method, override hashCode too&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So what happens inside our magic marble bag?&amp;nbsp; When we throw a marble into the bag, something like this occurs:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Are there any marbles in this bag with the same hash code as this new marble?&lt;/li&gt;

&lt;li&gt;Nope -- okay, add the marble.&lt;/li&gt;&lt;/ol&gt;



&lt;p&gt;Note:&amp;nbsp; equals is never even checked!&amp;nbsp; Only when the hash code is the same will equals be called.&amp;nbsp; If you&#39;re not buying it, put a print statement in the equals method to convince yourself.&amp;nbsp; The default implementation of hashCode will yield different hash codes for each object because it is based on the object ID, not on the object properties, and as it stands, the equals method will never get called when we throw a new marble into the magic marble bag.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;font-size: 1.2em;&quot;&gt;Try This&lt;/span&gt;&lt;/p&gt;
 

&lt;p&gt;So, then, remember the mantra and add the following method override:&lt;/p&gt;&lt;blockquote class=&quot;codeblock&quot;&gt;&lt;p&gt;// not a good, but still valid, hash code&lt;br /&gt;public int hashCode() { return 37; }&amp;nbsp; &lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;This hashes all marbles into the same bucket.&amp;nbsp; Note, this is not a good hash code, but a valid hash code nevertheless, and one which is consistent with our definition of equals.&amp;nbsp; Any integer value here would have the same effect.&amp;nbsp; I will save writing better hash codes for a future post -- right now just understand that even this inefficient single-bucket hash code is better than nothing, since the default hash code is no longer contractually valid once we override equals.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;So what happens when we add, for example, our second red marble?&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Are there any marbles in here with hash code 37?&amp;nbsp; Yeah - what&#39;d you think?&lt;/li&gt;

&lt;li&gt;Ok, so then are there any marbles here that are red?&amp;nbsp; Yeah - go away - no more room for red marbles.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;And after adding 2 red marbles and 2 blue marbles, we have our expected Set total of 1 red + 1 blue.&lt;/p&gt;

&lt;p&gt;For more efficient comparisons, be sure to look into better hash code overrides.&amp;nbsp; But even if efficiency is not your concern, be sure to provide some hash code other than the default whenever you implement your own equals.&lt;/p&gt;&lt;/div&gt;
</content>



    </entry>
    <entry>
        <title>Attaching a positive attitude to my business frustrations</title>
        <link rel="alternate" type="text/html" href="http://www.codeslick.com/2008/02/attaching-a-pos.html" />
        <link rel="replies" type="text/html" href="http://www.codeslick.com/2008/02/attaching-a-pos.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-45782348</id>
        <published>2008-02-18T11:54:45-06:00</published>
        <updated>2008-02-18T11:54:45-06:00</updated>
        <summary>One of the principles I&#39;ve learned from reading Tim Ferriss is that it should be okay to complain, provided you do it in the same breath as suggesting the next step to avoid the situation in the future. I complain...</summary>
        <author>
            <name>Scott Bradley</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Business" />
        
        
<content type="html" xml:lang="en-US" xml:base="http://www.codeslick.com/">
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;p&gt;One of the principles I&#39;ve learned from reading &lt;a href=&quot;http://www.fourhourworkweek.com/blog/&quot;&gt;Tim Ferriss&lt;/a&gt; is that it should be okay to complain, provided you do it in the same breath as suggesting the next step to avoid the situation in the future.&amp;nbsp; I complain a lot -- mostly as a way of thinking out loud, and sometimes it helps to relieve some stress and to work out a problem, particularly if through my complaining I can solicit feedback from others (yeah, that&#39;s mostly you &lt;a href=&quot;http://khontent.com/&quot;&gt;Kev&lt;/a&gt;).&amp;nbsp; But in general I think I could learn something from the principle that Ferriss has, and add a positive twist to my gripes.&lt;/p&gt;

&lt;p&gt;So, for this first post on CodeSlick, I&#39;m beginning on a sour note, but I am doing so with a purpose.&amp;nbsp; I want to articulate my next steps and also provide any caveats, advice, etc. to others who might be starting or running a small business.&lt;/p&gt;

&lt;p&gt;Taxes.&lt;/p&gt;

&lt;p&gt;Okay, now you know the &amp;quot;sour note&amp;quot; part was not an exaggeration.&amp;nbsp; I just had a horrible weekend because I decided it was my weekend to do business taxes.&amp;nbsp; Here is what I figured out:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;I&#39;ve been recording my payroll incorrectly.&amp;nbsp; Not surprising considering I&#39;ve been 100% do-it-myself so far with my business. &lt;/li&gt;

&lt;li&gt;Running a business in the state of IL is bad enough before you start making money.&amp;nbsp; It&#39;s worse after.&lt;/li&gt;

&lt;li&gt;My bookkeeping is really behind and not nearly as organized as it should be.&amp;nbsp; Probably because I really dislike doing it and see the time much better spent programming and solving technical problems.&lt;/li&gt;&lt;/ol&gt;





&lt;p&gt;So, in the same breath, here are my own replies to my complaints:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; I&#39;ve completed my tax forms, but because of my questions about payroll recording, deduction and depreciation recording, and all-around general uncertainty about the whole thing, I think it is time to get an accountant.&amp;nbsp; This week I am going to find an accountant to review my tax returns before filing them, and hopefully begin a professional relationship that will relieve what has amounted to a tremendous amount of stress for me.

&lt;/p&gt;

&lt;p&gt;My advice to small business owners:&amp;nbsp; Get an accountant sooner than later.&amp;nbsp; I started my company while working part-time tech gigs and going to graduate school, and looked in every corner for opportunities to cut costs.&amp;nbsp; If I were to do it again, this is the one place I would choose to spend money.&amp;nbsp; Don&#39;t want to spend money on having someone else file your articles and give you a shiny business binder for your biz plan?&amp;nbsp; Fine, money well saved -- do it yourself.&amp;nbsp; But, unless you have accounting skills already, I suggest you find someone who does to setup your books and to go to when you have questions or concerns.&amp;nbsp; I have not yet established a relationship with an accountant.&amp;nbsp; I will let you know how this works out when I do.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; I run an S-corporation.&amp;nbsp; Some people like LLCs, I chose S.&amp;nbsp; The merits of either is probably a subject best left to be expounded by your accountant, your lawyer, or both.&amp;nbsp; For purposes of federal taxes, S-corporations are nice.&amp;nbsp; What do I owe Uncle Sam?&amp;nbsp; $0.&amp;nbsp; That is a zero.&amp;nbsp; I didn&#39;t run the comparisons (which Turbo Tax does not really do easily by the way), but I would venture to say that I could eliminate virtually all of my expenses and depreciations, etc. and still owe $0.&amp;nbsp; Again, that is a zero.&amp;nbsp; Then there is state.&amp;nbsp; The state of Illinois seems to have made an art out of the practice of obfuscation for business owners.&amp;nbsp; There is no end to the pieces of mail I get from various departments who want a chunk of my business in terms of money and time.&amp;nbsp; So it turns out I was supposed to be paying corporate taxes through the year.&amp;nbsp; Not payroll taxes - I&#39;ve been paying those of course.&amp;nbsp; But even my pass-through S-corp which gets taxed a big fat nothing by Uncle Sam has to pay income tax to the state. Depressing and frustrating.&amp;nbsp; I will probably also get slapped with a fine for not paying in advance.&amp;nbsp; My next step: I plan to itemize every document that the state is expecting from me.&amp;nbsp; Corporate annual reports, payroll income taxes, corporate taxes, unemployment insurance, etc.&amp;nbsp; I will audit my payroll service to be sure they are doing the right forms.&amp;nbsp; For the forms they do not handle, I will setup some sort of tickler or calendar system for myself.&lt;/p&gt;

&lt;p&gt;My advice to anyone considering starting a business:&amp;nbsp; When you read all that advice about starting your business in Delaware or Nevada, consider it seriously.&amp;nbsp; I can&#39;t tell you what is right for your biz, but take into consideration the stress that will be incurred from the sheer amount of paperwork that your state will require.&amp;nbsp; Try to get a handle ahead of time - before you incorporate - on what those requirements will be.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Of course, it is my own fault that my bookkeeping is behind.&amp;nbsp; Part of my disorganization has been due to a move that has had me unpacking boxes for the past several months.&amp;nbsp; But, part has been due to my compulsion to do it all myself.&amp;nbsp; I spent months trying to come up with the perfect bookkeeping/accounting setup.&amp;nbsp; As a Linux user, I searched hard for a solution that would work in my world.&amp;nbsp; The short answer is that such a solution does not exist.&amp;nbsp; After being fed up with using spreadsheets to manage everything, I tried GNU Cash, but found it to be frustrating, and I knew that when I did get an accountant, I would have a hard time finding someone willing to work with my accounting system.&amp;nbsp; Even the web-based versions of QuickBooks and PeachTree require you to have Internet Explorer.&amp;nbsp; It is a sad state of affairs, but that is the way it is.&amp;nbsp; In the end, I&#39;ve gone with QuickBooks Simple Start, which is a free version of QuickBooks and seems to have everything I need.&amp;nbsp; I do my payroll through a service offered by my bank, so I don&#39;t need Intuit&#39;s help there thank you.&amp;nbsp; I keep a $250 Compaq PC running with Vista (don&#39;t even get me started on that subject) for the sake of running QuickBooks.&amp;nbsp; So, I guess you could say my accounting system cost me $250, but only after a lot of headache, thinking that I would find a solution that fit into my ideology.&amp;nbsp; I am considering the possibility of going with QuickBooks Online.&amp;nbsp; I
have heard that it is slow and frustrating, but I would like to
consider the possibility of outsourcing some of my bookkeeping, and I&#39;m
not sure yet how I will expose my system to a virtual assistant in a
way that I am comfortable with.&lt;/p&gt;

&lt;p&gt;My advice: The perfect solution for accounting and bookkeeping does not exist.&amp;nbsp; Particularly for those of us with less mainstream taste in our choice of computing tools.&amp;nbsp; Pick a solution that will make your relationship with your accountant as smooth as possible and go with it.&amp;nbsp; Trust your accountant&#39;s knowledge and experience more than your principles and ideology on this one.&amp;nbsp; Consider early in the development of your business how you will outsource bookkeeping and accounting tasks when you need to.&lt;/p&gt;

&lt;p&gt;I hope someone wanting to start a small business can get something out of this post.&amp;nbsp; My of my future posts will focus on more technical material, but I do hope to slip in some business related subjects occasionally.&amp;nbsp; I&#39;ve got a few new items on my to-do list now it seems, and so until next post, eyes on the code ... it can be slick sometimes.&lt;/p&gt;

&lt;p&gt;sB&lt;/p&gt;&lt;/div&gt;
</content>



    </entry>
 
</feed>

<!-- ph=1 -->

