by thebouv · 11y ago · 0 comments
Nested loops can be bad. Nesting logic deeply in other languages is bad.
Nesting style rules that belong together, well, together isn't nesting as much as it is putting like stuff together.
I may create section.myawesomesection {...} and everything to do with the stuff in there is in there, not leaking out. I don't accidentally use a class name used somewhere else and globally change it, I don't change all global UL tags to be flat, inline, no list-style. I change them in context of section.myawesomesection.
Not to mention allowing me to put my breakpoints together in context (particularly useful with a mixin like breakpoint(small) or something).
Yes you can really go nuts with the nesting and create a giant pile of crap. But then that's you being a bad planner (not even going to say coder here, as CSS/SASS is more about planning than coding).
Even with the specifity I vastly prefer nesting to a bunch of files with flat selectors and a huge amount of globals where you need a lot of time just to understand which elements affect which.
The post is quite opinionated and puts every issue in a bucket, as if there is only one way to write CSS. A few examples would have been useful.
Not using Nesting is the worst offender in the list.
A recommendation for that project was to lint for some ground rules forbidding certain behaviours, nesting being one of them. I've actually been considering avoiding SASS altogether in the future.
a) Performance (rule matching gets significantly harder as you nest);
b) Maintainability (did a change in the mark-up? Then check the CSS to see if it still matches);
c) Code reuse (obviously).
Using a nomenclature-based solution such as BEM, in the other hand, would avoid all these issues (complex, slow and over-specific selectors; DOM dependence; ease of reuse) while still avoiding style leaks. I'd argue BEM is even better at avoiding leaks: if you change all your global <ul> inside your section.myawesomesection, then it will - eventually, sooner or later - leak to a <ul> which you didn't want to style.
Could I have just given a class to the h2? Yep. But performance difference between h2.myclass{..} vs section.awesomesection h2{..}? Worth worrying over?
Of course, if there really is a difference, it would add up I suppose over a mega-site. So thanks for the food for thought. I'll have to weigh it versus developer happiness I get from the tidy style rules.
b) Well, if the markup changes, that can just as well change your CSS too.
At some point, some change in the markup, you're going to reexamine the CSS for it. It's not like you write the perfect CSS and walk away because it will always work with the document and all its iterations.
Of course, like you said, maintainability is an issue but it isn't exclusive to SASS or my use of nesting via section.myawesomesection either.
c) Styles that are in many places get noted and placed so they can be reused. But that one special snowflake of a rule that applies only to this section of content because my designer wants to especially call it out? The rule can be specific.
I'm not creating 200 line section.myawesomesection definitions. Reusable items are resuable. Specific items are specific (most often to overwrite a global style say like font, color, this specific absolutely positioned icon, etc). Generally a dozen lines or so. If longer, just like a function getting too long, and I rethink/rewrite.
d) I absolutely 100% will check out BEM. I'm no slave to SASS, nesting, or anything like that. I have no idea about BEM will happily switch if it is better.
It roughly means that you imply structure via naming conventions instead of nesting (e.g. if you have a box inside a block, you'd style it via single class such as .my-block__my-box, instead of the traditional nesting approach .my-block .my-box). You can - and should - still use SASS (or other preprocessor) with BEM.
Some interesting resources regarding this:
http://philipwalton.com/articles/css-architecture/
http://philipwalton.com/articles/side-effects-in-css/
http://philipwalton.com/articles/extending-styles/
http://benfrain.com/the-ten-commandments-of-sane-style-sheet...
What you basically accomplished here is to transfer the problem of complexity from one domain (CSS) to another (HTML) not solving it.
Even if Sass provides a visual way to emulate hierarchy by using the & operator and breaking up the name at the significant points for instance:
.myblock
&-mybox
color: black
It would be still a mess on the HTML side and there isn't really an equivalent workaround for it with tools like Jade or others.In the end, if the penalty of using a shallow nesting architecture is to increase the specificity score by 1-2 points, I'm willing to take it in exchange of a more readable and maintainable codebase whether in CSS or HTML domain.