15 ms·
I’d like to mention Go. This seems to me like a pointless quibble about how to catch and handle errors, the kind of argument you see buried in the HN comments a
by klodolph 8y ago
I’d like to mention Go. This seems to me like a pointless quibble about how to catch and handle errors, the kind of argument you see buried in the HN comments and gloss over because you’ve heard it a bunch of times before. The assert() macro in C is roughly just if ... { panic() } in Go. You want that, use it. So the idea that getting rid of assert is “essentially telling developers that they are not allowed to do run-time verification of invariants” is completely nonsensical.
The Go philosophy against assert() and assertion libraries is largely that the error messages produced by assert() are low quality. The idea is that you should not just have a stack trace, you should have a good error message. The assert() macro can give you a stack trace with some effort, but rarely gives you a good message. Good use of panic() is easy and gives you both. It’s also used mainly for runtime invariant checking, although there are some other ways it can be used (since it can be caught).
- hermitdev 8y agoI dont kbow about go, but in C or C++, it's a common idiom to do something like: asset(condition && "static reason why we expected this condition to be true"); Unfortunately, this doesn't allow for a dynamic string, but does generally present a message about the assert. Also, if properly configured, a failed assert results in an abort and should generate a core/crash dump. Pretty useful to be able to load in a debugger for further inspection. But to the point about provide a good message: that's on the developer, and its going to be true no matter the language.
- htfy96 8y agoif (argc != 2) throw std::runtime_error("Expect 1 argument. Actual "s + std::to_string(argc - 1)); Don't get terrified by exception. If you don't catch any exception, then it is mostly abort() with dynamic string
- Asooka 8y agoIt still incurs a nontrivial runtime cost, unless you've ifdef'd it away in release builds, but even so it is a heavy price to pay in debug or semi-debug^ builds for things like container operator[] bounds checking. The best implementation of assert I've seen raises SIGTRAP on error, which neatly lets you attach a debugger and break when the error occurs, at which point you have the full stack trace and program state. ^ semi-debug in our case means minimal optimisations and debug asserts enabled.
- jcelerier 8y ago> It still incurs a nontrivial runtime cost, why do you care about the runtime cost of something that makes your program crash ?
- fredsanford 8y agoExceptions have a setup cost. You pay that setup cost every time an exception frame is created.
- plorkyeran 8y agoThat is not true on most platforms in use today. The Itanium ABI uses "zero cost" exceptions which do not have any setup or runtime cost when an exception is not thrown (at the cost of making throwing exceptions much more expensive than other schemes). 64-bit Windows and arm64 use similar schemes.
- fredsanford 8y agoI guess I have to use weasel words in every post now. Let's pretend my previous post started with "In most cases, "
- Dylan16807 8y agoBut that's the point, it's not most. GCC and Clang default to zero-cost DWARF. Add Windows and Arm64 and that's almost everything.
- jcelerier 8y agoThe only "relevant" platform where exceptions aren't zero cost nowadays is 32-bit windows - not even 64-bit one. so no, absolutely not "in most cases".
- jasode 8y ago>why do you care about the runtime cost of something that makes your program crash ? I think both parent commenters (htfy96 and Asooka) got on a tangent because htfy96 substituted a throw exception for an assert() in gp's (hermitdev) code -- as if they were equivalent. However, exceptions have fundamentally different semantics from asserts and are not substitutes for each other. (For anyone unfamiliar with the different purposes of exceptions vs asserts, please read the top 2 answers in this Stackoverflow question.[0]) Things like array bounds checking (used as a runtime safety check instead of an "impossibility" check) would not be tested with assert(). To answer your direct question, programmers who use assert() correctly for "testing their understanding of the world by testing an impossible condition" don't want to pay a runtime penalty for it. [0] https://stackoverflow.com/questions/1467568/debug-assert-vs-exception-throwing/1468385 https://stackoverflow.com/questions/1467568/debug-assert-vs-...
- int_19h 8y agoThis will unwind the stack, executing destructors. Which is the last thing that you want to do in a scenario where conditions that were supposed to be invariants are broken. If your invariants don't hold, you don't really know what's going on - think of it as high-level undefined behavior. The faster you stop executing code within the security boundary (usually process), the better. This is exactly why assert() calls abort(), and why you should do so as well as soon as you've logged enough data to diagnose the issue.
- barrkel 8y agoEasier is to embed file and line number and possibly stringified assert expression via a macro. The string approach might help as a comment when reading the code, but suffers from all the problems comments do.
- jdsully 8y agoI usually just make my own AssertSz() macro that can take printf args. Works great for those pernicious asserts that need more data.
- msla 8y agoDoes panic() in Go give you source code line numbers and file names? As far as I'm concerned, assert() has two jobs, both essential: Killing the program AND giving an error message which gives the developers knowledge of precisely where the deadly assert() is. Doing only one of those things leaves the job undone.
- khc 8y agopanic gives you line number and stacktrace
- draw_down 8y agoIf panic() can be caught, then that is not equivalent to assert.
- repsilat 8y ago> The assert() macro in C is roughly just if ... { panic() } in Go The article very clearly says that (unlike normal C behaviour) it's a no-op in release builds of SQLite, because otherwise they get a ~3x slowdown. Does Go compile away those `if` statements?
- Stratoscope 8y ago> Does Go compile away those `if` statements, or did you not read the article? The HN guidelines specifically request that we not make comments like this. > Please don't insinuate that someone hasn't read an article. "Did you even read the article? It mentions that" can be shortened to "The article mentions that." https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- repsilat 8y agoYou're right, it was out of line. Edited, and thanks.
- Stratoscope 8y agoWell, thank you for taking criticism so well! We all get a little snippy at times - goodness knows I've done it myself more than I want to admit. I was about to delete my comment since it was no longer relevant, but maybe I will leave it here since your reply and edit are such a great example of how to be constructive and civil. (OTOH, let me know if you'd prefer for me to delete it.) Thanks!
- tptacek 8y agoHelp me understand why this matters? Is it really Hipp's argument that Go is unsuitable for serious systems work because invariant signaling in the code is too slow? That seems pretty nitpicky, and also ironic since the "serious" systems language to which he's comparing Go is C.
- RHSeeger 8y ago
- theonemind 8y agoI don't think assert got used correctly if it needs a meaningful error message, personally. When an invariant of my algorithm fails, I've generally found I have a fundamental logical error with no particular relationship to the point in the code where it fails.
- yawboakye 8y agoThe Go compiler has `assert` and uses it for checking invariants, as mentioned in the article. I love Go, I'm down with the decisions the designers have made for the language but not exposing a proper and simplified assertion statement/expression while expecting the user to make their own doesn't sit well with me.
- adito 8y agoIsn't go compiler written in go? Is it different version of go that has `assert`?
- apta 8y agoThey already have precedence in this, having a special version of the language only the golang authors can use (e.g. generics and asserts), and then claiming that they're not useful or too complicated for others to use.
- dan-robertson 8y agoError handling is quite different in Common Lisp than in other languages. Assert is also different. An assert has up to three parts: 1. The expression to be asserted 2. A set of places (ie variables/references) 3. A format datum and arguments (like sprintf) When an assert fails you see the formatted message from (3) and get some options: - invoke a restart to some place higher up the control stack - invoke the restart generated by the assert which asks for new values for the set of places in step 2, and then retries the assert - abort - crash These options may be invoked by the program itself or by the user if the debugger is invoked