News for hackers. Stuff that matters.
Re:Assertions in Production Code? (2008) by pmichalina on Friday August 31, 2018 @10:55AM (#17886071)
Assertions, throws, and try-catch are code smells. Really bad design choice considering we have more elegant solutions like monads.
Re:Assertions in Production Code? (2008) by js8 on Friday August 31, 2018 @11:45AM (#17886480)
There are three main ways to reduce errors in code: tests, asserts and abstractions. All three have advantages and disadvantages and generally deal with different classes of errors.

In this thread, we discuss asserts, not abstractions. Monad is an abstraction. (On the other hand, many people only see tests and forget about two other solutions, which are as important.)

Re:Assertions in Production Code? (2008) by pmichalina on Friday August 31, 2018 @02:31PM (#17887805)
Tests are good of course, but that assumes the tests are valid. I’d rather trust compiler to ensure runtime safety.
Re:Assertions in Production Code? (2008) by TeMPOraL on Friday August 31, 2018 @11:21AM (#17886266)
Assertions, yes, in so far as they mean "I know this can't happen, but I can't structurally prevent it in the program in an elegant fashion, with the tools I use". However, I can't see how monads are suddenly going to make this problem disappear.

As for throws/try-catch - beyond assertion use, they also report all problems that are outside your control (like IO failing due to HDD damage, or connection dropping). Those kinds of problems are here to stay.

Re:Assertions in Production Code? (2008) by oftenwrong on Friday August 31, 2018 @11:36AM (#17886391)
Sure, monads provide an elegant way to structure code with error conditions, but I don't see a problem with using simple assertions, throws, try-catch to enforce invariants. Personally, I don't care about elegance as much as whether or not it works, as long as it is simple to use and maintain. I can return something like a Maybe/Either, or I can throw an exception - both approaches are fine by me if it forces the programmer to account for error cases.
Re:Assertions in Production Code? (2008) by pmichalina on Friday August 31, 2018 @02:29PM (#17887795)
Throwing is a side effect breaking flow of control for no good reason. It’s takes you down the same path as all other spaghetti code.

Not as much elegance as it is about purity. Minimizing side effects is the number one way to reduce bugs.

It should be the number one guiding principle when creating out reliable software. Which means, you simply cannot use the primitive try catch or similar construct. Don’t break flow of control. Guide it to a terminal value instead.

These days, when I see try-catch and if-else constructs (which is in most codebases) it’s clear there will be bugs over the life of the application.

It’s fine, use them, but there is a world of greatness when you ditch these faulty constructs. Just like ditching OOP constructs. All built on false premises.

Re:Assertions in Production Code? (2008) by oftenwrong on Thursday September 06, 2018 @08:56AM (#17925869)
If you application is interacting with the outside world, you will be dealing with side-effects, and sometimes error conditions will happen.

For example, today I am working on a service that:

- uses a database

- calls external APIs

- publishes and consumes from a message broker

- interacts with local and remote filesystems over a variety of protocols

All of these things entail error conditions, most of which throw exceptions in the corresponding libraries. Sometimes they are converted to Either/Maybe, and sometimes they are wrapped in "native" checked exceptions.

The important bits:

1. The type system and compiler make sure the programmer has to deal with the error conditions at some point. From a programmer's perspective, a checked exception bubbling up the stack is not very different from returning a monadic object up the stack.

2. The core of the application is entirely pure. No exceptions (in both senses of the word). All side effects are pushed to the boundaries.

Re:Assertions in Production Code? (2008) by platinumrad on Friday August 31, 2018 @04:26PM (#17888414)
This is what happens when you read just one chapter of Learn You a Haskell kids.

Monads, not even once.

[dead]
Re:Assertions in Production Code? (2008) by platinumrad on Friday August 31, 2018 @07:14PM (#17889170)
Nobody here is saying that monadic error handling isn't good (in case it wasn't abundantly clear, my comment was this thing that people call a "joke"). The issue is pmichalina flippantly disparaging asserts, one of the few good things left in this hell world:

https://www.microsoft.com/en-us/research/publication/assessi...

Re:Assertions in Production Code? (2008) by carapace on Friday August 31, 2018 @08:23PM (#17889405)
It wasn't abundantly clear, but I'm often humorless.

Sorry for being so crusty. I just found out that about a third of my professional career was essentially waste because I was too thick to adopt Prolog twenty years ago. It almost physically hurts to think about it.

Anyway, I think OP's right about assertions being a code smell: an assertion is literally the place where you tell the machine you don't know exactly what you're doing, eh?

I've been working towards an error-free mode of development, a combination of Dr. Margaret Hamilton's "Higher Order Software" concept that developed out of the Apollo 11 project with mathematical derivation of programs. I'm just about ready to "productize" it but I'm not going to market it to other developers. I'm going to sell it directly to consumers. They won't know enough to argue with the method, they'll just be able to use the computer to solve problems, I won't tell them that they are programmers.

Bottom line: error-free software development methods have been available for decades but the industry tends to ignore them. In 2018 writing "assert", "throw" or "raise", etc. is a code smell: you are doing it worng. If I sound cranky it's because I am.

Re:Assertions in Production Code? (2008) by platinumrad on Friday August 31, 2018 @08:52PM (#17889504)
I hope you succeed, and I say that sincerely, but regardless of whether or not program synthesis should have been pursued more aggressively in the past, I'm not aware of any support for your statement that "error-free software development methods have been available for decades" for any reasonable definitions of "error-free" or "available".

>Anyway, I think OP's right about assertions being a code smell: an assertion is literally the place where you tell the machine you don't know exactly what you're doing, eh?

The whole point of the original article is that, today, the only thing keeping planes in the air is fault tolerance and lots of redundancy (see also: Erlang). Assertions are about humility and it's good to be humble.

>In 2018 writing "assert", "throw" or "raise", etc. is a code smell: you are doing it worng.

But somehow writing ">>=" isn't? On one hand you seem to suggest that error handling is bad because there shouldn't be errors (what about I/O? user error?) while on the other hand you seem to be defending the position that monadic error handling is a panacea.

Re:Assertions in Production Code? (2008) by carapace on Friday August 31, 2018 @11:39PM (#17890059)
> I hope you succeed, and I say that sincerely

Cheers! Me too. (^_^)

Brother, I really don't want to have this argument again. I don't care anymore if my peers believe me or not. I'm just going to bring it to market as best I can and see how it goes. Thank you for the well-wishing, and I apologize again for being so cranky.

A few lil points:

> I'm not aware of any support for your statement that "error-free software development methods have been available for decades" for any reasonable definitions of "error-free" or "available".

I know. You are making my point: The methods are there, and no one has ever even heard of them.

The primary method I am talking about was developed by Margaret Hamilton during the Apollo 11 mission to the Moon, and she has unsuccessfully marketed ever since AFAIK. Dijkstra once reviewed it and panned it harshly. It was a rare case of him not getting it. It went right over his head.

Hamilton, by the way, is the person who first coined the term "Software Engineering".

I'm not going to go into it further now, suffice it to say if you are typing text into a text editor to write code you are doing it wrong. There's a book, "System Design from Provably-Correct Constructs" by James Martin, that presents Hamilton's method if you're interested.

> The whole point of the original article is that, today, the only thing keeping planes in the air is fault tolerance and lots of redundancy (see also: Erlang). Assertions are about humility and it's good to be humble.

Planes stay in the air because they are designed to survive the failure of the individual parts. Now I would never suggest that proven-correct software cannot fail. Of course it can. Need I invoke the Cosmic Rays? I heed Murphy.

But you only write an assertion if you're not certain, eh? You're saying, in code, I think this will never ever happen, so if it does STOP-THE-LINE.

That kind of uncertainty is absurd, inexcusable, in software. (One of the very few realms that this is true, Herr Gödel notwithstanding. Software is electrified math, math is the "Art of What is Certain".)

I'm saying that our current common methods of software development are pathetic, that we are working far too hard to write software with far too many bugs, and it doesn't have to be this way. I'm further saying (to the OP) not to waste breath arguing with folks who can't or won't even accept the possibility! I've been doing it for a few years now and it's thankless. I've managed to convince exactly one other person and he's a brilliant programmer who came to it from a background in philosophy. He's a thinker rather than a puzzle-addict or complexity junkie.

Anyhow, if I manage to bring this thing together I'll make a noise here on HN (despite YC getting into bed with the Chinese Communists, the greedy fools.)

> On one hand you seem to suggest that error handling is bad because there shouldn't be errors (what about I/O? user error?) while on the other hand you seem to be defending the position that monadic error handling is a panacea.

Nope. Nothing like that. I can't explain it all her and now, but uh... Have you heard of Elm-lang? They boast zero front-end errors. Zero. The Elm compiler makes the JavaScript and it does not error. They do not write "error handling" to achieve this.

Re:Assertions in Production Code? (2008) by platinumrad on Saturday September 01, 2018 @12:09AM (#17890150)
Thanks for the response!
Re:Assertions in Production Code? (2008) by carapace on Saturday September 01, 2018 @08:48PM (#17894775)
Cheers, well met. :)
Re:Assertions in Production Code? (2008) by shidoshi on Friday August 31, 2018 @11:45AM (#17886475)
pp. 76-77 of Ousterhout's "A Philosophy of Software Design" talk about the issues with boilerplate try-catch. He basically does not approve, as they make readability suffer and often do not do what they advertise (truly catch errors.) I don't know how he would feel about production assertions, but I'm guessing he would buy into it if it helps reduce complexity. In the sense of finding remedies to troublesome bugs--which will occur.
Re:Assertions in Production Code? (2008) by swatcoder on Friday August 31, 2018 @03:39PM (#17888182)
This kind of absolutism only works in the lab and the journal.

The real world of software engineering is enormously varied and each project faces a different set of legitimate constraints and objectives. It's more valuable to discuss how and when to use "assertions, throws, and try-catch[es]" than to pretend that they're universally superceded by some other construct.

Re:Assertions in Production Code? (2008) by vmchale on Friday August 31, 2018 @11:16PM (#17889987)
Well, in languages released in the past 15 years, sure. But many languages are not that recent.