I Hate it When I Learn from Dilbert

Do we all live in fear of that moment when we notice that we’re the one on the other side of Dilbert? When Dilbert is wise and well, we’re not.

Two weeks ago I was writing a long paper explaining some nuances about the state of the templates at that time and asking my client not to reject it until he had looked into it and really understood it. So, in the next morning’s Dilbert strip someone comes to Dilbert and says “I’ll tell you my idea if you promise not to reject it until thinking about it” and Dilbert says “I already rejected it because only putrid ideas come with warnings”

So I spend the better part of the weekend rationalizing that my idea really doesn’t fall into that category.

And then I spring out of bed at 6AM Monday morning (I sort of wish that part was a joke) with the solution. So, let’s look at the problem today and the solution in the next post:

Yesterday’s code was:

 

Private Function MemberGetPrimaryKey() As String
      Return OutputFunction( _
                           Symbols.Method.GetPrimaryKey, _
                            Scope.Protected, _
                            MemberModifiers.Overrides, _
                            ObjectData.PrimaryKey.NetType, _
Function() _
<code>
   Return m<%= ObjectData.PrimaryKeys(0).Name %>
</code>.Value)
End Function

 

It’s easy enough to ditch the Return statement with a constant. I put these constants in a class and imported the class, which allowed me to directly access the constant, although it was in a different file:

 

Private Function MemberGetPrimaryKey() As String
      Return OutputFunction( _
                           Symbols.Method.GetPrimaryKey, _
                            Scope.Protected, _
                            MemberModifiers.Overrides, _
                            ObjectData.PrimaryKey.NetType, _
Function() _
<code>
   <%= returnString %> m<%= ObjectData.PrimaryKeys(0).Name %>
</code>.Value)
End Function

 

How bad is that?

But as Bill pointed out in comments on the last post, if all I’m doing is returning a value, I don’t need the code block at all and can teach the OutputFunction method to do the job. So, switching to a more complex and common example, and remembering that I’m out to solve the C#/VB single template problem to allow a single template for any architecture, I took these concept a few steps further. The result of a more complex method becomes:

 

  Private Function MemberPropertyAccessSet(ByVal propertyData As IPropertyData) As String
         Return _
<code>
         CanWriteProperty(“<%= propertyData.Name %>”, true)
         <%= OutputConditional(“m” & propertyData.Name & ” <> value”, _
            Function() _
            <code>
            m<%= propertyData.Name %> = value
            PropertyHasChanged(“<%= propertyData.Name %>”)
            </code>.Value) %>
</code>.Value
      End If
   End Function

 

Which for a single language is the same as merely doing:

 

 Private Function MemberPropertyAccessSet(ByVal propertyData As IPropertyData) As String
         Return _
<code>
         CanWriteProperty(“<%= propertyData.Name %>”, true)
         If m<%= propertyData.Name %> &lt;<> value
            m<%= propertyData.Name %> = value
            PropertyHasChanged(“<%= propertyData.Name %>”)
        End If
</code>.Value
End Function

 

Which would you rather debug? Imagine debugging the templates for an even more complex routine.

This spawned my Dilbert moment. If I have to convince you of the wisdom of this, then maybe it’s not so wise. So, what I jumped out of bed to do was build a preprocessor – converting templates that output VB into templates that output C#. By removing that technical restriction, we can find the sweet spot between reducing typos and obfuscating the code logic. I think that is about where the first and last code fragments in this post are. What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>