Archive for the ‘Web Development’ Category

HAML Basics

Monday, April 25th, 2011

HAML (HTML Abstraction Markup Language) is a template engine responsible for addressing many of the flaws in traditional markup. HAML is Whitespace Active, DRY, follows CSS Conventions, and integrates Ruby or PHP code. Its results in very clean and elegant markup.

I will be focusing on the Ruby implementation in this posting, but note that there are also implementations for PHP, .NET, Java, and several other languages. As you might expect with Ruby, HAML is installed as a gem, and once installed, all .erb files can be renamed .HAML to be run through the HAML compiler.

When using HAML, tags are marked with the % symbol. For example:

%strong hello

This would be compiled to:

<strong>hello<strong>

Although you could write:

%div hello

to create a div, it is not necessary as HAML makes the common div tag the default. It also borrows the period symbol from CSS to denote class, so the following:

.content Hi

would be compiled to:

<div class='content'></div>

Classes can also be chained, so the following:

.content.notice Bye

would be compiled to:

<div class='content notice'>Bye</div>

HAML is whitespace active, so we can nest divs as follows:

#parent
  #child
    #grandchild
      #brother-twin I am the evil twin
      #sister-twin I am the good twin

to create:

<div id="parent">
  <div id="child">
    <div id="grandchild">
      <div id="brother-twin">I am the evil twin</div>
      <div id="sister-twin"> I am the good twin</div>
    </div>
  </div>
</div>

Comments are also more legible in HAML:

/ here is a comment

would generate the bulkier:

<!-- here is a comment -->

HAML includes filters which are denoted by a colon, and some examples are

:plain
:javascript
:markdown

You can also create your own. Usage is simple:

#content
  :markdown
    Some *emphasis*

would compile to:

<div id="content">
  <p>Some <em>emphasis</em></p>
</div>

Including Ruby (or other) code is either silent with the dash symbol, or inline with the equals symbol.

#content
  .title
    %h1=@title

would create:

<div id="content">
  <div class="title">
    <h1>My Page</h1>
  </div>
</div>

One of my favorites is the Object Reference, which uses square brackets with an object. Basically, given an object, it will work as div_for and content_for in Ruby, generating a id and class name based on the id and class of the given object. This is very handy for use with javascript and jquery and really polishes the markup.

%div[monster]
  %ul.eyeballs
    -monster.eyeballs.each do |eyeball|
      %li[eyeball]
        =eyeball.color
would create:
<div id="monster_4">
  <ul>
    <li id="eyeball_1">Green</li>
    <li id="eyeball_2">Blue</li>
    <li id="eyeball_3">Grey</li>
  </ul>
</div>
(B.S. Computer Science, Rutgers, 1995) Alex spent a few years programming games with LucasArts for the Saturn and Playstation platforms, before turning his attention to Web programming.

Getting the CRUD out

Friday, March 18th, 2011

The topic of one of GORGES recent internal Friday Tech Talks was “Yii CRUD: Tips and Tricks.”  I proposed this because I was curious to hear war stories from other developers about the problems and solutions they ran into when building “CRUD” – principally in the Yii framework, which has become one of our favorite tools at GORGES for creating complex web applications.

CRUD stands for Create, Read, Update and Delete (or, for some folks, Create, Retrieve, Update and Destroy). It refers to the basic operations that any data-centered application needs to make it run – creating new objects or records, viewing them, editing them, and getting rid of them. The code that performs these operations tends to get pretty repetitive after a while, so for a seasoned developer it can become the part of the application that you most want to “get out of the way” so you can focus on the more interesting bits, like complex business logic and nifty Javascript widgets. It is also often predominant in the administrative side of an application, where there is typically less budget for fancy design and interactivity than on the public side – but it still needs to work effectively. Thus, when developers refer to it as “crud”, they don’t always have the acronym in mind.

Here are some of the comments I heard from our team on how to create CRUD efficiently, in order to deliver quality, maintainable, cost-effective applications to our clients:

1. Don’t build it at all, if you can help it - For simpler web applications, or ones that are more content-oriented, a “full stack” framework like Yii or Ruby on Rails can be overkill – a platform such as Drupal can take care of most of the housekeeping operations, with coding (or configuration) only required for the more “custom” features of the application.

2. Use code generators – Yii and most other advanced web frameworks include tools for generating “skeleton” code for CRUD operations. Most developers felt that these are a good starting point, especially when first learning a framework, but after a while it becomes more efficient to write from scratch, with judicious use of copy and paste from existing similar code (but beware the Rule of Three).

3. Modularize your MVC components – Even though the Model-View-Controller idiom forces the developer to think in modular terms to some extent, it is still possible to write overly repetitive code within that idiom, and violate the DRY principle. Frameworks such as Yii provide a great foundation for building a web app, but the larger and more complex an application is, the more it makes sense to build on that foundation by using inheritance, polymorphism, and other object-oriented techniques in the CRUD (as well as in the non-CRUD components).

