Better HN
topnewbestaskshowjobs
Log inSign up

csrf-token

59 karma · joined Sep 15, 2020 (6y ago) · 4 submissions

Submissions

comment 3y ago · link · parent

The way I think of invariants is in less formal terms, but which I find helpful on a day-to-day basis. Often, we write code that manipulates state. It can be difficult to reason about code that manipulates state if we can't narrow down the possible states an object or struct can be in during execution.

Invariants in our code or design can help narrow down this space of possible states by ensuring that certain combinations are impossible.

For instance, assume I have an object that contains an array of integers and a boolean called `hasNegatives` which should be true if and only if the array contains at least one negative integer. If the boolean can be updated or set independent of the array, then the object can't really guarantee that the array and `hasNegatives` will always be "in sync" so to speak.

If your code promises to maintain that invariant (by, for instance, making `hasNegatives` read only and privately updating it when the setter for the array is called to match what's inside the array) then when using this object or seeing an instance of it elsewhere in the code you can happily use it with confidence knowing that the field is always correct with respect to the array within.

Obviously, this is a simplified example, but when applied to an object with many fields, knowing that only certain valid combinations are possible can radically simplify the rest of your code that interacts with that object.

comment 3y ago · link · parent

I don't think "struggling" is the right perspective here. If you have more RAM available, the browser is built to prefer using it in lieu of re-fetching data or writing to disk. Using RAM is orders of magnitude faster and results in a snappier app across tabs, when you browse history, etc. It's not hungry because the browser is written poorly. There's no magic data structure that can keep us from caching HTTP responses in order to make the browser snappy so the browser uses more when you have more.

Certainly other apps could do better. But I've made peace with the fact that browsers and IDEs, for instance, should use as much as I have available because the whole point is for them to be fast and to be fast they need to cache as much data as possible.

comment 4y ago · link · parent

humans drew the pictures.

comment 5y ago · link · parent

Love these scroll-zoom format articles from NYTimes that go through a work of art in detail. I always learn something interesting.

Here are two others i've enjoyed: https://www.nytimes.com/interactive/2020/09/25/arts/durer-se...

https://www.nytimes.com/interactive/2020/08/07/arts/design/h...