frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: How do you model temporarily invalid data structures

2•escot•7h ago
Say you’re making a chess game. A valid board only has one piece per square, or zero. But, you have an algo that generates boards by moving pieces around and during that algo you may move a piece on top of another and then later decide to move one of them to get back to valid.

Do you try to use some existing Board type and just avoid in your algo those invalid states (like by using a stack or some data structure to avoid iteratively moving pieces one at a time).

Do you have a separate InvalidBoard type that allows multiple pieces per square?

I think it’s context dependent but I’m curious how you’ve seen this handled in different ways.

Comments

shoo•2h ago
Like you say, there's a fair bit of nuance. Data representations that prevent invalid states being represented are extremely helpful in many domains. I'd strongly recommend making invalid states impossible to represent for schemas at external interface boundaries - although that could also create problems if the definition of what's valid changes over time!

A niche example of this kind of thing is when solving commercial/industrial combinatorial optimisation problems. Maybe the goal is to maximise profit or minimise capex subject to a large number of constraints. Sometimes it is intractable to solve the true problem, but there's some approximation of the problem by relaxing one or more constraints that's much easier to solve. In some of these business contexts its completely OK if the optimiser ran overnight before spitting out a solution, provided it found a 5% better one. In that setting it'd be natural to decouple the internal representation(s) of a black box optimiser from the high-level way you represent the true problem & its solutions elsewhere in the system.

Some of these systems might end up feeling a little like a compiler toolchain - high level descriptions of problems & solutions that get transformed into / recovered from lower level implementation-specific representations.

If your context has high performance needs, e.g. needing to solve the problem 30 times per second in a real-time game or control system, or react as quickly as possible in a low-latency trading system, maybe it could be less of a good tradeoff to introduce avoidable copying of data between a strict correct representation & a relaxed representation. Could write the clean thing first & then profile and see if the overhead of copying is relevant. If the representation of your solution is small, there probably isn't that much overhead in copying it, unless your performance needs are extreme or your hardware is severely limited.

If every OS was open source, you could run vista

3•ms_menardi•3h ago•0 comments

Ask HN: Is anyone working at least 4 hours daily on an Apple Vision Pro?

141•widenrun•21h ago•95 comments

Ask HN: How do you model temporarily invalid data structures

2•escot•7h ago•1 comments

Sqlit – A lazygit-style TUI for SQL databases

19•MaxTeabag•1d ago•1 comments

We built a B2B API infrastructure for resume parsing and tailoring

3•engspeedy•13h ago•0 comments

Amdahl's Law for LLM generated code

3•akiarie•16h ago•1 comments

Did the Linux memory management maintainer "just quit"?

12•hliyan•1d ago•3 comments

Ask HN: When and why did you start believing in God?

7•dvrp•23h ago•35 comments

Ask HN: Why didn't the C64 come with Simons' BASIC in the box from 1983 onward?

15•amichail•2d ago•13 comments

Ask HN: How do you prepare to tech interview? Any tip and tricks?

16•DmitriyBuchilin•1d ago•19 comments

Ask HN: Did Messages get removed from Google Takeout?

22•MollyRealized•3d ago•1 comments

Ask HN: Why do we snore? And what to do about it?

19•simonebrunozzi•1d ago•17 comments

S there room for a VPN with zero Five Eyes servers and RAM-only infrastructure?

4•yanbinette•1d ago•2 comments

Ask HN: Shouldn't Google need to give a public statement about Railway incident?

180•srameshc•6d ago•105 comments

Ask HN: Best worldwide / classic phone games?

7•bix6•2d ago•7 comments

Ask HN: How to get back into programming without AI?

9•coolcoder613•1d ago•18 comments

Are Tech Meetups Dead?

15•mlinhares•2d ago•13 comments

Alternatives to HN for "tech outside of AI" discussion?

61•summonerOS•1w ago•37 comments

Ask HN: Are there any serious efforts to organize tech labor now?

40•0rganize•6d ago•29 comments

Ask HN: Is $300/HR too low these days for custom full stack?

20•noduerme•3d ago•15 comments

You've reached the end!