News for hackers. Stuff that matters.
Re:How to avoid the assignment statement by philwelch on Monday July 12, 2010 @04:25AM (#1507199)
Error checking can be done in a function-composing style if your functions check for error on their inputs, recognize error-indicating values on their parameters, and consistently output error-indicating values.

There is definitely waste involved in this--when composing functions this way, you have extra function calls and error-code checking on your parameters rather than checking data along the way and bailing out if there's a problem. Your functions have to check the arguments rather than having their preconditions enforced outside of them, which makes the composed functions less elegant. I don't know how practical it is in the end, but pushing most of the error-checking complexity into lower level functions and writing your upper level code in a compositional style sure seemed elegant every time I tried it.

Re:How to avoid the assignment statement by cperciva on Monday July 12, 2010 @04:52AM (#1507250)
Error checking can be done in a function-composing style if your functions check for error on their inputs, recognize error-indicating values on their parameters, and consistently output error-indicating values.

True -- but this is only possible if you accept layering violations. To take an extreme example, how is printf() supposed to know that if a string parameter is NULL then it should error out without printing anything?

Re:How to avoid the assignment statement by loup-vaillant on Monday July 12, 2010 @05:13AM (#1507288)
Your example is wrong. Functions parameters should be guaranteed to be error-free in the first place. If you decided that a NULL string was an error condition, you should test for that before calling printf.

You should test return values, not parameters.

Re:How to avoid the assignment statement by philwelch on Monday July 12, 2010 @05:29AM (#1507306)
If you have to check your return values yet you can't check your arguments, how do you compose functions? foo(bar(x)) requires foo to check its argument, since bar(x) could return an error code and foo() needs to know how to deal with it.

You don't have to check all possible instances of spurious arguments--you should be allowed to specify some preconditions--but you definitely have to check for error codes!

Example: Suppose we're writing a basic filesystem. We have the following primitive functions:

unsigned long getino(char pathname): For a given designated device and path, get the inode number of the designated file. Return 0 (there is no inode number 0) in case of error.

MINODE iget(unsigned long ino): For a given designated inode number, return a pointer to a data structure in memory containing the INODE data; allocated if necessary. Return NULL in case of error.

You can either test your return values from these functions, in which case you will do this:

  unsigned long myIno = getino(path);
  //error check myIno
  MINODE * myMINODE = iget(myIno);
  //error check myMINODE
or put some argument checking in iget() and do this:

  MINODE * myMINODE = iget(getino(path));
  //error check myMINODE
How much argument checking has to go into iget()? Exactly this much:

  if (!ino) return NULL
It seemed like a neat idea at the time, and considering I called that particular composition of functions a dozen times in that project, there was a lot less repeating myself in teaching my functions to understand error codes.
Re:How to avoid the assignment statement by cperciva on Monday July 12, 2010 @06:06AM (#1507371)

  unsigned long getino(char * pathname):

  For a given designated device and path, get the
  inode number of the designated file. Return 0
  (there is no inode number 0) in case of error.
Completely off-topic, but there is a bug in this prototype: Inode numbers have type ino_t, not type unsigned long.

(Ok, two bugs, if you count the fact that there can theoretically be an inode number 0. And three bugs if you count the fact that pathname should be declared as a (const char *). But the ino_t vs. unsigned long bug is really bad if you care about portability.)

Re:How to avoid the assignment statement by philwelch on Monday July 12, 2010 @06:26AM (#1507395)
You're right on all counts, and if I were writing a real filesystem rather than a class assignment I would have taken these issues more seriously.

(edited to add paragraph:) Nothing I've said in this discussion should be taken as a strongly held opinion about anything--I'm rather shy about sharing these thoughts in the first place, but considered this particular example illustrative of a possibility I take seriously.

You do not want to see the other horrors that particular professor of mine has perpetrated, incidentally.

Re:How to avoid the assignment statement by loup-vaillant on Monday July 12, 2010 @05:59AM (#1507361)
In general, I prefer not to have to check for arguments. In your case, the right™ way to do it is probably monads: they help you separate the error checking from the "normal" path.

But that's C code, so I have to agree with you.

Re:How to avoid the assignment statement by cperciva on Monday July 12, 2010 @05:18AM (#1507296)
You're disagreeing with the wrong person here.
Re:How to avoid the assignment statement by loup-vaillant on Monday July 12, 2010 @05:54AM (#1507350)
I disagreed with your "extreme example", on the grounds that checking arguments instead of return values is a bad idea.
Re:How to avoid the assignment statement by cperciva on Monday July 12, 2010 @06:00AM (#1507365)
Yes, I know. I also disagree with philwelch's suggestion of checking arguments -- that's why I presented that extreme example.
Re:How to avoid the assignment statement by loup-vaillant on Monday July 12, 2010 @06:33AM (#1507405)
OK, let's pretend I was tired. I was disagreeing with the wrong comment…
Re:How to avoid the assignment statement by philwelch on Monday July 12, 2010 @05:13AM (#1507289)
The easiest way is to treat printf() as a procedure rather than a function, and to restrict your use of compositional style to cases where it's still a good idea.
Re:How to avoid the assignment statement by nostrademons on Monday July 12, 2010 @06:13AM (#1507378)
You've just invented monads. ;-)