• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Programming Help

Take everything Dijkstra writes with a grain of salt. Hes an extremist language bigot.

This is the same guy who absolutely refuses to use a GoTo even when a GoTo is the best tool for the job. He would have had a point if languages offered a flow control paradigm for every job.. but they don't.. and he has absolutely no point in regards to modern Basic designs which is actualy forgivable considering he wrote all that junk prior to modern Basic design.

As a C programmer, even though goto is available I have NEVER had to use it. If you find yourself in a position where goto is necessary when programming in a structured or object-oriented language, then you have made a serious error in you original algorithm, and probably need to redesign it.

I completely agree with him about BASIC; otherwise I would not have posted the quote. BASIC encourages programming practises which are... well, frankly, bad.

Just to stick my oar in one more time: The D&D book is entirely adequate if you like textbook-style books - it has more coloured boxes and checklists (and questions, if you like that sort of thing) than you can shake a stick at. It also rejoices in what seems like a billion different typefaces to illustrate the different parts of a program. This has the opposite effect of what's intended - making it harder to read the code than if it was unadorned.

Also, it's about twice as expensive as similar books (e.g. Stephen Kochan's book), twice as heavy, and only half of the book is actually about C - the rest of it being an entirely pointless excursion into C++ and Java. I can't imagine what sort of a person could expect to learn all that from one book. I would consider it if money was no object, or you can get it from a library.

To be honest though, I wouldn't bother with C at all (although I do agree it's probably a better choice for what the OP is wanting than C++).

Of course, I would never buy such a book; I borrow all my textual references from a library, including fictional time passers. I'm pretty tight-fisted though, being an unemployed university student. ^^ However, the section of the book I was reffering to (that is, the entire section of the book on C) is very useful.

It goes quite a bit further into linked lists, stacks, queues, etc than [SIZE=-1]Al Kelley and Ira Pohl's book, "A Book on C", although it covers bit fields in less detail. I recommend both books though, for anyone wishing to learn C. And I would recommend C over C++ from a purely experienced point of view; I simply prefer working in C. I can program in C, C++ and C#, and if object-orientation is required, I would definitely choose C# over C++. I do not like C++ at all, in case you haven't noticed. ^^

This is all personal preference though, but since C is my forte, I thought I'd point out that it would probably cover everything that he needs, and he'd pick up some good programming practices along the way (to make up for his exposure to BASIC :P). In any case, no matter what language you wish to learn, I would highly recommend you read "Code Complete" by Steve McConnell. It is a fantastic resource that in my opinion every programmer should read.
[/SIZE]
 
As a C programmer, even though goto is available I have NEVER had to use it. If you find yourself in a position where goto is necessary when programming in a structured or object-oriented language, then you have made a serious error in you original algorithm, and probably need to redesign it.
What sort of gotos are we talking about? The use of break and continue in loops? I use them all the time. What about something like callcc in Scheme?

It goes quite a bit further into linked lists, stacks, queues, etc than [SIZE=-1]Al Kelley and Ira Pohl's book, "A Book on C", although it covers bit fields in less detail.[/SIZE]
[SIZE=-1]
This is part of my problem with C as a beginner's language. In Java, books show off how cool the language is with Swing GUI apps. In Python and Ruby, books show off how cool the language is by implementing a web service in five lines. In C, books show off how cool the language is by... implementing a linked list.

This is far too low level for a beginner. Yes, a person should know about the speed implications of different data structures. But do they really need to build their data structures from scratch? Not unless you're a budding computer scientist or a complete geek. I am slowly becoming the latter (I have the Cormen et al algorithms book in my to-read pile in my bedroom), but I have used a whole bunch of different data structures in a variety of languages without feeling even the slightest twinge of shame about not knowing how they're implemented.

In any case, no matter what language you wish to learn, I would highly recommend you read "Code Complete" by Steve McConnell. It is a fantastic resource that in my opinion every programmer should read.
[/SIZE] I'm reading that book right now! Spooky.
 
As a C programmer, even though goto is available I have NEVER had to use it. If you find yourself in a position where goto is necessary when programming in a structured or object-oriented language, then you have made a serious error in you original algorithm, and probably need to redesign it.

