--- comments: true date: 2012-04-28 22:57:37 layout: post slug: the-single-responsibility-principle-applied-to-css title: The single responsibility principle applied to CSS wordpress_id: 3614 categories: - Web Development tag: - Abstraction - CSS - Front-end architecture - HTML - OOCSS --- Having just spoken at [the Front-Trends conference in Warsaw](http://2012.front-trends.com/), I’ve decided to expand on something which my talk mentioned a lot: classes. My talk covered how to build big, scalable front-ends and one of the key factors involved in doing so is sensible and generous use of abstracted classes. One thing that really helps you achieve this is the application of the [single responsibility principle](http://en.wikipedia.org/wiki/Single_responsibility_principle), a method used mainly in OO programming. Loosely, the single responsibility principle states that every module or chunk of code (a function etc) should do one job well and one job only. The benefits of this are mainly in the way of maintainability and extensibility. If we don’t adhere to the SRP then we are likely to end up with code which does more than it should, this means that altering one part of that code could negatively impact a seemingly unrelated part of the same snippet. It also makes our code a lot less flexible in that we find our code is trying to do too much; it is too specific in its job to be portable and reusable. Abstracting chunks of functionality into several responsibilities means we can reuse a lot more of our code and recombine it over and over with other similarly abstracted chunks. The [Wikipedia article](http://en.wikipedia.org/wiki/Single_responsibility_principle) makes a much better job of explaining than I can so I would recommend giving that a quick read (it’s only short). Also, you will find that a lot of us do this already, and perhaps without even realising. A class of `.wrapper` is a good example of the SRP at play; by having a single, reusable class whose sole job it is to group content we make our code a lot easier to work with. We don’t need to apply lots of widths to lots of elements; we use one extra `div` and delegate the width-constraining responsibility to that. We do this because it makes sense, but we don’t do it often enough; if we start actually thinking like this all the time we find we can vastly improve our code... The SRP is normally applied in programming circles but I have definitely found it is _incredibly_ useful when making lean, scalable front-ends. Here’s a quick example of un-abstracted code that **doesn’t** follow the SRP: Buy now! .promo { display: block; padding: 20px; margin-bottom: 20px; background-color: #09f; color: #fff; text-shadow: 0 0 1px rgba(0,0,0,0.25); border-radius: 4px; } Here we have a class for a promotional box of content. Here we are doing **two** things—we are defining box model and structure _and_ we are defining cosmetics (colouring etc). We can refactor this code to adhere to the SRP by splitting those two chunks of functionality into two classes: Buy now! .island { display: block; padding: 20px; margin-bottom: 20px; } .promo { background-color: #09f; color: #fff; text-shadow: 0 0 1px rgba(0,0,0,0.25); border-radius: 4px; } We now have two classes which each carry a single responsibility; `.island` boxes off content and `.promo` applies our promotional styling. This now means that we can do things like this, which previously we couldn’t: