Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

67% Positive

Analyzed from 487 words in the discussion.

Trending Topics

#case#reserved#switch#check#try#need#bid#ask#buffer#more

Discussion (10 Comments)Read Original on HackerNews

SPascareli1332 minutes ago
I never fully understood how to work with this "reserved" values being valid instead of errors when you can't allocate more memory, in either case you still need to check if you have a real entity or a reserved/error, right? Does it really make things simpler?
AlotOfReading25 minutes ago
It helps avoid the happy path effect. You're always handling something and there's no hidden control flow from the program runtime creeping in because of the cases you missed.
Tcepsa26 minutes ago
In the reserved case you do still need to check if you have a reserved entity, but you can put it (along with the other allowed "pseudotypes") in a switch/case block and have it just break back out immediately (the no-op mentioned in the article) rather than having to use a separate if/else to check for Null, or clutter things up with a try/catch wrapper.

Does that address what you're asking about?

SPascareli1317 minutes ago
But that assumes I have some switch case somewhere right? If I passed an array of "orders" to a downstream function, it knows that what it has is orders, not something else, so it doesn't need to check for anything, and in the case I checked for errors upstream (when I try to allocate a new order) all downstream functions know that no invalid order can be passed, in which case you only have one check at creation time.

That's why I haven't fully understood yet how working like this is simpler.

Tcepsa8 minutes ago
Fair enough; in the example provided the "tag" was allowed to be "bid, ask, or reserved" so I assumed there would be a switch statement to control the handling of the bid and ask specifics, and that it could drop the reserved ones there. That's less helpful when it's just between "is this an actual instance or just a placeholder?"
sjducb11 minutes ago
Why is a try/catch wrapper clutter but a switch case is not?
Tcepsa2 minutes ago
I had assumed that there would already be a switch/case because I was going with the example in the code of there being three pseudotypes (bid, ask, and reserved). So it seemed natural to me to use a switch statement to have it execute the code specific to them, and that doing so would be less cluttered than not having the reserved option and instead doing a switch/case (or if/else-if) for bid/ask and a separate try/catch wrapper in case it was a null object.
theokruegerabout 1 hour ago
static allocation is de-facto standard in embedded for obvious reasons, and works really well there. in operating systems with more complex memory models designed entirely around dynamic workloads, im not sure asking devs to adopt another slightly complicated design pattern that imposes new hard caps is any less of a cognitive load than before.

i can't bash the functionality and correctness aspect of static allocation, but it is akin to the humble linked list in the sense that you should already know going into the problem that you need it.

AlotOfReading35 minutes ago
Someone recently made the point to me that a lot of dynamic situations can be rewritten as locally static allocations with proper continuations. The idea being that you re-enter the continuation with more memory when you've exhausted your existing pools. The problems are obvious, but it's a neat middle ground.
senderista16 minutes ago
The simplest example being a stack buffer that expands to a heap allocation when required. There is an API pattern to facilitate this: when the size of the provided "out" buffer is insufficient to hold the result, return an appropriate error code and populate an out parameter with the required size. So you try once with the stack buffer, and if that fails, retry after allocating a heap buffer of precisely the required size. We used this pattern everywhere in Windows dev.