Refactoring your CSS & XHTML/HTML

Posted by Sheldon Finlay on July 16, 2009

Refactoring is a concept that is often used in the context of programming. In fact, it’s one of the cornerstones of Test-Driven Development (TDD) in Ruby on Rails. Oddly, I rarely see refactoring discussed in the context of writing CSS and XHTML. Whether you write Ruby, PHP, Perl or CSS and HTML, refactoring your code should be a part of your coding workflow.

So what is refactoring? It’s basically cleaning up your code, making it more efficient and easier to maintain. When writing prose, you bang out a first draft to get your ideas down on refactoring-csspaper. It’s a bunch of crap often. Then you revisit your mess and make it beautiful and worthy of submitting to your local town paper.

Coding is no different. When writing XHTML and CSS I am often trying out things, adding extra divs and spans, and creating a mess of CSS and XHTML. It’s called the process. It’s messy. When I finally get to the look I wanted to achieve, I take a quick break and bask in the glory of having everything aligning and looking the way I had envisioned it to look. But I am not done yet. Far from it. I must refactor my code.

Refactoring code removes duplication, aids in readability and maintenance of the code in the future. The goal here is to make things simpler, while continuing to achieve the layout I have designed. If you refactor and suddenly something doesn’t look right, that is not refactoring, that is breaking something. You want to refactor in small steps and test your code in the browser to ensure things are still working as intended. You should be using some form of source control management (SCM) like Git and regularly checking your code changes in. This way if you botch something, you can always pull a working version out of your SCM.

Now you might be thinking to yourself, I write semantic markup, isn’t that refactoring? No, that’s called doing your job as a standards aware designer. You should always strive to create meaningful markup. That’s the name of the game. Refactoring is revisiting your code and identifying areas where your code can be improved, streamlined and made simpler. In six months or a year, when you revisit this code you’ll spend less time trying to remember how this code works because you took the time to make it simpler and easier to maintain.

Refactoring is a huge topic and I plan on addressing it using several examples in a series of posts on the topic. We are going to take some baby steps here. For now I will leave you with a simple refactoring of CSS.

I ended up writing this CSS code in a site I was working on today:

Do you notice something in this code? There is a huge amount of duplication here.  The height and the padding-top attributes are the same for each of these classes. This duplication violates the DRY principle: Don’t Repeat Yourself. Duplication is a very bad thing. If we want to change the height, we have to change it in four places. This is not ideal. A better approach would be to refactor this CSS and remove the duplication and place it into a group of parent classes:

So here I  have refactored out the duplication and placed it in a group of parent classes. Now if I want to change the height of padding of these elements, I just need to deal with making the change in one place. The readability has been improved along with the maintainability. And I have saved a a few lines of CSS. While not a huge amount in the small example, the LOC saving in a large CSS file can measure in the kilobytes!

Thats it for now. If there is one takeaway in this lesson, it is look for and eliminate duplication whenever possible. Stay tuned for more examples of CSS & XHTML refactoring.

ava7 Patterns

Posted by Sheldon Finlay on January 22, 2009

I remember when background patterns were everywhere on the web. Was sometime around 2004 or so. Thankfully, the trend moved on. But sometimes a paisley pattern is just what you need for a winning design. I stumbled upon ava7 Patterns and was quite impressed. They have over 980 patterns thus far and a beautiful interface for for finding the right color and pattern shape for your web site. Patterns can also be sorted by top rated, most downloaded and most commented, so you know which patterns have been over-implemented.

User Interface Tips

Posted by Sheldon Finlay on January 20, 2009

Here are a couple posts from Smashing Magazine regarding user interface design. A definitely must read for web designer and anyone working to design modern web application:

whitehouse.gov + web standards 2

Posted by Sheldon Finlay on January 20, 2009

It’s great to see the new whitehouse.gov web site embracing web standards. This is a trend I noticed with Obama. He hires the best people for the job and makes no exceptions when it comes to web design. His campaign web site was made with web standards, whereas the other top candidates sites looked pretty clunky behind the scenes. Web leadership starts at the top, and well, you can’t get much higher than the POTUS.

The new whitehouse.gov web site is validate xhtml. It takes advantage of jQuery for some smooth user-interface effects. It even has Atom 1.o feeds. I haven’t had a chance to look it over completely, but at first glance I am very impressed. Very nicely done, Mr. President.

An Introduction.

Posted by Sheldon Finlay on January 15, 2009

I built my first web page around 1995. I was a second-semester freshman at UMass Amherst and had just bought my first real computer: A Packard-Bell 100Mhz with 8MB of ram. I was the envy of many at the time, both in having Windows 95 and being able to navigate the servers at OIT. My first site was called MURG (named after a friend whom we affectionately called Grum), and was a collection of random, like so many web sites were around that time. I wish I still had a copy of it. The floppy I backed it up to seems to be corrupted and the WayBack Machine doesn’t go that far back. The site was hosted on the Umass OIT-UNIX server, which was my first experience with bash and shell work. Ah, these were the salad days when the web was green and new.

Now, I can trace my technical lineage back further to around the time I was in 5th or 6th grade. Like so many other geeks in my generation, my first computer introduction was a RadioShack TRS-80 and BASIC  programming. I used to stay up all night typing in lines and hoping the program would run. It was a lesson in zen-like detachment, as I had no way to save my work. I later learned how to play music in basic and do graphics rendering. My prowess in BASIC earned my an achievement award in junior high. A little later I got a computer that ran MS-DOS. But even still I used to boot disk to program in BASIC.

Various hand-me-down computers came into my life between junior high and my first real computer in college. I learned a lot about breaking things and getting them to work again. I was a brute force computer user and learned the most from my mistakes.

Today, I am a web designer, web developer, and a server administrator. A sort of interdisciplinary web geek if you will. Along the way I have worked for an ISP, published in computer magazines, started several companies and consulted for numerous others. I am a devoted Mac user as of 2007, having switched after getting into Ruby and Ruby on Rails (more on that later). Although, I still spend a lot of time each day on Windows still. I am very passionate about Ruby as a programming language and Ruby on Rails for application development. I still feel like I am in the honeymoon phase with Ruby on Rails and continue to be amazed by it. Rails has changed the everything about the way I work and how I approach problems. It has been the biggest influence in my professional life apart from the web itself.

I am also a father to Erin and a husband to Amy. My wife runs KnittingHelp.com, one of the most popular knitting sites on the web. I serve as the designer, developer and host and she is the talent. When I am not on a computer I like to do strength training,  eat good food, enjoy beer and wine, take photos, and just enjoy the world around me. I am also a sacred harp singer.

This blog is powered by WordPress. I had a hard time deciding between using a Ruby on Rails blog engine (Mephisto or Typo) or sticking with a PHP blog. Ultimately I choose WordPress, but I still have a pang of regret for not using a Rails blog engine.

On this blog I hope to share some of the things I am passionate about and give back a little of what I have learned over the last decade or so of work with the web.