I completely agree with him about BASIC; otherwise I would not have posted the quote. BASIC encourages programming practises which are... well, frankly, bad.
One would be hard pressed these days to find a "BASIC" that had any relation to the GOTO and parameter-less GOSUB strewn language of yesteryear. Even the scripting languages now usually provide object oriented grammar and they all provide structured programming. vb.net and c#.net, though Microsoft specific, have evolved into quite useful languages.

Still, I prefer C++ for certain kinds of programs. I have a set of templated fixed point routines that makes simulating digital dsp hardware as easy as just writing standard float expressions. The C++ code that is generated is very efficient and faster than the equiv in fp.
 
Still, I prefer C++ for certain kinds of programs. I have a set of templated fixed point routines that makes simulating digital dsp hardware as easy as just writing standard float expressions. The C++ code that is generated is very efficient and faster than the equiv in fp.
Does it use that crazy template metaprogramming?
 
Does it use that crazy template metaprogramming?

A little. Mostly to determine various useful "constants" related to frac bit length and max/mins at compile time without having to include them in the instantiation. The code is still pretty readable.
 
As a C programmer, even though goto is available I have NEVER had to use it. If you find yourself in a position where goto is necessary when programming in a structured or object-oriented language, then you have made a serious error in you original algorithm, and probably need to redesign it.

I completely agree with him about BASIC; otherwise I would not have posted the quote. BASIC encourages programming practises which are... well, frankly, bad.

You are wrong about GoTo.

Please tell me how you break out of nested loops in your imaginary perfect algorithm world... do you check for the break condition in each and every nest? Do you throw an exception? Do you use code-duplication?

All of these are inappropriate to the problem and could increase the algorithms complexity considerably. Further, in the case of the multiple-tests for the break condition at every nest you have abandoned good scoping. The GoTo, in the absence of the correct flow control paradigm for this sort of problem, is the simplest most appropriate solution.

No language that I am aware of has all the necessary flow control constructs.

And in regards to Basic, do you program in Basic? Have you ever programmed in Basic? Modern Basic is a structured object oriented language.

The difference between C# and VB.NET? The semi-colon. Thats it.

Are you saying the semi-colon is what its really all about, or are you saying C# encourages bad programming?
 
You are wrong about GoTo.

Please tell me how you break out of nested loops in your imaginary perfect algorithm world... do you check for the break condition in each and every nest? Do you throw an exception? Do you use code-duplication?

All of these are inappropriate to the problem and could increase the algorithms complexity considerably. Further, in the case of the multiple-tests for the break condition at every nest you have abandoned good scoping. The GoTo, in the absence of the correct flow control paradigm for this sort of problem, is the simplest most appropriate solution.

No language that I am aware of has all the necessary flow control constructs.

And in regards to Basic, do you program in Basic? Have you ever programmed in Basic? Modern Basic is a structured object oriented language.

The difference between C# and VB.NET? The semi-colon. Thats it.

Are you saying the semi-colon is what its really all about, or are you saying C# encourages bad programming?

Haha, ha ha ha ha... I haven't laughed that hard in a while.

I have used Visual Basic, and it is VERY different to C#. Have you ever used C#?

Like I said, I have never had to use the "goto" statement (I have used "break" in switch statements, especially for event polling when I toy around in SDL, but never "continue" either). I will reiterate what I said, in that if you find you need to ruin the flow of control with a break statement, then your original algorithm off which your program is created must be flawed.

JamesM: C isn't low level! D:

And I do have a penchant for coding things from the ground up :P But seriously C would be all this guy really needs. It's very simple to write an i/o program quickly, and since he doesn't want to get into complicated data structures anyway, that would hardly be a problem. Besides, an understanding of fundamental data types is obviously beneficial, even if you don't ever use it again.

Also try directly accessing the pixel data of a surface or the contents of the sound buffer through the windows API. Good luck too. :P
 
I will reiterate what I said, in that if you find you need to ruin the flow of control with a break statement, then your original algorithm off which your program is created must be flawed.
What about continuations? They're basically dressed-up gotos.
 
What about continuations? They're basically dressed-up gotos.

Urgh, that was a mistyping.. I meant foc-breaking statements, like goto.