4. Build widgets where appropriate – This is just a specific example of modularization; Yii provides a widget API for building components that can be reused in different areas of an application. There are many standard widgets available, for components such as date pickers, but again, in some cases custom-building just the widget you need for a given application makes sense to keep everything “DRY”.

5. Use advanced languages - Just like application frameworks, many of the standard web development languages provide a great basis, but leave plenty of room for additional optimization. Two tools that our team is very excited about are HAML and SASS, which provide more efficient and elegant syntax for generating XHTML and CSS, respectively, than coding directly to each language. I won’t go into technical details here, but both tools allow the developer to create more robust solutions with fewer keystrokes.

These techniques and the fact that GORGES has structured ways of building staff efficiency, illustrate how GORGES developers are constantly looking for better ways to deliver quality web applications – avoiding CRUD!

Ted Caldwell is an Application Developer and Senior Software Architect for GORGES. Ted's work encompasses all aspects of web application development, including requirements analysis, database and software design, programming, project management, and system administration. Ted focuses on delivering practical, cost-effective technology solutions for GORGES clients, using a variety of custom and off-the-shelf software tools and frameworks.

Unit Testing – A Key Part of Web Development Best Practices

Thursday, March 10th, 2011

Unit Testing: What is it and Why is it Important?

A unit test is a program that verifies that another program works as expected. Unit testing is an approach to software development in which the programmers write tests for their code to make sure that it continues to work as the system evolves.

A simple example of a unit test for some programming code that calculates sales tax based on the state in which an order is placed would have inputs of a state and an order amount, and test whether the result is the expected sales tax.

The ‘test suite’ is run continuously during development, instantly alerting developers to emerging code issues. The developers also run the test suite before publishing updated code to make sure they don’t break anything that used to work. Some software development environments, such as Rails and .NET provide unit testing frameworks out-of-the-box. Other development environments need varying levels of work to configure a working unit testing framework.

A common objection to unit testing is the time and expense it takes to write the tests themselves versus the time it would take along the way to fix bugs as they emerge. We at GORGES have found this to be a spurious argument. As we write programs, we have to check that they work and that they don’t break other programs. If we don’t have tests, we have to manually click around. That introduces both the element of human error (oops I forgot about that!) and the expense of paying humans the time it takes to do all that clicking when a machine could do it just fine. However these are not the only benefits of unit testing:

  • Programming code changes that break an existing test are identified immediately, allowing the programmer to fix the problem while the issues are still fresh, saving time, and money.
  • Unit tests provide effective code documentation so that new programmers on the project can become productive faster.
  • Changes made to supporting software, such as frameworks and operating systems, are tested quickly, reducing the risk of introducing bugs when technology upgrades are made.
  • The cost of implementing new features in the future is reduced because the unit tests provide context and ensure that old programming code is not broken by the new programming code
  • Refactoring, a common task of restructuring the code to make it easier to use and maintain, easier and less time-consuming because the unit tests ensure the refactoring process does not break the application.
  • Regression bugs, issues that appear multiple times, can have specific unit tests written for them, so ensure we don’t get ‘bitten’ twice or more times by the same bug.

Unit testing is not a panacea for ensuring that a software application is free of bugs – almost all software applications exhibit a problem at some point. The value of unit testing is that it helps to significantly reduce bugs, and make it easier and cheaper to fix any bugs that do appear.

Web Development – Adapt or Die

Monday, November 22nd, 2010

The blog title sounds extreme, but there is truth to these words in our industry.

I have been developing software since high school, and building software and web applications for about thirty years.  If there is one thing I can count on, it is that the software business will continue to change.

Ten years ago we had primitive web browsers, and web pages were usually exclusively HTML.  Microsoft and their proprietary ActiveX technology dominated the browser wars.  As a developer, there were few debugging tools and no decent server-side or Javascript frameworks.  Developing web sites took time, and the results were clunky and crude by today’s standards.

I am pleased at how efficient we are nowadays and how much value we offer, since we develop great solutions at a fraction of the time and cost compared to the days of web infancy.  We have learned to leverage existing open source or proprietary packages as much as possible, and have a wealth of development, debugging, and deployment tools in our arsenal.  If we are allowed to target “modern” browsers such as IE7/IE8, Firefox and Safari, then we can count on browser support for features required by web 2.0 graphics and behaviors.

The web server hardware industry has also had amazing strides, and every year we see better value and better prices.  For example we recommended a single modern server for hosting a client’s complex web application instead of their previous vendor’s recommended 3-server cluster approach; the resultant performance has been similar to our estimated model, and every month for the last three years our customer has saved thousands of dollars since hosted cluster solutions are expensive.

Back to the blog title:  in our business, if we do not continue to learn from and embrace technology improvements, then we will lose our competitive edge.  The obvious result would be that we will no longer be quality or price competitive in the web development market.  Few other industries have this sort of pressure – consumer electronics and mobile phones are probably other examples.

