The following post is written by David Tsang, one of the designers here at Elcom.
Intended Audience: Designers and implementers
Dry stands for "Don't Repeat Yourself" as opposed to it's polar opposite WET (Write Everything Twice)
What is dry code?
Dry code is an approach to CSS coding that minimises repetition of styling for commonly used styles.
Why write dry code?
The aim of dry code is to achieve:
- Smaller file sizes – so that visitors have quicker site load times
- Easier maintenance - efficient workflow of CSS so you're not repeating yourself.
When writing DRY code the aim is to apply it for styles that are likely to be constantly changing. There's minimal value in doing it for styles defined once or twice – it'd be more work than it’s worth and defeats the DRY approach purpose.
Here's an example:
A typical non-DRY approach would be to do this if we wanted to add a border-left
Here's a DRY'er approach to this purely using fundamental CSS:
With this example there is less repeated code, if we wanted to add a new border property you would simply just have to define the colour.
This can be scaled globally if you wanted to reuse the attributes for e.g:
Here we're simply defining the border colour each time rather and chaining the CSS to repeat the code.
As you can see the potential using DRY and it's a fun approach in coding CSS the way it should be!