Break statements are necessary for the use of switch. I have never used goto or continue in the 6 years I have been programming in C, and I have never used break for anything other than switch statements. I also do not simply flag the iteration terminator when I want to break out of a loop; if your code is well structured you simply will never have to do this.

But you're reading code complete, so you would know what I'm talking about.
 
Break statements are necessary for the use of switch. I have never used goto or continue in the 6 years I have been programming in C, and I have never used break for anything other than switch statements. I also do not simply flag the iteration terminator when I want to break out of a loop; if your code is well structured you simply will never have to do this.

I know this argument, but it's not very convincing to me, because if you keep your methods (or functions or whatever) short, the chance of confusion is pretty small. And the 'well structured' way is a lot more cumbersome than just busting out of the loop. Consider the two simple chunks of Java below, for returning a Thingy object from a collection of some kind. The first one obeys the rules about not breaking out of a loop, and the second one doesn't:

Code:
boolean foundWhatIWant = false;
int i = 0;
Thingy item;
while (!foundWhatIWant && i < myList.size()) {
    item = myList.get(i);
    if item.doesWhatIWant()
        foundWhatIWant = true;
    ++i;
}
return item;

and:

Code:
for (int i = 0; i < myList.size(); ++i) { 
    Thingy item = myList.get(i);
    if item.doesWhatIWant()
        return item;
}
I don't know about you, but I like the second one better, and well-structured be damned. If I was to use a foreach loop in the second example (and I would), the second one is even shorter. Maybe your situation is different, but it's in these sorts of loops that I most commonly come across people refusing to break early, and I must say, I take great delight in refactoring that code out of existence... let's hope we never have to work together!
 
I wonder if he thought he'd be spawning an 'X considered harmful' industry when he wrote that paper.

It's such a cliche, that "considered harmful considered harmful" has entered the lexicon.

But how far can one go?

A google search reveals 162 mentions of "considered harmful considered harmful considered harmful" - a bizarrely large amount, if you ask me.

But only one person has ever opined that "considered harmful considered harmful considered harmful considered harmful", according to the mighty Goog.

And no-one has ever bothered to type "considered harmful considered harmful considered harmful considered harmful considered harmful" anywhere google can find it.

Until now.
 
If you read some of the spagetti code people wrote back then, you'd consider it harmful, too. And toss in a little instruction modification just for good measure.

But a few breaks, continues, exits, iterates, nexts, restarts, and returns are not going to kill us. It's still structured. And code like this is not easy to read:
Code:
loop {
  if condition_1_okay then
    if condition_2_okay then
      if condition_3_okay then
        important part of loop;
}
I'd rather see:
Code:
loop {
  if not condition_1_okay then next;
  if not condition_2_okay then next;
  if not condition_3_okay then next;
  important part of loop;
}
The paradigm is: get the hell out if there are any issues; do the work. Who wants to write a function with a half dozen checks at the beginning and the real stuff buried deep inside IFs and boolean variables all the hell over the place?

~~ Paul
 
Last edited:
I know this argument, but it's not very convincing to me, because if you keep your methods (or functions or whatever) short, the chance of confusion is pretty small. And the 'well structured' way is a lot more cumbersome than just busting out of the loop. Consider the two simple chunks of Java below, for returning a Thingy object from a collection of some kind. The first one obeys the rules about not breaking out of a loop, and the second one doesn't:

Code:
boolean foundWhatIWant = false;
int i = 0;
Thingy item;
while (!foundWhatIWant && i < myList.size()) {
    item = myList.get(i);
    if item.doesWhatIWant()
        foundWhatIWant = true;
    ++i;
}
return item;

and:

Code:
for (int i = 0; i < myList.size(); ++i) { 
    Thingy item = myList.get(i);
    if item.doesWhatIWant()
        return item;
}
I don't know about you, but I like the second one better, and well-structured be damned. If I was to use a foreach loop in the second example (and I would), the second one is even shorter. Maybe your situation is different, but it's in these sorts of loops that I most commonly come across people refusing to break early, and I must say, I take great delight in refactoring that code out of existence... let's hope we never have to work together!

You have completely missed what I am saying - how far into code complete are you? O_o

I also do not simply flag the iteration terminator when I want to break out of a loop; if your code is well structured you simply will never have to do this.

