jordan.terrell
Just trying to make sense of things...

Never Underestimate A Well Written Regular Expression

Tuesday, 6 April 2010 11:36 by jordan.terrell

A couple of weeks ago, Kirill Osenkov posted an interview question that got the attention of a few .NET developers, myself included.  Like a moth to a flame, we were all eager to present a solution to this interview question:

In a given .NET string, assume there are line breaks in standard \r\n form (basically Environment.NewLine).

Write a method that inserts a space between two consecutive line breaks to separate any two line breaks from each other.

Roughly 20 answers were given in the comments to Kirill’s post, some with subtle differences, some with completely different approaches.  Some were even written in F#.

When I first saw the interview question, I very quickly came to my answer:

   1: string output = Regex.Replace(input, @"(\r\n)(?=\r\n)", "$1 ");

My answer uses Regular Expressions, which is a concise language to search text, sometimes in complex ways.  I first became aware of Regular Expressions in 2004, and I was immediately enamored with them.  I had always written complex search functions using operations like IndexOf() or directly accessing character arrays.  Text searching always seemed slow to me, but I later realized that it was just my poorly written code.  Very quickly I dove into learning the Regular Expression language (at least the dialect in .NET), and found many uses for it.

I’ve found since then that many developers are either unaware or fearful of Regular Expressions.  I’ll admit, some expressions that I’ve seen look very cryptic and intimidating.  But they are very powerful.  Plus they have the benefit of usually being very fast (although you can write slow ones).

During the discussion in the comments of Kirill’s post, it became obvious that performance is something to be considered in such a routine.  As a result, Rik Hemsley commented that he had created a benchmarking test bed to run each suggested solution.  Here are the results:

image

My solution came out as the best performing.  I say this, not to gloat - because in reality I’m just using what Microsoft wrote for us to use. I say it because I wanted to show that knowing about and using Regular Expressions is important when you need to parse text.  I’m sure that someone could come up with a better performing solution, but for a one-liner, Regular Expressions are hard to beat.

If you interested in learning about Regular Expressions in .NET, the MSDN documentation is pretty good.  Plus there are books that you can read on them as well as sites that have examples.

State Machine ViewModel

Friday, 19 March 2010 08:09 by jordan.terrell

Jason Bock recently wrote a post on turning WPF Binding errors into exceptions.  When I first started to get into WPF development, I too found that the binding system was significantly better (i.e. actually worked) that Microsoft’s previous attempts to do UI binding.  That said, I also found, as Jason did, that WPF’s binding system was less than helpful when you fat-fingered a binding.  I did learn that there we many ways that you could get debugging output from the binding system, but I never took it to the level that Jason did and turned the binding errors into exceptions.  I’m sure I’ll find his binding extension useful in the future.

Sometimes Invisible Binding Errors are Okay

However, I did make a comment on Jason’s post that later on I found a use for not having exceptions be thrown on data binding issues errors.  There is a pattern that I’ve used on one or two WPF applications where I host a collection of “screens”, which are just UserControls, in a single Window, and the visibility of the UserControls is driven by the DataContext of the Window.  The DataContext is both a ViewModel in the Model-View-ViewModel pattern, and an implementation of the State pattern.

The difference is that, in a traditional State pattern implementation, you typically have a base class or interface that defines all of the input or operations that all the states need to respond to.  However, with a state-based ViewModel and WPFs dynamic binding system you will see this is not necessary.

Let’s start with a simple scenario, a wizard-like, step-by-step WPF application for taking simple feedback comments from users.  Let’s say the list of states is: AskForComment, CaptureName, CaptureEmail, CaptureText, and Thanks.  Clicking the next button will transition from one state to the next, changing the DataContext of the Window to the new state.  As the state changes, the WPF binding system will be notified of this.  It will attempt to rebind the visibility of all the UserControls to the new DataContext, which is the new state, and depending on the state, different UserControls will be visible.  The UserControls that are not visible, will have binding errors, but since they are not visible and thus not in use, the errors can be ignored.  The user can happily enter information into the newly visible control, unaware of any errors.

