Thursday, August 7, 2008

Leading from the heart


Recently I was asked to meet with a group of tech leads, in order to impart on them my leadership style. It was very flattering, as they spoke highly of how they wished to emulate me.... to be more like me.... to adopt my style.

I warned them that they should seek their own style. One that felt natural to them, rather than mimic me. There are many paths to success, of which I've only walked one. However I did my best to describe the way I manage and lead a group of developers. I became enthralled with the fact that the point they latched onto the most was one in which I mentioned it is important to say what you mean and mean what you say. I thought this was just common sense...

Most people (and last time I checked, developers are people) wish to be respected. They want to feel included, part of the group, part of the community. They want to work on things that make a difference. They wish to have purpose.

Larger organizations tend to formalize their communication. Rather then get to brass tacks and actually say what we mean 'Hey Joe, your coding skills aren't up to snuff', we soften and cushion the blow and say something else, and create white lies like 'Hey Joe, we've found an excellent position for you in the mailroom. It is a lateral move which is great for your career!'

My advice was to speak plainly to the developers, and be honest (although not brutally so). Most developers aren't dummies, and can see through any mask a leader may put on to sugar coat things. If as a leader you are re-organizing the team in order to save money, don't tell the developers your motivations are to improve their efficiency, make their lives easier, or streamline the process. The end goal is to save money, and it should be clearly stated.

Many view leadership as a pyramid, with the leader on top and those that report to them forming the base. In this model, it is the workers responsibility to assist the leader in accomplishing goals and objectives. My view is that this pyramid should be inverted. The leader's motivation should be to assist the workforce in realizing as much potential as they possibly can. A leader must memorize and internalize (e.g. believe) in 5 words, which they must continually repeat to each member on the team 'HOW CAN I HELP YOU?'

This form of servant leadership requires someone who honestly cares about their team members. I've reported to some leaders who have asked how they could help me, but it was clear from their tone that they didn't actually want to.... or didn't care about me. These 5 simple words can become the downfall of a leader who truly doesn't care, and is out for their own gain.

Leadership can be rewarding, when you can see the fruits of a highly functioning team. However it comes with a curse, as your personal biases and feelings must be suppressed for the greater good of the team. It is easy to be a dictator, barking orders from on high to obedient lackeys who quiver in fear in your presence. It is much harder to take the time to listen and understand those who are not clear what the team's mission is, and articulately communicate your desires and goals rather than direct orders.

Monday, August 4, 2008

Rolling up the sleeves, and turning on a dime

One of the most difficult challenges that every architect faces is the balance between the pragmatic and the academic thinking. As an old proverb says, one must walk with their head in the clouds and their feet on the ground. Too academic, and you are an ivory tower architect whom is an outcast from the development world. Too pragmatic, and suddenly you are making short sighted decisions and are shunned by your business and executive stakeholders.

Developers reject ivory tower architecture for good reason. Why should developers listen to an architect whom has no clue what they are talking about? The fact that the architect can string together a group of buzzwords into an eloquent sentence does not make them a good architect. Therefore it is an architecture imperative that the architect continues to keep their skills relevant, and understands the technology on which they work. This is not to say that the architect should aim to be in the same league as the master developer. Often this is too difficult to attain considering the architect's desire to acquire a wide breadth of knowledge.

An architect must roll up their sleeves and get their hands dirty. As I discussed in my blog article about the architect's wide breadth of knowledge, there must be a focus on gaining practical hands on experience over academic learning. While it should not be avoided altogether, academic learning must be approached with caution as it tends to breed ivory tower thinking. The architect should be close to their team, understanding in clear detail the development lifecycle, and the health of the codebase. I recommend spending some time pair programming with some of the technical leaders on the team.

Don't become entrenched. Since many architects are born from a technical background, it is often the case that they become entrenched and lost within the codebase, never to return. As with academic learning, hands on coding time must be controlled and monitored. Never become assigned to a task which is on a project's critical path, which has a deadline. At worst, ensure that task is assigned to another team member, with yourself in a high level support and mentoring role. The intent is to survey the codebase, and be performing inspections, not to fall back into development (regardless at how good you are at it, and how enjoyable it is). Time must be equally spread among the project management, requirements, and development disciplines. If you find yourself more often tied to the code, then perhaps you should reconsider your career. Some developers are forced into architecture because it is sold as an upwardly mobile career path. I suggest you find a company that values your focused developmental skills, and offers you more glory and pay for slinging code if that is what you gain the most pleasure doing.

Ruby on Rails for Architects #6 - Spit and Polish

Continued from: Ruby on Rails for Architects #5 - A Working WIKI

