Ninjas in your code at Drupalcon Portland: SASS with extends and placeholders
It’s fair to say that in the last year, adopting the CSS preprocessor SASS< has completely changed frontend development for me. That’s a sentiment I’ve heard others express when they started using it — and I was pretty late to the party.
I got attracted to it initially through variables. We’ve all been there when a client or a designer wants to change a color and suddenly we have to change dozens or hundreds of values across CSS.
Around the same time SASS came onto the scene, some thoughtful people were exploring more maintainable frontend development and CSS architecture through ideas/acronyms like OOCSS, SMACSS and BEM, and folks like Nicole Sullivan<, Jonathan Snook< (a Drupalcon featured speaker!<), Nicolas Gallagher<, and Harry Roberts<.
I spoke with Dale about SASS, object-oriented CSS and some the things he’ll be covering at Drupalcon. Be sure to join me at his session, “Sass: OO’S’CSS w/Extends and Silent Placeholders,” on Wednesday at 2:15 PM!<
IB: Some people argue SASS creates bloated code — do you see placeholders addressing that concern effectively?
DS: Sass doesn’t create bad code. Bad coders do.<
The whole concept of placeholder selectors< was designed to fight code bloat and be a more pragmatic solution to OOCSS. In my presentation, I illustrate how using the various techniques generate code.
The real “ah-ha” moment comes when we see how using placeholder selectors makes it easier to manage and literally re-use code — thus reducing dreaded CSS bloat.
IB: What’s the advantage to using silent placeholders over mixins for a set of rules?
DS: Simply put, being able to re-use code without duplicating code. The use of mixins was the first part of being DRY with our code<. Looking at the Sass it felt AWESOME! But when we looked at the output CSS, this is when we realized that we were all living a lie.
Our CSS rules were being duplicated tens, if not hundreds of times. We weren’t being DRY, we simply put the onus of duplication on the machine.
Placeholder selectors embrace the one of the oldest concepts of CSS and that is to extend the CSS selector for reuse. But with traditional CSS this was difficult to do, especially when you were dealing with thousands of lines of code. Sass’ @extend directive allows developers to create name-spaced reusable chunks of code that is portable, re-usable and extendable without duplicating anything.
IB: Do you see placeholders as helping to reduce the amount of tight coupling< — scattering pieces of styles in multiple places?
DS: While Placeholder Selectors are a tool that can help with scattered code, it is not the only solution. Having a file/folder structure that embraces the different types of code, e.g. CSS selectors, placeholder selectors, mixins and functions, can assist in creating reusable modulare code and maintain a process of control over the many parts.
Images: Screenshot from Dale Sande’s presentation.