I’ve created a sample application that shows one way this could be implemented. If you look at the MainWindow.xaml, you will see five UserControls that represent the different views as the states change.  Each of them has their Visibility property bound to the current state through a StateVisibilityConverter.  Inside each UserControl, various elements are bound to properties on the DataContext that may or may not exists, depending on what state you are in – but again, that doesn’t matter – if the properties don’t exist, the UserControl should not be visible.

The key takeaway is that sometimes you can use the lack of exceptions on WPF binding errors to your advantage, and I would venture a guess that Microsoft decided not to throw exceptions on binding errors to enable scenarios like this.

“M” and Oslo’s Future

Wednesday, 11 November 2009 11:37 by jordan.terrell

If you’re at all interested in Oslo, you may be looking forward to the 2009 PDC to see the direction it is going to take.  As of yesterday, we got a small preview of that direction – and to be honest, without having all the nitty-gritty details that I hope will come out of the PDC, I’m concerned and disappointed – and I’m not the only one who feels this way.

“M” Interacting with the Database – Please Don’t!

I understand that using DSLs and modeling is an excellent way to capture and manipulate data that can be used by applications.  However, this quote from Doug’s post is what concerns me (emphasis is my doing):

Time after time we heard that “M” would make interacting with the database easier

The “M” language and tooling should have absolutely nothing to do with interacting with the database.  The fact that Microsoft has heard “time after time” that people would like to use “M” to interact with the database strikes me as a problem with many people not understanding what I believe “M” was envisioned to do and should be used for.  “M” (MGrammer, MGraph, MSchema) and its supporting tooling should be about the definition and runtime representation of models and languages used to create and manipulate instances of models.  It is my strong opinion that this functionality should have no direct dependency on databases or database interaction.  The core foundational value I saw in Oslo was a shared platform providing:

  • A DSL definition language (MGrammer)
  • A lowest common denominator representation of a model (MGraph)
  • Model schema definition and validation (MSchema)
  • Tooling (Intellipad, m.exe, possible VS integration, etc…)

What is so sadly ironic is that Microsoft recognized this from the beginning.  During many presentations at the 2008 PDC, the bond between developers and text and text editors was mentioned.  Microsoft knew they needed to have a first class story to tell when it came to text, and that was how “M” was born.

The Repository and Quadrant

My opinion of Repository is that it is a mistake to try to tackle Repository with the first release of Oslo.  Even during the 2008 PDC presentations it felt like the Repository was a solution in search of a problem.  I understand the value in being able to use models to define runtime execution characteristics of an application (e.g. HTML, XAML, WCF Service Descriptions, etc), but how many have you seen that execute their models from a SQL Server database?!?!? There might be a small class of applications where it makes sense to store and execute a model from a database, but my guess is that more often than not a model would either be stored in or transformed into something that looks nothing like a database.  Perhaps it would be embedded into an application redistributable as code or an embedded resource or persisted as a file.  Perhaps it is never persisted.  Regardless, if the model is to be persisted, that should be a separate responsibility.  Repository is a “nice to have”, but honestly I can’t see using it much, if at all.

As for Quadrant, I don’t feel versed enough in Quadrant’s capabilities to voice a strong opinion.  I do see value it having a common tool for visualizing and manipulating models.  However, I would spend my “development dollars” less on Quadrant for Oslo’s first release, and more on the “M” language and tooling.

Concerned, but Hopeful

Douglas has made it clear that there is more information coming about the “M” and DSL story.  I for one hope that “M” can stand alone from Repository, Quadrant, SQL Server, and anything to do with databases.  If this is not the case, I hope the Oslo team hears loud and clear that it should make some fundamental changes.

However, if “M” does stand alone, this post and the comments from others should help to keep it that way – even when “time after time” people associate “M” with database interaction.

Tags:   ,
Categories:   Programming | .NET
Actions:   E-mail | del.icio.us | Permalink | Comments (2) | Comment RSSRSS comment feed

Scott Chacon – Git Documentation Master

Friday, 30 October 2009 08:48 by jordan.terrell