It is interesting to note that only the software industry allows small firms to compete with larger ones, since creating new hardware products require so much more capital than software development.  That is one reason why there is so much more innovation and creativity in the software industry, which really has exploded now that laptops and app phones are ubiquitous.

What will the web world look like in the future?  Prognostication is not one of my strengths, but I imagine we will see more web applications tailored towards mobile solutions (app phones & differently-sized tablets), continual improvements in frameworks, and probably the browser battles will continue to be fought between Microsoft, Firefox, and Google.

And as for myself, I plan on continuing to learn and adapt.

worked in academia, corporate research labs and several technology startup companies prior to GORGES. His expertise is software architecture, database development, and system administration. Matt brings GORGES over 25 years experience developing fast and robust software on a multitude of platforms and languages.

Add REST-style URLs to older Java web applications using UrlRewriteFilter

Thursday, November 11th, 2010

Say you have an old old Java Web application (jsp-based) that uses URLs like this:

/queryVehicles.jsp?make=Ford&model=Fusion

But we’re in 2010 where URLs should be pretty and more REST-style like so:

/vehicles/ford/fusion

Well, you have two options. You can re-write your web application to use newer web frameworks that supports REST-style URLs such as Spring MVC 3.0, which will probably take many months to do. Or you can use UrlRewriteFilter which should take no more than 30 minutes and will work with any existing Java web application.

UrlRewriteFilter very easy to use. You basically:

  1. Add UrlRewriteFilter as a servlet filter in your WEB-INF/web.xml
  2. Populate the configuration file WEB-INF/urlrewrite.xml with mappings of REST-style URLs to the corresponding URLs of your legacy web application (with query params and what not). UrlRewriteFilter is similar to mod_rewrite of Apache HTTP Server. So your rewrite rule would look something like this:
     <rule>
         <from>^/vehicles/([a-z]+)/([a-z]+)$</from>
         <to>/queryVehicles.jsp?make=$1&amp;model=$2</to>
     </rule>

That’s pretty much it.

UrlRewriteFilter is extremely powerful. You can perform pretty complex mapping since it uses regular expression pattern matching. Check out the manual page for details.

Saving brain cells with WinSCP

Wednesday, September 22nd, 2010

One of my daily challenges is getting access to the hundreds of different web sites that we host in order to make edits, tweak file permissions, or fix bugs. It’s easy to remember a handful of different URLs, login IDs, and passwords, but my photographic memory only has a 5-1/4″ floppy for storage, so as the number grows I need help.

One tool that’s helped me achieve more ninja-like response times is WinSCP, a free GUI-based FTP/SFTP client. Like most FTP clients, it lets me maintain a list of stored sessions that I can open in a flash, but what sets it apart is the slick integration with the popular PuTTY SSH client.

I came to WinSCP by a round-about path – for a long time I used FileZilla for SFTP and PuTTY for SSH, but the effort of maintaining a separate list of site logins for each tool ate up valuable brain cycles and finger motions that could have been used profitably for other tasks. When I discovered WinSCP, it was like a floor wax and a dessert topping in one – the tool lets you launch both PuTTY and SFTP sessions to a given server in a single stroke. Sometimes you need a little command shell, sometimes you need a lot – now you don’t have to choose.

Setting up the PuTTY integration takes a few minutes, but it’s fairly simple. In WinSCP, open the Preferences dialog and select the Integration / Applications heading on the left. Enter the path to your PuTTY installation (you need to install PuTTY separately), and set the other options the way you like them – the most useful one lets you pass the session password to PuTTY so you don’t need to enter it again. You can set it to launch a PuTTY session every time you connect to a server via SFTP, or you can just use the convenient button on the main toolbar to launch PuTTY when you need it. Either way, the seconds you save by not having to re-enter the login ID and password each time can add up in the long run.

One minor annoyance that I recently solved was setting the color preferences for PuTTY when launched from WinSCP – I knew how to do this with stand-alone PuTTY, but the integrated version always came up with a blue-on-black scheme that was a challenge for my aging eyes. To fix this, just use the normal PuTTY settings dialog (in PuTTY, right-click on the title bar and select Change Settings), but be sure to select “WinSCP temporary session” in the list of saved sessions, and save your changes to that.

WinSCP has a host of other features I haven’t even explored yet, including remote directory-synchronization functions and extensive scripting and command-line support. You can also generate Windows shortcuts to saved sessions, which could be stored with
your other project files to make your life still easier.

If you want to spend more time with your family and less time searching for passwords, I’d recommend giving WinSCP a try.

Ted Caldwell is an Application Developer and Senior Software Architect for GORGES. Ted's work encompasses all aspects of web application development, including requirements analysis, database and software design, programming, project management, and system administration. Ted focuses on delivering practical, cost-effective technology solutions for GORGES clients, using a variety of custom and off-the-shelf software tools and frameworks.
©2012 GORGES - All rights reserved
where programming meets design and lives happily ever after