By now we have a working WIKI on Ruby on Rails, however it lacks some features which bring the WIKI fully to life. Namely, the ability to link WIKI posts together. We will clone the syntax from some of the popular WIKI's. Links to other WIKI pages will be wrapped in double brackets, [[LikeThis]].

Ruby has a powerful syntax for dealing with regular expressions. If you haven't worked with regular expressions before, this may feel overwhelming. In a nutshell, a regular expression is a pattern matching language for searching strings. We will add a new method to the Revision model. Edit app/models/revision.rb and add the following method:


# Regular Expression to match words surrounded
# by double brackets [[LikeThis]]

WIKI_LINK = %r/\[\[\s*([^\]\s][^\]]+?)\s*\]\]/

# Return the Revision's content marked up in HTML format
def to_html

rawtext = content
if rawtext.nil?
return ""
end

# First, HTMLify the wiki links
rawtext.gsub!( WIKI_LINK ) do |matched_text|
name = matched_text.gsub(/[\[\]]/,"")

"<a href=\"" + name + "\">" + name + "</a>"
end

rawtext
end

Ruby's gsub method can find patterns matching a regular expression, and allow a block of code (within the do block) to be executed on it. The string returned from the do block replaces the searched text. Therefore this is a basic search & replace method.

In our code, when any grouping of [[WikiLink]] is found, the brackets are stripped off, and the link replaced with an HTML anchor (<a href>) tag.

Now we must modify the HTML in our VIEW to call our new method. Modify the last line in app/views/wiki/view.rthml to appear as so:

<%= @page.revisions.last.to_html if @page %>

Now, rather than simply displaying the revision contents, they are transformed via the to_html method we just created.

Extra Credit
I won't cover them in this tutorial, but there are a variety of extra credit activities that could be performed to make this program more functional.

  1. Markup - Utilize a markup/markdown library to provide full support for text manipulating (such as bold, italic, headers, etc). A nice one to use is called RedCloth. You can add support to this library in the newly created to_html method on the Revision model.
  2. Revision History - Since previous versions of the content are stored within the database, the controller can be modified to display content for previous versions. I'd recommend adding a new button on the layout to view the history.
  3. Tagging - Tagging WIKI pages would require the addition of new columns to the database schema. There are also Ruby gems available to assist in adding tagging support.
  4. HTML Prettiness - The HTML in this tutorial is rather bland. Modification to the layout and views can do wonders for making this appear like a fully functional application. For inspiration, look at this layout gala informing you how to utilize CSS to layout a page.

Friday, August 1, 2008

Ruby on Rails for Architects #5 - A Working WIKI

A continuation from: Ruby on Rails for Architects #4 - Analysis and Design

