To goto or not to goto?

Started by
92 comments, last by godplusplus 12 years, 8 months ago

I don't know much about working in a team, that's sure. But isn't the implementation of one pure function the responsibility of one or just a very few workers? When does a worker need to read someone else's implementation of a pure function? I can imagine that but definitely not on a daily basis, and not in a scale that concerns the whole team.

By pure functions I mean pure functions, that are mathematical functions/algorithms for example, and that are obviously properly unit tested, etc.


You go to war with the code base you have not the code base you want.
Advertisement

But isn't the implementation of one pure function the responsibility of one or just a very few workers? When does a worker need to read someone else's implementation of a pure function? I can imagine that but definitely not on a daily basis, and not in a scale that concerns the whole team.


This is more or less the exact opposite of the truth.

There are many cases when one needs to read (and deeply understand) someone else's code:
  • Code reviews
  • Understanding the implementation details of an API (because in reality, "purity" doesn't exist)
  • After the author has left the team
  • When it is more expedient to just read the code than to pester the poor guy all day for information about his work (this is, roughly speaking, 90% of the time)
  • Analyzing code from external sources, e.g. examples or open source

I could probably think of a few more, but that's just off the top of my head.

Code is read more often than written; the more people you add to the team, the higher the percentage of time spent reading code versus writing code.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Testing only confirms the presence of bugs, not their absence.

Plus sometimes you need to read the function to determine how you can call it, e.g. are its parameters optional? What do those boolean parameters actually mean? These things aren't always documented, and the documentation may not be up to date.

The fact that the function might be pure - just another thing that can end up being out of date!
It's much clearer now, thanks!
In C++ goto is completely unnecessary and I've never once felt the need to use it. I can see why it can be useful in C code in certain situations, though.

I guess I am a heretic, but I use goto in other situations that haven't been mentioned here. The most common is something like this:
for (int i=0; i<n; ++i) {
switch (some_array) {
case 0:
//...
break;
case 1:
if (some_condition())
goto DONE; // Break out of the loop, in a situation where `break' would just break out of the switch block.
break;
//...
}
}
DONE:
//...



Heretic! :P

[source]
for (int i=0; i<n && !(some_array==1 && some_condition()); ++i)
{
}
[/source]

In C++ goto is completely unnecessary and I've never once felt the need to use it.

As a form of exception handling when you are compiling C++ with exceptions disabled?

My display is 60 lines. Long before I reached the goto, I'd seriously be questioning if the function should be broken up. Probably into at least 5 or 6 different subfunctions.


Just because a function is long does not mean it is bad. If the method you've pulled out is actually used in a number of places (instead of just the one location you pulled it from), then it may sense to turn it into a function. If it is only called from that one location then you've basically just made the code a bit harder to maintain for no real benefit.

[quote name='alvaro' timestamp='1312691317' post='4845658']
I guess I am a heretic, but I use goto in other situations that haven't been mentioned here. The most common is something like this:
for (int i=0; i<n; ++i) {
switch (some_array) {
case 0:
//...
break;
case 1:
if (some_condition())
goto DONE; // Break out of the loop, in a situation where `break' would just break out of the switch block.
break;
//...
}
}
DONE:
//...



Heretic! :P

[source]
for (int i=0; i<n && !(some_array==1 && some_condition()); ++i)
{
}
[/source]
[/quote]
Sure, but readability would suffer ridiculous levels by putting the various states of some_array into the loop's construction. I mean that already looks a bit shady with just the one in there.

[quote name='RobTheBloke' timestamp='1313772092' post='4851265']
[quote name='alvaro' timestamp='1312691317' post='4845658']
I guess I am a heretic, but I use goto in other situations that haven't been mentioned here. The most common is something like this:
for (int i=0; i<n; ++i) {
switch (some_array) {
case 0:
//...
break;
case 1:
if (some_condition())
goto DONE; // Break out of the loop, in a situation where `break' would just break out of the switch block.
break;
//...
}
}
DONE:
//...



Heretic! :P

[source]
for (int i=0; i<n && !(some_array==1 && some_condition()); ++i)
{
}
[/source]
[/quote]
Sure, but readability would suffer ridiculous levels by putting the various states of some_array into the loop's construction. I mean that already looks a bit shady with just the one in there.
[/quote]

Okay.
bool bContinue = true;

for (int i=0; i<n && bContinue; ++i) {
switch (some_array) {
case 0:
//...
break;
case 1:
if (some_condition())
bContinue = false; // Break out of the loop, in a situation where `break' would just break out of the switch block.
break;
//...
}
}
//...




I have honestly never even considered goto here, and still will never.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement