Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Jonathan Fernandez
Jonathan Fernandez
7,542 Points

Dry Code - Question?

Hi everyone,

I have a question regarding DRY practice and just wanted a second opinion. When writing my CSS document, I want to keep it's document flow organized by separating the selectors of nav, content, & footer. Although the code I'm working with will have me repeat my display value 3 times if I organize my code in this way.

Is it better to combine all selectors like so:

.navbar, #brandName, nav, #selection,  .content, .footer {
    display: flex;
}

Or is it ok to have repeated myself as I am doing now since the selectors are in different sections of the document?

/* navbar */
.navbar, #brandName, nav, #selection {
    display: flex;
}

/* content */
 .content {
     display: flex;
}

/* footer */
.footer {
    display: flex;
}

I feel the latter makes it easier to read and manage the code. Any opinions will be greatly appreciated.

Best, Jonathan

1 Answer

Steven Parker
Steven Parker
229,744 Points

I would not consider this a case of "repeating yourself". Particularly if there is any chance at all that you might want to apply other properties to the individual classes/IDs/elements that they will not share.

In most cases, the practice of D.R.Y. will make things easier to read. Those are the times to embrace it. When it conflicts with readability, it's either a good time to make an exception or it's time to reconsider the whole approach.

And I agree it's much clearer to organize properties by selector than to organize selectors by shared properties. The latter could easily be an obfuscation tactic.

Jonathan Fernandez
Jonathan Fernandez
7,542 Points

Thanks for the detail feedback and opinion. It's good to know that it's is ok to code this way. : )