<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6111003760176345680</id><updated>2011-10-07T14:01:39.353+02:00</updated><category term='hungarian notation'/><category term='naming'/><category term='rant'/><category term='wart'/><category term='value object'/><title type='text'>Developer Inside</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://devloperinside.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6111003760176345680/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://devloperinside.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Samuel</name><uri>http://www.blogger.com/profile/06388423186442139936</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-C-kst1lJakQ/To7qIfFAmOI/AAAAAAAAAFw/JhrM0iWoyA8/s1600/f866fca5a7395a360cca27b5e9da0ed7.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6111003760176345680.post-8574703711674676034</id><published>2010-01-28T18:04:00.033+01:00</published><updated>2010-01-31T13:16:36.763+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='wart'/><category scheme='http://www.blogger.com/atom/ns#' term='rant'/><category scheme='http://www.blogger.com/atom/ns#' term='naming'/><category scheme='http://www.blogger.com/atom/ns#' term='value object'/><category scheme='http://www.blogger.com/atom/ns#' term='hungarian notation'/><title type='text'>Learning about the Wart</title><content type='html'>When I first read about &lt;a href="http://en.wikipedia.org/wiki/Hungarian_notation"&gt;Hungarian Notation&lt;/a&gt; I was baffled how anyone could even earn money thinking and writing about things as naming. Even less how anyone could suggest using several letters of consonants for a variable name followed by a "descriptive" suffix such as &lt;span style="font-family:'courier new';"&gt;wnd&lt;/span&gt;. I soon learned the importance of naming, but never grasped the benefits of the Hungarian notation.

&lt;p&gt;About a year later I found out that this had in fact been adopted in the &lt;a href="http://en.wikipedia.org/wiki/Windows_API"&gt;Win32 API&lt;/a&gt;. As an API lot's of people look up to it as exemplary code and follow it's style lavishly and the Hungarian notation spread quickly.&lt;/p&gt;

&lt;p&gt;Today, to my great relief, the hungarian notation has vanished almost entirely and there is a reassuring concensus among most programming communities that it was a horrible path that must never be followed again.&lt;/p&gt;

&lt;p&gt;However, lately there has been an unhealthy outbreak of the hungarian wart. Apparently inspired by &lt;a href="http://www.joelonsoftware.com/articles/Wrong.html"&gt;this&lt;/a&gt; article by &lt;a href="http://www.joelonsoftware.com/AboutMe.html"&gt;Joel Spolsky&lt;/a&gt; people claim it has been all misinterpreted. The original thought was not to prefix by variable type but rather by variable purpose. This is called &lt;i&gt;Apps Hungarian&lt;/i&gt;. The conveyed feeling is that there is the bad™ Hungarian notation as spread by the Win32 API and then there is the forgotten good Hungarian notation as intended by its inventor. This is an encouraging thought. It is true that it was misinterpreted. It's also true that Hungarian Apps is superior to the much despised System Hungarian notation.&lt;/p&gt;

&lt;h3&gt;Still not quite right&lt;/h3&gt;
This new mutation of the persistent Hungarian Wart is sadly also problematic.

&lt;p&gt;
Let's dive further into the example given by Joel Spolsky since it it a very good &lt;a href="http://www.joelonsoftware.com/articles/Wrong.html"&gt;example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So to recap we have have this html-rendering systems that always must &lt;i&gt;encode&lt;/i&gt; string it writes. As the the problem becomes more interesting we find out that some strings are not encoded while others are. Those cases must be handled by the system and the goal here is of course to make the code easy to read and that errors are noticed easily.&lt;/p&gt;

&lt;p&gt;The Apps Hungarian comes in handy here. If you use clever prefixing such as &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;s&lt;/span&gt; for encoded strings (&lt;b&gt;s&lt;/b&gt;afe to write)  and &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;u&lt;/span&gt; for nonencoded strings (&lt;b&gt;u&lt;/b&gt;nsafe to write) you can clearly see a programming error.&lt;/p&gt;

&lt;p&gt;&lt;span style="font-family:monospace"&gt;Write(uName);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This statement obviously contains an error since a nonencoded string is written. The point here is that each row can be inspected and decided whether it might cause an nonencoded string to be written.&lt;/p&gt;

&lt;h3&gt;The twist&lt;/h3&gt;
&lt;p&gt;Here is the twist: Rather than using prefixes to diffrentiate between two types of strings - Use the &lt;a href="http://en.wikipedia.org/wiki/Type_safety"&gt;type system&lt;/a&gt;! In this exact example I propose using two diffrent classes - The &lt;span style="font-family:monospace"&gt;EncodedString&lt;/span&gt; and the &lt;span style="font-family:monospace"&gt;RawString&lt;/span&gt;. This way we can make the &lt;span style="font-family:monospace"&gt;Write&lt;/span&gt; method to only take a &lt;span style="font-family:monospace"&gt;EncodedString&lt;/span&gt; as parameter. If desired we can even create an overloaded &lt;span style="font-family:monospace"&gt;Write&lt;/span&gt; method that takes the raw string and automacially encodes it before writing it!&lt;/p&gt;

&lt;p&gt;Ok, so what are the cons of this approach. Yes, a tiny bit of extra boilerplate since the &lt;span style="font-family:monospace"&gt;EncodedString&lt;/span&gt; class and its raw ditto has an inner &lt;span style="font-family:monospace"&gt;string&lt;/span&gt; field that required operations are delgated to. Lets say encoded strings needs to be concatenated. This can be implemented for example like this in the &lt;span style="font-family:monospace"&gt;EncodedString&lt;/span&gt; class.&lt;/p&gt;

&lt;p&gt;&lt;span style="font-family:monospace;white-space:pre"&gt;
    public EncodedString operator +(EncodedString rhs)
    {
        return new EncodedString(_s + rhs._s);
    }
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Also this gives you a performance penalty for the extra method call. In a html-rendering program like this, and basically for all programs, I would say this is neglectable.&lt;/p&gt;

&lt;p&gt;In the extremly buzzed &lt;a href="http://domaindrivendesign.org/"&gt;Domain Driven Design&lt;/a&gt; lingo this is called a &lt;a href="http://domaindrivendesign.org/node/135"&gt;Value Object&lt;/a&gt;. Call it what you want , but it's a nice trick!&lt;/p&gt;

&lt;p&gt;On the topic - I saw this declaration the other day:

&lt;p&gt;&lt;span style="font-family:monospace;white-space:pre"&gt;
    class FooExtensionsClass
&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;So it's a class that is a class that has the word "class" in the class name. Ok, ok, I get it. Watch out - the wart might be back again. True story.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6111003760176345680-8574703711674676034?l=devloperinside.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://devloperinside.blogspot.com/feeds/8574703711674676034/comments/default' title='Kommentarer till inlägget'/><link rel='replies' type='text/html' href='http://devloperinside.blogspot.com/2010/01/legacy-of-hungarian-wart.html#comment-form' title='2 kommentarer'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6111003760176345680/posts/default/8574703711674676034'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6111003760176345680/posts/default/8574703711674676034'/><link rel='alternate' type='text/html' href='http://devloperinside.blogspot.com/2010/01/legacy-of-hungarian-wart.html' title='Learning about the Wart'/><author><name>Samuel</name><uri>http://www.blogger.com/profile/06388423186442139936</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-C-kst1lJakQ/To7qIfFAmOI/AAAAAAAAAFw/JhrM0iWoyA8/s1600/f866fca5a7395a360cca27b5e9da0ed7.png'/></author><thr:total>2</thr:total></entry></feed>