MODELS
To have a working WIKI, we not only need Pages, but also Revisions. In the previous article, we created the Pages model, view and controller. For this exercise, we will create the Revisions model, and expand the design. Run the following command (from your project's root directory) to create the Revisions model.

ruby script/generate model revision

Within the app/models directory, you will find both a page.rb and revision.rb. Viewing them will reveal empty class structures with no methods. But it is important to note that in the previous article we still had a fully functionally CRUD application. This is due to the fact that these classess extend the ActiveRecord:Base object, will will dynamically at runtime grant a series of methods to this object allowing it to query the database table matching the pluralized form of the model object. Rails is smart enough to know that the plural of revision is revisions, and the plural of page is pages. It also assumes you have followed the comment convention to use underscores for column names. So for example, first_name in the database would be automatically translated into firstName. Helper methods are also provided to assist in finding and interacting with the data in the database. Even though this is an empty class structure, it is possible to call Page.find_by_name() or Revision.find_by_id(). We will see the power of this in a little bit.

While the models work fine individually, we need to inform Rails of the referential integrity and relationships which exist between these two models. A single page may have many revisions, and a single revision has a single parent page.

In revision.rb add inside of the class:
belongs_to :page
In page.rb add inside of the class:
has_many :revisions

CONTROLLER
Next we will configure the controller. Edit the app/controllers/wiki_controller.rb file. We will add a method for each method we wish to expose to the user via the URL. As we discovered with our scaffolding of the pages controller in a previous article, the controller in question is referenced by the URL, followed by the method inside of the controller. Therefore, if we have a method in our wiki controller known as view. Then that method will be executed when we point our browser to http://localhost:3000/wiki/view.

The URL string, and how it is interpreted by the controller is edited through the config/routes.db file. Let's open that file now and make some specific changes for our new wiki controller. Add the following two lines into routes.rb:

map.connect '', :controller => "wiki"
map.connect 'wiki/:action/:name', :controller => "wiki"

The first pattern matches empty quotes for the controller 'wiki'. This will map a URL of http://localhost:3000 and direct it to the index method of the wiki controller. This is useful for removing that Ruby on Rails starting page we see when we start the web server. For the Rails page to be fully eradicated, you must also delete the html file public/index.html

The second pattern informs the controller that the URL should be expected to be parsed in a manner in which the controller's name (wiki) is followed by the action, or method on the controller to execute, followed by the name of the WIKI page we wish to perform an action on (actions being edit, view, etc). Therefore, http://localhost:3000/wiki/view/Home will load the app/controllers/wiki_controller.rb controller, and call the view method upon in, passing a parameter of name="Home".

Let's add some functionality to our controller. Paste the following methods into the app/controllers/wiki_controller.rb:

def index
redirect_to :action=> 'view', :name => 'Home'
end

def view
@name = params[:name]
@page = Page.find_by_name(@name)
end

def edit
@name = params[:name]
@page = Page.find_by_name(@name)
end

def save
@content = params[:content]
@name = params[:name]
@page = Page.find_by_name(@name)

if !@page
@page = Page.create(:name => @name)
end

Revision.create(:page => @page, :content => @content)

redirect_to :action => :view, :name => @name
end


Let's quickly walk through these methods one by one.

index - This method is called whenever the controller is accessed directly. (e.g. http://localhost:3000/wiki). In these instances, we simply redirect to viewing of the home page of the wiki.

view - Pull the @name variable off of the URL parameters that was passed, and use the Page model to find an object in the database matching on the name field. Remember above where I explained the find_by methods are dynamically accessed thanks to ActiveRecord.

edit - This method will be invoked in order to display the edit form to the user. This is necessary since we will wish to show them the content which currently exists in the database, and give the user to modify it (rather than present them with a blank form). Therefore, this method mimics view. The difference between view and edit is the .html which will be delivered to the end user. By default, the view which will be delivered will match the name of the method in the controller. Therefore after the view method is called, app/views/view.rhtml will be displayed, and edit will cause app/views/edit.rthml to render. More about this in a minute.

save - The user will not see this URL, it is called from the form displayed within app/views/edit.rthml. The purpose of this method is to create a new record in the Page database table, if it does not yet exist. Then a new Revision record is created in the database. While not a requirement now, we wish to have the ability to

VIEWS
To save some coding, Rails comes bundled with the ability to support layouts. We can create a layout for our controller by creating a file called app/views/layouts/wiki.rthml. This layout will be rendered alongside each view which is executed by the controller. Create the wiki.rthml file and add some simple content such as this:

<html>
<head>
<title>
Wiki > <%= request.path_parameters['action'] %>
</title>
</head>

<body>
<%= yield :layout %>
</body>

</html>

This simple layout will allow us to remove the skeleton of the page (e.g. html,head,title,body) from our views, making them appear less cluttered.

Before we can finish, we have 2 more files to create. app/views/view.rthml and app/views/edit.rthml.

app/views/view.rthml:
<%= link_to 'view', :action => 'view', :id => @name %>
<%= link_to 'edit', :action => 'edit', :id => @name %>


<%= @page.revisions.last.content if @page %>

app/views/edit.rthml:
<%= link_to 'view', :action => 'view', :id => @name %>
<%= link_to 'edit', :action => 'edit', :id => @name %>

<h2 id="pageName"><%= @name %></h2>
<%= form_tag :action => "save" %>
<input type="hidden" name="name" value="<%= @name %>">

<textarea name="content" rows="20">
<%= @page.revisions.last.content if @page %>
</textarea>

<input name="Save" type="submit" value="Save" id="Save">
</form>


Tags beginning with <%= contain executable ruby code, rendering the output of their result to the screen. Since these views were executed after the controller's methods, the variables used in the controller are available here. Therefore, <%= @name => outputs the @name variable created in the controller, same goes for the @page variable.

Rails offers a series of helper methods to display dynamic HTML tags. link_to and form_tag both ouput HTML tags (link_to outputs 'A' anchor/link tags, and form_tag outputs 'form' tags).

Thanks to ActiveRecord, we can query all of the Revisions for a page using the @page.revisions syntax. Here, we take that power a step further, asking ActiveRecord to only return us the most recent of revisions (@page.revisions.last), and then displaying the value of the contents field from the database (@page.revisions.last.content).

WORKING WIKI
Now we have a functioning wiki. Once again, start Rail's embeded web server:

ruby script/server

and point your browser to http://localhost:3000. You can now edit and view pages by manipulating the URL. In our next, and final article.... we will learn how to transform the text in the database to WIKI markup language.

Continued in: Ruby on Rails for Architects #6 - Spit and Polish