243 points · by molecule · 10y ago · 96 comments
Slightly off topic, I'm slightly bothered by the amount of trash talk CSS receives since it's pretty much the best language we've ever had to define interface styles, and it's only getting better. Maybe some of the rules about behavior are arguable, but by and large I've never met a design I couldn't get to work with some well-defined CSS.
And if you can't read nested CSS I can't imagine how you'd feel in the face of some heavy XML.
XML was initially thought of as a simpler SGML, but when it took off it started to be used for all kinds of data including data which are not based on human-readable text at all. XML is fine for markup, but needlessly verbose and complex for non-markup languages.
Here is a real world example of what i am talking about, from a code base i currently work with, and don't dare touch: http://i.imgur.com/fMlvoRS.png
The idea of maintaining legacy sass with heavy embedding in years to come makes me shudder.
That one you mention is a huge problem. Hence the documentation states "Don't nest more than 3 layers deep".
Most sites don't need variables and nestings of CSS.
I would rather see duplicate class names like this in CSS.
.wrapper1 .header {}
.wrapper1 .header h2 {}
.wrapper1 .body {}
.wrapper1 .body .article {}
.wrapper1 .body .article .blurb {}
.wrapper1 .body .article .quote {}
.wrapper1 .footer {}
Repetition doesn't bother me. In fact, it is surprisingly helpful.
It's not the nesting in the SASS that's the problem per se, its the fact that it makes it easy to create the kind of code that you list that's the problem.
Modern good practice suggests a single classname for each item, so you're not fighting with specificity too much.
The original "bad" example doesn't even do that, its just using :before and :after etc. not nested selectors.
Alternative is to create descriptive, but potentially very lengthy class names that is single level.
if wrapper and header:
...
if wrapper and body:
...
to if wrapper:
if header:
...
if body:
...
when coding?Consider the case below. If I want to understand what conditions are necessary for body, that code is now offscreen. I generally consider overly-nested conditions as code smell.
if wrapper:
... 30 LOC ...
if header:
... 30 LOC ...
if body:
... 30 LOC ... Two If statements, no nesting
vs.
Three If statements, 1 level deep nesting
To me, the first option is clearly simpler and more readable. Then again, looking at the second option I notice immediately that I can dismiss it if wrapper happens to be false. But it really depends on what the other code lines contain.Also, it does force you to write CSS more efficiently because if you work with pre-processor language, you often don't see the bloat.
if wrapper:
do_thing_to_wrapper(wrapper)
def do_thing_to_wrapper(wrapper):
...Every modern CSS codebase attempts to use 1 location agnostic class per element. Usually when warning against 3 levels of nesting in preprocessors it is to maintain readability on pseudo selectors but not an encouragement to nest classes.
Maybe context.
What that guy posted is fine (only if you have SOME sort of rules around nesting - our team has been getting along great ever since using rscss.io methodology)
Doesn’t firefox even support in-browser viewing and editing of SASS?
And I think the benefit of the ease-of-writing is minimal (especially if you're using something like BEM where you're trying to avoid cascading anyway), whereas the drawbacks in terms of understandability are super duper high.
I think it's always ok to use them for pseud-oclasses and I think in general if you've nested something more than 2 levels deep you probably have a sign you might need to refactor something.
No right or wrong answer here, just genuinely curious.
.myclass { ...30 loc ... } /* .myclass */
I really don't see this enough.
In the future every new language should come with a "go fmt" equivalent.
I originally didn't like braces in go, but then I realized there's just enough in there to make auto-formatting easy.
Yes, you can criticize the nested classes and inconsistencies of brace placement. (And you should).
But I love seeing things coming out of 18F. I think this sort of open gov computing projects are blessed.
Even if you're not 100% open source but your development is "in the open", that's a huge step towards a better service to citizens.
* CIO: https://playbook.cio.gov/designstandards/
* CFPB: https://cfpb.github.io/design-manual/
* GOV.UK: http://govuk-elements.herokuapp.com/
Hope to see more open gov open source projects in the future.
.accordion
.accordion-item
.accordion-item-selected
.nav_bar
.nav_bar-link
.nav_bar-link-clicked
Though its the most ugly, I'm a fan of the original B__E--MI feel this is something people need to learn to look past - because I agree entirely. It looks terrible. Absolutely terrible. Names get long (but yay for autocompleters!) - but everything is readable. CSS shouldn't be aesthetic. It should be legible. Maintaining something you can't read is hell - every programmer knows this.
I also make use of prefixes to namespace. Which makes my classes even longer. [0]
But there is absolutely no question what my code is doing when I read the HTML. I know exactly where changes need to be made. My coworkers are familiar enough with my style that they know what code is safe to change and what code will have side effects. They're slowly adopting the style - although it's almost a 180 from the old style of "be as specific as possible so you don't accidentally break something someone else had worked on 8 months ago". (#NotEvenKidding .our .selectors .are > .like .this)
[0] http://csswizardry.com/2015/03/more-transparent-ui-code-with...
You might be able to enforce a distinction between elements and modifiers (with elements nested below parents, while modifiers are siblings of the elements they modify) like so:
.accordion {
.accordion-item { }
.accordion-item-selected { }
}
But this requires nesting where true BEM otherwise avoids it.*I'm an 18F employee.
18F specifically does not recommend using Bootstrap for production work because of one,
the difficulty in adapting its opinionated styles to bespoke design work and two,
its CSS style places semantic layout instructions directly in HTML classes.
https://pages.18f.gov/frontend/css-coding-styleguide/framewo...[1] https://github.com/paypal/bootstrap-accessibility-plugin
Personally I always opt for the latter, since I find it heavily reduces the amount of syntactic noise, which is a personal bugbear of mine. is the main reason for using SCSS the familiarity, or the fact it's a strict superset of CSS, or just that people generally dislike whitespace sensitivity? I can see the benefits, but it would be interesting to know if there's another angle I haven't considered.
"Use soft-tabs with a two space indent."[0]
Ugh, I work in Gov (DOD) and we recently switched to two spaces (I'm a Java/PhP dev and have always used 4 spaces) and was hoping to use this to support a switch back, guess that's a no go...
[0] - https://pages.18f.gov/frontend/css-coding-styleguide/format/
* Caution, rant below *
I also get really annoyed when people introduce SCSS/SASS/LESS in introductory texts. Confuse a beginner right out of the gate with these template languages that were made to work around CSS, which works perfectly fine, but is just slightly inconvenient because it doesn't offer near-programming-language levels of sugar/complexity.
/rant
Do note this practice may be discouraged as HTTP2 gains wider adoption.
Concatenation yes, minifying is always a good thing though.
What the new standard approach will be I'm not sure.
"… you cannot work at 18F on your position description for more than four years … We believe that in this industry most people aren't going to stick around for four years and beyond. They will join 18F, do their years of service in the federal government, and then return to the private (or public) sector. Due to the high-pressure nature of our work, it makes sense that people will move on after a couple of years. That helps 18F stay fresh and strong."
What the frack?!? Isn't the whole point of working for the Federal Government to have a job for life? 18F is actually worse than (non-startup) private sector jobs in that your contract says you cannot stay in the job longer than 2 (or 4) years. Fascinating.
No, though certainly the opportunity for a long-career with a stable employer is part of the attraction for some people of some federal positions.
OTOH, even term-limited positions like those at 18F don't really conflict with this, they just mean you need to find a different federal position before the term ends, leveraging your 18F experience.
> 18F is actually worse than (non-startup) private sector jobs in that your contract says you cannot stay in the job longer than 2 (or 4) years.
Limited-term-contract, non-startup, private sector jobs where you need to line up a new gig before the contract expires to keep working are not exactly uncommon, so I'm not sure how you characterize 18F as worse than non-startup private sector jobs based on a feature it shares with many non-startup, private sector jobs.
Just seems like there is a mismatch between 18F's (high) cool factor and the compensation structure. These pay rates do not strike me as "contracting" or "consulting" rates. They strike me as more in line with "full time employee" pay rates at ordinary private-sector (non-startup) companies. But the job term is capped like a contract job. I don't see the financial upside to match their statement that https://pages.18f.gov/joining-18f/pay-grades/:
"Due to the high-pressure nature of our work, it makes sense that people will move on after a couple of years."
Sounds all startupy without the startup upside.
I work at 18F, but am speaking for myself here. Pay rates and length of service are both tied to existing rules for federal employment. The advantage of being part of the government (and thereby following these rules) is in the possible impact of our work, like working with the FEC to build a new website with intermediate releases: https://beta.fec.gov/