Skip to content
Prev 39219 / 63421 Next

Request: Suggestions for "good teaching" packages, esp. with C code

Ted
On Behalf Of Gabor Grothendieck
NB: I am drawing on my experience with C++ and Java, as I have 10x as much
experience  with them as I do with R)

It  is often not practicable to use a single point of exit.  I routinely
have checked all the requirements/assumptions of my code at the beginning,
to ensure error conditions do not arise once the code that does the real
work gets started.  That means that there is as least one exit point between
the beginning of my checks and the beginning of my code that is doing the
real work; often more.  These exits generally include construction of an
error condition object with the details of what the error is and why it
happened. (but that is my high performance C++ code, and Gui code written in
Java).
purposes of this widely practiced principle is to encourage modularity.

This I'd agree with, to an extent.  I routinely try to keep my functions
short enough to be able to see the whole thing without scrolling.  This
means I break large tasks into a number of small ones, implemented in
functions that can be inlined.    And of course, such small functions make
writing complex conditional blocks much easier and it makes them much more
readable.  Thus, if you looked at my C++ code, you'd find a large number of
smaller functions with a single exit, and a small, but significant, portion
of my functions are a bit longer with multiple exits.
return rather than multiple points of return.

This is an issue only if your code is badly designed spaghetti code.    if
your function is that long, it will be a nightmare to write decent unit test
that test all possible paths through the code, let alone those tests
required to verify that the result it produces is correct.  But if you have
ensured that all your functions can be viewed on your screen without
scrolling, it is easy to see all exit points, and write unit tests for each.
The functions that test for conditions that can produce errors often form
the basis of the unit tests needed for testing every possible exit point
(basically killing two birds with one stone).  This is relatively simple if
handled right, with a good eye for detail.

One of the things I would point out is that such generalities can be useful
in introducing young people to programming, but it is wise not to be too
dogmatic or generalize too widely.

Cheers

Ted

Thread (20 messages)

Paul Johnson Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Hadley Wickham Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Jeff Ryan Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Gabor Grothendieck Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Sébastien Bihorel Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 David Scott Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Geoff Jentry Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Kevin Wright Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Geoff Jentry Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Jeff Ryan Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Gabor Grothendieck Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Ken.Williams at thomsonreuters.com Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 David Scott Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Gabor Grothendieck Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Duncan Murdoch Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Ted Request: Suggestions for "good teaching" packages, esp. with C code Feb 15 Martin Becker Request: Suggestions for "good teaching" packages, esp. with C code Feb 16 Duncan Murdoch Request: Suggestions for "good teaching" packages, esp. with C code Feb 16 Luke Tierney Request: Suggestions for "good teaching" packages, esp. with C code Feb 16 Martin Becker Request: Suggestions for "good teaching" packages, esp. with C code Feb 16