DRYing Up CSS
DRYing Up CSS
Decluttering is critical because it feeds into the speed of writing new UI, changing look and feel in one fell swoop or just plain old refactoring.
Join the DZone community and get the full member experience.Join For Free
DRYing up CSS
Albert Einstein once said, "Out of clutter, find simplicity."
For the longest time, I did not understand what he was trying to convey, but then I started interpreting it by testing its applicability in aspects of my life. My interpretation has since been ‘de-duplicate and reuse.’ I have since been able to go lean to a good extent on many things I do. Be it all the stuff I have in my basement, in my closet, or my code.
As a UI developer, when I find myself writing/reading CSS code, I find Einstein’s advice holds water so well that every bone in my body wants to refactor the CSS. We all focus these days on choosing the best UI framework or how UX is so important. But when you get down to business, i.e. code, CSS is usually the neglected child. It is all over the place and it is not easy to refactor without guaranteeing styling regressions.
We deploy cool tools to minify it, optimize it, comb it, and all the other goodies. But I have observed in various projects that of all the core ideas behind CSS, reusability of styles is the most overlooked one. It is still code, and if we are repeating it, then like all other code that is not DRY. It is cluttered code and a liability. Discipline to keep CSS rules to as few as possible yet having them be reusable through an enterprise web app is often missing. And as developers, regardless of seniority and experience, we all make these mistakes in the process of getting stuff done ASAP or just exercise laziness in the name of a later-refactor-spike and leave duplicate CSS crumbs throughout the codebase.
Decluttering is critical because it feeds into the speed of writing a new UI, changing the look and feel in one fell swoop or just plain old refactoring. So here are some of the questions I like to ask (or provide answers to) with a team when CSS is involved:
Do We Have Preprocessors?
If you are not using LESS or SASS, kudos to you. You have a heart because these not only reduce the time it takes to write CSS but also coming up with DRY patterns that I am professing here. For the purpose of these thoughts, let me assume we are using LESS.
When Do I Start?
If you are starting a project or a web application, then it is a no brainer. But if you have decided to change tires while running the car, then a piecemeal, small chunk approach is best. Large scale changes are OK, but then in my travels, it has worked best when there is constant communication between the developer and the feedback loop with UX designers is small. Of course, there are tools like CSS Lint, where you start with whole CSS codebase and break into pieces. In any case, the simple agile approach is to find a page that has the largest number of style patterns, break them down just as if you were paper prototyping and come up with style buckets, which brings me to the next point…
Where Do I Put This?
Yes, a simple common sense approach of feature (or even page) based CSS files is prevalent these days. However, there need to be places like
control.lesswhich just by file naming convention itself tells a team member what goes where. For example, if there are constructs like panels as dictated by UX and they are going to be repeated throughout the UI, we need those in
panels.less. The first person to write does a harder job, yes, but then we all thank him/her by reusing and owning the DRY code as a team.
What Should I Name It?
This depends entirely on the team. While file names are easy, class naming often becomes a dragged out "topic" at work. SASS has great conventions on doing this. There's no reason why it cannot be applied to LESS or even vanilla CSS. BEM (block, element, and modifier) is a good approach as well, but you end up writing long class names. Class names can just be functional (what it does), presentational (what it looks like) or content-based (what it contains). Regardless of the convention but with a consistent approach, this can increase maintainability many fold, especially if you are in the business of making your own UI components just identified by classes. This also helps the team relate to the CSS codebase quicker as a living document rather than just a code artifact.
Is This Reusable?
This is the hardest thing for a team member to get used to. If you are implementing a new UI page and you are going to add a good chunk of styles, then it is great to be conscious of offering your work as reusable modular patterns. But it is not always the case. More so in fast-paced environments. This is where code reviews play a crucial role in enforcing reusability and identifying patterns. Pair programming CSS with a buddy is a luxury, but man does it pay dividends! I have benefited from that first hand.
Bottom line: Once a start is in place, then you just follow up in the code reviews until it becomes a culture.
How Do I Use It?
Ah! The $64,000 question! Well, if the good work has been communicated well, then the answer is just one link in a Slack message away. Check out the docs from the Ionic framework or Bootstrap. Personally, these are the epitomes of how usage of UI constructs should be communicated. The same approach can be taken for in-house CSS styles and patterns. The documentation does not have to be this fancy, but an internal wiki or GitHub wiki with screenshots coupled with HTML decorated with class names is a darn good start. If your team is not lucky enough to get a set of styling guidelines from an army if UX designers, then this wiki becomes one.
Lastly, another simple analogy that I would like to share is that CSS is no different than say, Java. Think of CSS way of doing a Java class with reusable utility methods… catch my drift?
Opinions expressed by DZone contributors are their own.