TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Writing correct code, part 1: whatever happened to invariants?

49 pointsby MikeTaylorabout 15 years ago

7 comments

barrkelabout 15 years ago
The irrelevant and distracting pictures of food on this blog are <i>really</i> starting to annoy me. I wish the irrelevant picture URL was more predictable, such that it wouldn't accidentally conflict with e.g. a graph or diagram.
评论 #1293909 未加载
alexgartrellabout 15 years ago
Denote preconditions, postconditions, and invariants during interviews! It'll save your life when it comes to quickly finding the "off-by-one" bugs that tend to be problematic when you're reinventing strcspan or another C string manipulation function, or doing some crazy recursion.<p>In real life, whenever I'm writing complex C functions, I always denote my preconditions, postconditions, and invariants. I also tend to denote non-obvious pre/post conditions above the functions, to optimize for the case of source-diving (when someone's just glimpsing around and trying to get the gist).
lsbabout 15 years ago
Invariants are still here. Instead of talking about them in comments, you can have automated test-generation via QuickCheck:<p><a href="http://haskell.org/haskellwiki/Introduction_to_QuickCheck" rel="nofollow">http://haskell.org/haskellwiki/Introduction_to_QuickCheck</a>
_piusabout 15 years ago
Nice article. There's been a backlash against formal correctness and verification in software, but fundamentals like asserting (or even just writing) invariants can make almost everyone's code better.
eruabout 15 years ago
The beauty of Haskell and other languages with a powerful type system is, that you can express a lot of invariants with the type system.<p>E.g. it is possible to express the Red-Black-Tree invariants in Haskell's type system.
RiderOfGiraffesabout 15 years ago
Lovely article about a simple technique that can be used to prevent a lots of bugs, and assist in the generation of correct code.<p>Interesting that his code still has a (probably never triggered) bug.<p>EDIT: Actually, looking at the exchange in the comments, the bug I thought it has, isn't. The code is right, but for a subtle (to me) reason that isn't clear (to me) from any of the comments, the code, or the invariant. Even more interesting than I thought.
WildUtahabout 15 years ago
This is my favorite sushi blog and the programming content is worth reading sometimes, too.