I'm talking about control breaking structures, not the return statement. Your second method of a linear search is simple modularisation, and obviously a good thing. I'm talking about people that, rather than seperating their code into modules, create spaghetti code by cutting the control from one section to another, bypassing a loop that normally would take place. BASIC (and other variants similar to BASIC) encourage this.

I am specifically talking about the use of "continue" and "goto" statements in C, and in some cases, "break" too.
 
I'm talking about control breaking structures, not the return statement.
But the point of the return statement in the second example is that it breaks out of the loop early, exactly like a break statement would. It is a control breaking structure.

If you want, remove the return statement in the first example, and replace the return with a break in the second - they still have the same form.

Your second method of a linear search is simple modularisation, and obviously a good thing.
I guess I am confused. The first and second methods are both terminated by return statements - they're both modularisation, they're functionally identical. But one uses return to break early, which you said was bad practice, and the other checks a boolean and uses a while loop, which I assumed you would have considered to be superior, as it doesn't violate the norms of structured programming.

I am specifically talking about the use of "continue" and "goto" statements in C, and in some cases, "break" too.
But I'm pretty sure that continue and break can't be used outside of loop constructs? Can you give a short example of the bad use of continue?

I'm talking about people that, rather than seperating their code into modules, create spaghetti code by cutting the control from one section to another
I agree that using goto to create multiple entry points to a block of code is a truly terrible idea. But you never see that in modern languages like Java, C++, Python and Ruby. And if you create small methods (which most books recommend), then there's really no harm in having multiple exit points from a block of code. So I really think that insisting on not using break and continue (which do exactly the same thing in those other languages as they do in C) makes code harder to read. It certainly does in my experience.
 
I'm talking about people that, rather than seperating their code into modules, create spaghetti code by cutting the control from one section to another, bypassing a loop that normally would take place.

You were not talking about people. You were talking about GoTo and Basic.

Neither are people.

Is your problem with People, or GoTo and Basic?

BASIC (and other variants similar to BASIC) encourage this.

No they don't. You are wrong. If you can't program in Basic without writting spagetti code, then thats your deficiency. Why would that deficiency not carry over to other languages??? Is it because you were told that Basic does that and then you proceed to fullfill the prophecy?
 
I have used Visual Basic, and it is VERY different to C#. Have you ever used C#?

Now I am certain that you are completely uninformed (as in ignorant)

Both VB.NET and C# share the same features. They differ only in SYNTAX. Semi-Colons and different keywords. Its not debatable. It is exactly what Microsoft intended (their goal is to eventualy move everyone to the same language, controlled by them)
 
Nah, that's what function pointers are for. :D

Of course, a function implies a return (and stack usage.)

The anti-goto crowd is going against the godfather of algorithms, Donald Knuth.

He gives examples of two algorithmic situations where the common flow control methods (If, For, Do, Continue, Break, Case, etc..) are insufficient: http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf

He also mentions a bit of trivia in this paper about Dijkstra's paper.

A nice little quote from Knuth's paper:

Code:
We've already mentioned that go to's do not have a syntactic structure 
that the eye can grasp automatically; but in this respect they are no 
worse off than variables and other identifiers. When these are given a 
meaningful name corresponding to the abstraction (N.B. not a numeric 
label!), we need not apologize for the lack of syntactic structure.
 
Skylark,

I personally would _not_ recommend C++ at all. I vastly prefer C; it's a lot less obfuscated. If you need object oriented programming, use C#; it performs this task a lot more sanely. This is my personal preference though, obviously.
I have heard this type of argument many times, and I simply do not understand it. With a few very minor exceptions, any C code is also valid C++ code. So what advantage does C give over C++? If there are aspects of C++ that don't appeal to you, simply don't use them. All C++ does is give you more tools to work with. You don't have to actually use them.

I personally think that C++ is far superior to C for mathematical and scientific programming, because it provides a very efficient way to deal with the deficiencies that C has for those types of tasks. In fact, I don't even use the object oriented aspects of C++ for most of my work. My programs are procedural. But I use classes to build the specialized data types I need to work efficiently. One of the strengths of C++ is that it doesn't tie you down to an object oriented approach. Your program can very easily lie anywhere between being procedural and object oriented.


Dr. Stupid
 

Back
Top Bottom