Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Class names and other selectors are more like a query language, because they don't do anything if you don't have an html element with that class or matching the selector.

on edit: a downvote on that, really. because I don't think class names are anything like a variable!





One of the main issues with global variables is their unlimited scope, which means any part of the program can modify them. That's what he's saying.

Give your element a class. Now show me the CSS that styles it. Good luck because it can be literally anywhere.


Not really. They're like a variable which is mutated in all kinds of places, without notice or structure.

Absolutely the worst kind of variable.

One very obvious example is browser defaults.

You literally have no idea what they are. You can override them, but not until you find a list so you know what to override.

Did you know the default browser margin for body is 8px?

Insane.


This doesn't make CSS classes mutable global state. CSS rules are applied according to specificity of selectors. You could maybe say that the overall style of a page is mutable state, but not the CSS classes themselves. A CSS class is assigned and that's it. Unless JS is involved, it also doesn't disappear or toggle.

I'm aware of browser defaults and how they work, yes. Just because someone does not agree with you does not make them uneducated.

Frankly I tend to find analogies not very useful, I don't think my analogy of CSS as a db is very good, but as bad as it is I feel it makes some sense as opposed to talk about classes as if they were global mutable variables.

I will take my analogy slightly farther.

I will say the browser ships with a DB system called CSS, and a toy DB called BrowserDefaults implemented in CSS, the same way MS Access used to ship with Northwind. The especially sucky thing about this system is that when you want to create your own DB you have to extend BrowserDefaults. This is why a lot of specific DBs have what are called resets that basically delete all of the BrowserDefaults setup so it is not messing with your DB.

The browser also ships with two languages with the ability to query CSS, one is JavaScript and that is a pretty straightforward language it can read and write to CSS using an API called the CSSOM. The other query language is a tree based query language called HTML (actually there is also another tree based language called XML in there but nobody talks about that anymore, let's just say it works almost the same as HTML only when you use it, it automatically clears all the data out of the base BrowserDefaults DB)

(We can see what I mean about analogies suck right about here)

This tree based query language is crazy as shit! Because it is not just a query language but also has some weird transformational capabilities.

And also the thing that is weird is that the HTML query language can be changed by the JavaScript language!

Let's look at an example

Let's suppose we have a tree structure like this

<div class="one"></div>

<div class="two"></div>

and the JavaScript holds a tree fragment <span class="myspan">text</span>

and the CSS says

.myspan { color: green; } .one span { color: red }

.two span { color: black }

the JavaScript reading that fragment css properties will see that "color" = "green"

but placing it inside of the parts of the HTML tree gives different values for the color property. Some people will mistakenly claim at this point that the is mutation of the global state but that is obviously incorrect. The CSS state has not been mutated at all, what the HTML query has done essentially is run a transformation and output an object, the CSS state remains unchanged because JavaScript can use the CSSOM to query what the data in the myspan class is.

JavaScript can mutate the value of the CSS classes, but HTML cannot. HTML essentially copies all the classes, runs a transformation, and outputs the result.

This is actually a pretty interesting situation, I don't feel that there is any similar architecture anywhere. I don't feel it really fits with DSSL or Latex (because of how JavaScript works with HTML)

Now although I don't feel that I have ever really had the problems that other people of undoubtedly superior programming ability seem to have with this, I can see how this unique and somewhat accidentally arrived at architecture can be irritating to people.

Note - when I say I don't have any problems I mean sure, I can create bugs with this, I can find bugs impacting me with this, but I've never really had long running hard to resolve bugs due to CSS/HTML. Bugs that are generally hard to resolve I find are in the JS stack.

Going back to the browser defaults thing - again not really a good example of either of our analogies, more an example of how every programming language has its idiosyncratic and stupid things you just have to learn about it and keep in mind or they will bite you on the ass.

on edit: obviously I am not making any claims as to internal implementations in browsers and how they do it, I am just discussing the external experience of these languages working together and how to think about them.


I'll just note here that there are some obvious points about CSS custom properties here that might seem more like mutating state, although there it is not global state being mutated, but I can't really address all that here, as it is midnight and I need to get up early.

also let us suppose I have the following

.one span {color: red}

.one span {color: purple}

inside of a CSS file.

Again I don't think I could consider that as mutating state.

This is mutating state

let color = "red";

if (x} { color = "purple"; }

because it mutates the variable value at run time based on something else.

The variable at different parts of the program have different values.

If you read .one span with the CSSOM in the above example it will always return color: purple.

That is not a mutated value, that is poorly written code that has been compiled down.


again here, in case someone wants to argue that the css itself has mutated the state, no it hasn't.

JS with CSSOM can read what the value .myspan, .one span and .two span all are.

because this is the point where CSS breaks down as a db holding properties, it is here that it is instructions for the HTML language on how it is to run transformations.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: