When or If
meyerweb.com
meyerweb.com
@perchance supports(display: grid) as-well-as media(min-width: 33em),
@why-then-of-course:
{
}
@on-the-other-hand
{
}
Seems obvious, really. @perchance ...
@thusly {}
@contrarily {}
:)It took me a while to learn that writing is not just putting words together and filling the page, but actually to tell something, to convey an argument. That meant learning to get rid of all unnecessary conjunctions and keeping it simple and clear.
My teachers all knew whatever boring book/topic backwards as we'd all just spent weeks discussing them in class. Now go write an essay!
Knowing my audience, I skipped the details, got the concepts right and put it down in a page or less because I know my teacher knows the details so I don't need to rewrite them.
Consequently, I didn't do well in English class.
sass stepped on css' toes by reusing the @xxx syntax from css instead of creating their own syntax.
Yes and no - they're accepting that there may be future compatibility breaks (like this very case) but using some arbitrary other glyph as a marker is also dangerous. Whenever you're working on a second layer tool you're limited in how much future compatibility you can guarantee (and the limit is 0).
SASS code is written in the SASS language, with CSS as a back-end; the only thing that can break a piece of existing SASS code (in terms of making it either fail to generate, or generate different CSS) is a backwards-incompatible upgrade to the SASS processor. For instance, someone has SASS code which relies on certain @-things to be passed through to CSS, but an upgrade to SASS claims some of them for itself.
(A change in CSS could break the generated CSS code, which is a different issue.)
I think most of the trouble is actually that Sass accepted the theoretical compatibility breaks on behalf of their users, who didn’t realise they were signing up for this, and end up discouraged when breakages happen and they have to resort to ever weirder and wackier workarounds that should work but somehow break things in some other part of their toolchain, et cetera ad desperationem.
I think it would have been much better if every piece of Sass syntax had used been clearly denoted by the use of the $ symbol ($@ instead of @ for its at-rules, a $ prefix on its functions, &c.), except probably for nesting (though not bare mathematical expressions, I’d force them to be wrapped in $(…)—they’re the worst to find when trying to port away from Sass syntactically rather than compilationally). $ was and is not used by CSS and could probably be agreeably reserved for such tools, and even if not, the unquestionable superiority for tooling (that it makes identifying Sass rather than CSS parts of the stylesheet textually trivial rather than absolutely requiring a compiler that is unlikely to be designed for general tooling use) would even make switching from $ to something else (e.g. a -sass- prefix, which would also be a reasonable choice, though slightly more verbose) genuinely easy.
But of course, that ship has long sailed, and I have no time machine.
I find it ironic that the css "when/else" draft spec uses "if" for describing the conditional scenarios in plain english. As if the spec itself admits the syntax they are choosing is clearly sub-optimal.
https://developers.google.com/web/updates/2018/03/smooshgate
“When” actually seems better in a purely declarative language.
I don't really have a strong opinion on the when vs. if fight w.r.t. yielding support to SASS but I think that "if" is just a better keyword to use in this instance based on language usage alone.
By contrast, in CSS, if the condition later becomes true, the declarations in it do start to apply. So I also think "when" (in the sense of "whenever") makes more sense than "if".
I assumed you were referring to programming paradigms. "Procedural" is part of the "imperative" paradigm [1]. "declarative" is different from "imperative" [1]. SQL is, for the most part, "declarative" [2].
> If it executes multiple times that's because it's part of a construct that runs everything inside it multiple times.
How is that different from CSS? Aren't CSS rules also part of some larger construct and they get run multiple times (i.e. once the context changes)?
In this case I think it's on sass to introduce the awkward workaround on the syntax clash.
"When" usually describes an event that is likely to happen in some future time. "If" describes something that may or may not be true.
@case {
supports(display: grid) and media(min-width: 33em) {
}
otherthing(arg: whatever) {
}
otherwise {
}
}
You're welcome.I got it down to one @ sygil to write a long conditional ladder with any number of conditions and a default.
It could be called @cond, in the Lisp style; not a lot of people know that though.
The first condition that is true fires; the rest are then ignored, so the order of the cases matters. Many languages call that sort of thing using "case" terminology.
Probably a diagnostic in the browser console is worthwhile if the always true "otherwise" condition is followed by anything.
"We use if to introduce a possible or unreal situation or condition. We use when to refer to the time of a future situation or condition that we are certain of."[1]
Could you really replace the if as used in a programming language context with when?
To me, when doesn't make sense in that context at all, but again, English is not my first language.
[1] https://dictionary.cambridge.org/grammar/british-grammar/if-...
When you run it with "this" value, it'll do "this". When you run it with "that" value, it'll do "that". Because, eventually, all code paths will be run.
It would also be semantically valid to say: When you execute this program/method, if the value is "this", do "this".
At the end of the day, English rules are more what we like to call guidelines.
> At the end of the day, English rules are more what we like to call guidelines.
Tell that to my English teacher;-)
I was about to disagree with you, that we use "when" when several things might be true, but then I realised that in those cases each of the possibilities will happen, but at different times, like "when the light is read you stop, when it is green you go".
It's fascinating how many quite subtle rules you internalise about your native language without realising it.
Developers will stop arguing about arbitrary syntactical nuances when pigs fly!
when light_is_green {
go
} when light_is_red {
stop
} otherwise {
go_very_fast
}
:)Will the two "if" implementations have dramatic differences in how they act?
The main one is that Sass is, basically, a "code generator". It's a sort of template language that generates CSS code. And so, a "Sass-if" works at code generation time, that is, when you execute the Sass command:
sass-if (some logical condition) {
<some CSS code>
} else {
<some other CSS code>
}
Depending on the condition and at compile time, sass would spit out the first block of code or the second. The condition is, basically, a calculated expression that applies to values and variables available at compile time.Then -after deployment and when a client visits your website- that CSS code is served to the client. Clients are varied and so CSS wants to have its own if structure that applies at runtime. The conditions generally would check the current runtime conditions (i.e. is this running on a large screen? is this browser in vertical orientation? etc)
css-if (some condition pertaining this specific current browser) {
<some CSS code>
} else {
<some other CSS code>
}
So, yes, they are clearly and necessarily different, both in scope and usage. And so you'd generally need both if because they do different things. And that where the problem arises, because if you want to have both, their syntaxes need to be distinguishable.One thing I don't understand is why not: 1) change Sass implementation to support "sass-if" and backport these changes to some older versions if necessary 2) upgrade the Sass's version and do a mass replace which sounds kind-of simple 3) use CSS's if happily
It's not completely "kind-of simple" though, as there is, potentially, a fair amount of code written with Sass in currently running projects. It is a cost to consider.
Also, there have been some precedents in the JavaScript community, mainly with Mootools -an old library, mostly irrelevant today but with some existing code still running in the wild- and the name of a couple of methods such as includes and flatten. The thing is that TC39 really pushes for "not breaking anything at all", and by proximity the CSS community seems to have some people with a similar opinion.
Anyone remember the method name?
"If you're product exists to correct a deficiency in some other product, don't be surprised when that product makes your product native functionality."
If CSS chooses "if", it's up to Sass to deal with that. What Sass uses shouldn't be a concern for CSS.
It doesn't seem like it is considered often.
For example: searching for "js ..." doesn't product results for spread operator or destructuring assignment. You would have to ask someone what is going on and the answer would have to not confuse the 2 eventho they share syntax.
"CSS if/else" obviously does produce many search results and for "CSS when" I get only 3. It seems everyone already knows what they are talking about. This is how we build human languages.
Personally I would prefer it if CSS was frozen and a new style type was introduced to contain all the heretical Turing-equivalency A style sheet should probably never have without disappointing those who really really want it.
Then we could say: You shall not load this x-domain.
I guess nobody in the working group uses an editor with tab completion? Unless you're going to be writing CSS on a whiteboard, this seems like a non-issue to me.
if ...:
# ...
elif ...:
# ...
else:
# ...In those languages, without something like `elif`, you get an endless march to the right when you chain a series of ifs:
if c1 then
s1
else
if c2 then
s2
else
if c3 then
s3
else
if c4 then
s4
else
s5
end
end
end
end
C doesn't need this because it only allows a single statement for the then and else branches, and `{ ... }` is a statement. Therefore, you can chain its directly by having the else statement be an if statement. if_stmt <- "if" expr INDENT stmts DEDENT ("else" "if" expr INDENT stmts DEDENT)* ("else" INDENT stmts DEDENT)?
It does require one-token lookahead, but it's unambiguous.We're specifically talking about the subset of languages where these blocks _don't_ have their own delimiters[3].
1. https://en.wikipedia.org/w/index.php?title=Python_syntax_and...
2. https://en.wikipedia.org/w/index.php?title=Python_(programmi...
3. There is a reading of Bob's original comment such that it's suggesting that Python and end-if languages are among languages without them (or something—I can't make total sense of that part of the comment; it gets kind of garden path-y), but they're not.
if c1 then:
s1
else if c2:
s2
s3
Without significant indentation, it's not possible to tell if "s3" is indented to be part of the the second if's then branch.In other words, "else if" and "else INDENT if" are unambiguously different in Python.
That's not true in other languages that use "elif"/"elsif"/"elseif" where only newlines are significant and not indentation.
if c1 then
s1
else
if c2 then
s2
else
s3
end
end // <-- Two "end"s
Versus: if c1 then
s1
else if c2 then
s2
end // <-- Only one "end"
If you don't make the newline there significant, then every if statement at the beginning of an else clause would be interpreted as chained: if c1 then
s1
else
if c2 then
s2
end
s3
end
Here, the parser will incorrectly treat the first "end" as closing the outer "if" when it's intended to close the inner one. It will then think "s3" is outside of the "else" clause and then consider the second "end" to be a parse error.In principle, this isn't actually ambiguous and if the parser had unbounded lookahead it could probably find the second "end" and rollback and figure out what's going on. In practice, languages with unbounded lookahead are hard for humans to parse too.
Having an explicit "elif" token avoids all of this weirdness.
Thanks for Dart, absolutely lovely language.
> We had our safety briefing last night – all pretty obvious advice like "don't fall in the sea, it's cold". I wrote that one down.
> Barbara, our second skipper, is Dutch so the word for "if" is the same as "when". "When the boat catches fire" was not exactly reassuring and "when we turn over in a big wave" even less so.
Or do a 180 and go the C ternary operator route. ;)
If you're talking about code - sometimes it's nice to have extra-defensive code even if it's mostly inconceivable it will ever be triggered (especially if this will be used as a library for a lot of folks) but mostly you wouldn't.
Isn't it abandoned?
What's also fascinating (or, rather, horrifying) is how bad the whole thing is.
SASS is 15 years this year. Its concepts have proven their worth over, and over, and over again (and reimplemented several times in Less, PostCSS plugins etc.). W3C's own design system uses SASS [1]. How much of that did we get to see in CSS in the past 15 years? Maybe, just maybe we'll finally get nesting some time in the next 10 years or so.
Conditionals? Hahaha. If you're betting on getting them sometime before 2045, you're a fool.
The same thing repeats all across other tech in browsers. Detecting ambient light in browsers? Oh, w3c got you fam [2]. Providing usable styleable common components? Ahahahaha, even w3c relies on third-party implementations [3].
Most of the people on these committees have never developed a website in their life. Those that did only maybe implemented a blog or two a few decades ago. And the rest of browser implementors? Oh, they've never touched anything beyond C/C++ code in browser engines. And trying to get people who actually do this for a living there? It's a rather tough endeavor [4]
So, I wouldn't hold my breath for a decision on conditionals. Or for conditionals in general.
[1] https://design-system.w3.org
[2] https://www.w3.org/TR/ambient-light/
[3] https://design-system.w3.org/third-party-plugins/
[5] See "Solutions https://www.quirksmode.org/blog/archives/2021/08/breaking_th...