As I’ve continued my investigation into Git, a name kept appearing whenever I found excellent Git documentation.  That name is Scott Chacon.  This is a person who favors concepts over commands, and I immensely appreciate his efforts in demystifying Git.  I’ve found three resources that he, to one extent or another, has authored – all of which are great.

Of the three resources, for someone starting out I would recommend the Git Internals PDF.  It’s concise and simple to understand, and I’ve already seen one person who, more or less was anti-Git, read it in an hour and become pro-Git.  The other two resources are excellent as well.

I have to hand it to Scott, he sure knows his Git.  He just also knows how best to teach it.

Tags:  
Categories:   Programming
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

iSynaptic.Commons Lives

Friday, 30 October 2009 08:35 by jordan.terrell

The last time I talked about iSynaptic.Commons was in July of 2008.  Basically I said that I was putting the release on hold.  Well, I’ve finally released it under the MS-PL license.

My reason for releasing it is by no means because it is complete.  It is no where near there.  I’ve got a lot of work yet to do on it, and I’ve got many things that I want to completely change or remove.  So my recommendation is that you don’t go use the code in a project you intend for it to keep stable, because it is going to be a moving target for now (i.e. there will be breaking changes).  Especially the Text.Parsing namespace and the Xml namespace.  Those were very naive and simplistic, and I will be replacing.

However, my primary reason for releasing it is to get feedback.  I don’t expect much at first, but I want it to be something I can talk about in this blog, and solicit direct feedback about specific parts of the framework.

Finally, I talked about iSynaptic.SolutionBuild and iSynaptic.Modeling in my previous iSynaptic.Commons post.  SolutionBuild exists and is in partially working order.  I will, at some point, be working on moving this into a public repository.  I may even change SolutionBuild to be based on PowerShell, and possibly psake.  As for the Modeling project, I’ve re-envisioned it under a different project I’m calling iSynaptic.Core.   Currently Core doesn’t exists (other than the repository being stubbed out), but what I wanted to accomplish with the Modeling project will be wrapped into the Core project.  Core will depend on Commons (Commons being a lower-level framework).  Likely, some of what is in Commons will be pushed into Core – I want to keep Commons lightweight, but useful.  Besides rolling Modeling into Core, details on what Core will be is something for a future post.  I don’t necessarily want to talk about it, until I have something to show.

Initially, my activity on all of these initiatives will be light.  I have numerous projects that are work related that I need to complete, and other non-development personal obligations as well.  This is just me taking the first step…

Tags:  
Categories:   .NET | Programming
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Fowler’s DSL Book Milestone

Wednesday, 21 October 2009 14:08 by jordan.terrell

Martin Fowler just updated his roadmap for his upcoming DSL book.  I can’t wait to get it!

Tags:   ,
Categories:   Programming
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Favor Concepts Over Commands

Monday, 19 October 2009 13:25 by jordan.terrell

Finally, I understand Git!!!

For a while I’ve been scratching my head on why so many people are jumping on the Git bandwagon.  I understood conceptually the benefits of a Distributed Version Control System (DVCS), but every time I sat down to learn Git I ended up more confused than when I started.

I even bought a book - Pragmatic Version Control Using Git.  Even after going through that book I still didn’t get it.  I was drowning in a sea of commands and I understood the basics of adding and committing files, however, outside of that I was finding myself getting into trouble and not having a clue on how to work through it.  I tried using some visual tooling to help, but it didn’t.  I attributed it to Git being far to complicated (and in some ways it is compared to other VCSes), and stopped trying to learn Git with a plan to pick it up in the future.  Much later in the future.

Then I stumbled on a book that, although being published by Apress, is available in open source formPro Git.  Not much time had passed since my last attempt, so I wasn’t really enthusiastic about reading another Git book, so I semi-begrudgingly read chapter 1.  Still, I wasn’t excited – chapter 1 covered the same stuff that was in Pragmatic Version Control Using Git.  Chapter 2 wasn’t that much better – just a laundry list of commands that you are presented with.

