Re: on goto
nathancbaker@gmail.com (Nathan Baker) wrote (abridged):
Yes. It led to simple code, but that was partly because all the
complexity was hidden behind an iterator. Nathan Baker's version
exposes some of that complexity, and I think illustrates how hard
it is to get it right.
Perhaps the words "simple" and "complexity" have different meanings
to different people because of their different training and the goals
they've been conditioned to target??
Juha's code uses 3 loop constructs, 4 conditionals, and 2 exit
points.
Mine uses 1 loop construct, 2 conditionals, and 1 exit point.
But your code was broken in the ways pointed out by other posters. In
particular, it accesses data[0][0][0] even if data[0].size() is zero,
which will give a bounds error or a crash. It also failed to return a the
address where the item was found, and was incapable of searching for
value 0. It also did huge amounts of unnecessary work, in that the inner
loop tested all of the conditionals. (And by my count it uses a lot more
than 2 conditionals even as posted.) You need to fix all the bugs in your
code before we can even consider its complexity.
When I try to write code myself that uses your approach, but correctly
handles all the cases the original code handles, I get a horrible mess.
Specifically, you need to be aware that data[0][0].size() may be
different to data[1][0].size() or data[0][1].size(), and that any of them
may be zero meaning that (eg) data[0][0][0] does not exist and must not
be accessed. Similarly for data[0].size() and data.size(), and data[0][0]
and data[0].
This seems to mean you need a loop to find the first accessible value
before you even look at data[xInd][yInd][zInd].
-- Dave Harris, Nottingham, UK.