When or If
Published 2 years, 8 months pastThe CSSWG (CSS Working Group) is currently debating what to name a conditional structure, and it’s kind of fascinating. There are a lot of strong opinions, and I’m not sure how many of them are weakly held.
Boiled down to the bare bones, the idea is to take the conditional structures CSS already has, like @supports
and @media
, and allow more generic conditionals that combine and enhance what those structures make possible. To pick a basic example, this:
@supports (display: grid) {
@media (min-width: 33em) {
…
}
}
…would become something like this:
@conditional supports(display: grid) and media(min-width: 33em) {
…
}
This would also be extended to allow for alternates, something like:
@conditional supports(display: grid) and media(min-width: 33em) {
…
} @otherwise {
…
}
Except nobody wants to have to type @conditional
and @otherwise
, so the WG went in search of shorter names.
The Sass-savvy among you are probably jumping up and down right now, shouting “We have that! We have that already! Just call them @if
and @else
and finally get on our level!” And yes, you do have that already: Sass uses exactly those keywords. There are some minor syntactic differences (Sass doesn’t require parentheses around the conditional tests, for example) and it’s not clear whether CSS would allow testing of variable values the way Sass does, but they’re very similar.
And that’s a problem, because if CSS starts using @if
and @else
, there is the potential for syntactic train wrecks. If you’re writing with Sass, how will it tell the difference between its @if
and the CSS @if
? Will you be forever barred from using CSS conditionals in Sass, if that’s what goes into CSS? Or will Sass be forced to rename those conditionals to something else, in order to avoid clashing — and if so, how much upheaval will that create for Sass authors?
The current proposal, as I write this, is to use @when
and @else
in CSS Actual. Thus, something like:
@when supports(display: grid) and media(min-width: 33em) {
…
} @else {
…
}
Even though there is overlap with @else
, apparently starting the overall structure with @when
would allow Sass to tell the difference. So that would sidestep clashing with Sass.
But should the CSS WG even care that a third-party code base’s syntax gets trampled on by CSS syntax? I imagine Sass authors would say, “Uh, hell yeah they should”, but does that outweigh the potential learning hurdle of all the non-Sass authors, both now and over the next few decades, learning that @when
doesn’t actually have temporal meaning and is just an alias for the more recognizable if
statement?
Because while it’s true that some programming languages have a when
conditional structure (kOS being the one I’ve used most recently), they usually also have an if
structure, and the two sometimes mean different things. There is a view held by some that using the label when
when we really mean if
is a mistake, one that will stand out as a weird choice and a design blunder, 10 years hence, and will create a cognitive snag in the process of learning CSS. Others hold the view that when
is a relatively common programming term, it’s sometimes synonymous with if
, every language has quirks that new learners need to learn, and it’s worth avoiding a clash with tools and authors that already exist.
If you ask me, both views are true, and that’s the real problem. I imagine most of the participants in the discussion, even if their strong opinions are strongly held, can at least see where the other view is rooted, and sympathize with it. And it’s very likely the case that even if Sass and other tools didn’t exist, the WG would still be having the same debate, because both terms work in context. I suspect if
would have won by now, but who knows? Maybe not. There have been longer debates over less fundamental concepts over the years.
A lot of my professional life has been spent explaining CSS to people new to it, so that may be why I personally lean toward @if
over @when
. It’s a bit easier to explain, it looks more familiar to anyone who’s done programming at just about any level, and semantically it makes a bit more sense to me. It’s also true that I come from a place of not having to worry about Sass changing on me, because I’ve basically never used it (or any other CSS pre-processor, for that matter) and I don’t have to do the heavy lifting of rewriting Sass to deal with this. So, easy for me to say!
That said, I have an instinctive distrust of arguments by majority. Yes, the number of Sass developers who’d have to adapt Sass to @if
in CSS Actual is vanishingly small compared to the population of current and future CSS authors, and the number of Sass authors is likely much smaller than the number of total CSS authors. That doesn’t automatically mean they should be discounted. It’s good to keep CSS as future-proof as possible, but it should also be kept as present-proof as possible.
The rub comes in with “as possible”, though. This isn’t a situation where all things are possible. Something’s going to give, and there will be a group of people ill-served by the result. Will it be Sass authors? Future CSS learners? Another group? Everyone? We’ll see!