Honestly, I don’t know why I kept reading, but I’m SO glad I did.  Chapter 3 is where things just clicked.  The author started to use visualizations to show how Git works, and he started to explain all the underlying concepts of how Git represents things.  As each command is executed, he would show a visualization of how the Git repository changed, and that further reinforced the conceptual ideas.  Thru the rest of the book the visualizations were used and this added so much to the value of the content.

I finally understand Git and I will be slowly moving over to it as my main VCS – replacing Subversion.  I still think that the supporting tools need to mature (e.g. Visual Git tools – I don’t like command prompt source control), but the basic toolkit is rock solid.

If you want to learn Git, I strongly suggest you buy Pro Git (support the author).  If you plan to teach Git using any medium (e.g. book, blog, talk), please favor teaching concepts over commands – your audience just might git [sic] it!

Tags:  
Categories:   Programming | General
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

DNUG Talk: Follow Up

Friday, 2 October 2009 10:01 by jordan.terrell

I think last night’s talk I gave on Domain Driven Design was a success.  I received good feedback from those who attended.  We had around 50 attendees.  A good number of people had never heard of Domain Driven Design (which was surprising to me), many had heard of it but didn’t know much about it, and one or two people had read the book.  It was exactly the audience makeup I was targeting.

Domain Driven Design is not an easy topic to introduce.  It’s very broad, at times very complex and intricate, and it is a very conceptual topic – that is to say, it is more about how you think and communicate about a domain (business problem/solution space), and less about how you write software around the domain.

I outlined a number of follow up resources – frankly a LOT of follow up resources.  Domain Driven Design isn’t something that you are going to pick up in a few months; it is going to take time to absorb and apply.  There are technical concerns that often influence how you apply Domain Driven Design, and those concerns can often be addressed with other documented patterns – that’s why I provided supporting resources.

If you came to my talk, I’d appreciate any feedback you’d like to offer.  I’m giving the talk again at the Twin Cities Code Camp on October 24.  That is going to be a challenge – I only have 75 minutes at that venue, and I easily used 90 minutes last night (excluding discussion about follow up resources).

Thanks to everyone who came!

DNUG Talk: Intro to Domain Driven Design

Thursday, 1 October 2009 07:51 by jordan.terrell

I’m giving a talk today at the Twin Cities .NET User Group, sponsored by ILM and hosted at Microsoft.  I chose to give it on Domain Driven Design because it is something that I’ve been learning since 2005, but there still seems to be many people who don’t know what it’s about.  I’ve received lots of feedback from people expressing interest in the topic, so hopefully we’ll see a good turn out.

I’ll be posting the resources after the talk – stay tuned!

Customer Is Not A Party

Monday, 3 August 2009 10:59 by jordan.terrell

This is in response to this blog post and it’s comments (read it first).

I prefer the usage of the term Party because I also believe it is a more common abstract term.  I will say this though: Customer is not a Party.  It is a 'Role' that a 'Party' plays.  For instance, I would use 'Person' and 'Company' as parties, and 'Customer' and 'Supplier' as roles.

This is not my own idea.  Martin Fowler does a good job of talking about Accountability patterns, which is where he references the concept of a 'Party', but that concept goes much further than the Accountability patterns.

Archetype Patterns

There is a much greater pattern.  In fact there are two patterns - Archetype Patterns.  The first is the 'Party' Archetype Pattern, and then there is the 'Party Relationship' Archetype Pattern.  Both are closely related, as the latter builds on the former.  These two Archetype Patterns are well covered in a wonderful, if not horribly named book: Enterprise Patterns and MDA (hereafter EPM).  Many Archetype Patterns are discussed: Party, Party Relationship, Customer (which more accurately models this as a Party Role), Product, Inventory, Order, Quantity, Money, and Rule.

I would strongly recommend someone looking at Martin Fowler's Analysis Patterns for modeling ideas/concepts to also read EPM.  In my opinion EPM is, in some ways, a superset of what Analysis Patterns talks about, and EPM's patterns are more generally applicable – hence usage of the term Archetype.

One final comment - with all of these patterns there are two principles that need to be applied, as Martin Fowler pointed out (Analysis Patterns, page 13):

Principle: Patterns are a starting point, not a destination.

Principle: Models are not right or wrong, they are more or less useful.