Functions Considered Harmful



  • Let me start out by saying this is not a flame, or a personal attack.  This is just me posting a link to some of the worst code I've seen in my life, which is pretty much the whole point of this site.

    Okay let's just say there's this guy who wrote this "search engine" and he posted some of the code to his website.  This code could hypothetically be found here.

     It would be helpful to have some sort of search engine to search through the code for this search engine, because the code structure can best be described as a large bowl of spaghetti.  One of the benefits of this coding style is that you get the speed of VB6 and the clarity of assembly language all rolled into one.

    Some highlights:

    1. No functions, but plenty of gotos and labels.  All of the labels are given helpful names such as "line_13012".

    2. A surprisingly large amount of comments, the majority of which are completely useless.  Some of my favorites:

        array_aaa(array_pos) = " " + Left(aaa, tt - 1) + " "
        array_ooo(array_pos) = " " + Left(ooo, tt - 1) + " "
        aaa = Mid(aaa, tt)
        ooo = Mid(ooo, tt)
        xxx = Mid(xxx, tt)  'january 21 2001


     and

    If III < 1 Then GoTo line_13628i        'should never happen

    3. Not one, but two completely useless replacements for a boolean:

    match_flag = "YES"

    ...

    match_flag = "NO"


    and (notice anything wrong with this one?)

    If sscreen_saver = "Y" And sscreen_saver = "N" Then


     

    If you listen closely while you read the code, you can almost hear Dijkstra weeping.



  • Here's the link to the code.  It got cut out of my post when I edited it and now I can't edit it anymore.



  • Somehow, my earlier post linking to the sourcecode got deleted...

    The author of the program even followed up my post with the flow-charts, so it's not like he was objecting..

     



  • @Yorinaga said:

    Somehow, my earlier post linking to the sourcecode got deleted...

    The author of the program even followed up my post with the flow-charts, so it's not like he was objecting..

     I saw the flowcharts, and I was wondering why nobody had posted the code.  It would be a shame if this one gets deleted because that code is more genuinely WTF than 90% of the stuff that gets posted here.



    So let me get this straight:  A guy comes to a forum about bad code and posts an off-topic post complaining about censorship.  The members of the forum post on-topic bad code from the guy's public website, and they get censored?  My irony detector is off the charts.
     



  • @burnmp3s said:

    Here's the link to the code.  It got cut out of my post when I edited it and now I can't edit it anymore.

    Maybe it's not fair because he's not a professional, but, hey, even Alex posted student code yesterday!  I read the description on what this thing is supposed to do, and it made no sense.  Now I find that the code makes even less sense.  I'm not entirely sure I could understand this code if I had a stack of paper to trace it out. 



  • For some reason I'd choose to use a combination of "find", "grep" and others on a one-liner for this...

    But that's just me. 



  • @Yorinaga said:

    Somehow, my earlier post linking to the sourcecode got deleted...

    The author of the program even followed up my post with the flow-charts, so it's not like he was objecting..

    I hope you know the reason why I deleted those other threads. BTW, the code looks like it was originaly made for the line-number oriented home computer BASIC of the 80's, The author probably learned programming on a C64, Atari 800XL or Acorn BBC and never quite made the switch to structured programming.



  • @burnmp3s said:

    So let me get this straight:  A guy comes to a forum about bad code and posts an off-topic post complaining about censorship.  The members of the forum post on-topic bad code from the guy's public website, and they get censored?  My irony detector is off the charts.

    The code is a WTF, no question; but I don't want the discussion go personal. This thread here looks ok. BTW, here are the flowcharts:

    http://www.telusplanet.net/public/stonedan/pict01.jpg

    http://www.telusplanet.net/public/stonedan/pict02.jpg



  • @burnmp3s said:

    @Yorinaga said:

    Somehow, my earlier post linking to the sourcecode got deleted...

    The author of the program even followed up my post with the flow-charts, so it's not like he was objecting..

     I saw the flowcharts, and I was wondering why nobody had posted the code.  It would be a shame if this one gets deleted because that code is more genuinely WTF than 90% of the stuff that gets posted here.



    So let me get this straight:  A guy comes to a forum about bad code and posts an off-topic post complaining about censorship.  The members of the forum post on-topic bad code from the guy's public website, and they get censored?  My irony detector is off the charts.
     

     

    You do realize that his post[s] and his site are a joke, right?  Right??? 



  • @Morbii said:

    @burnmp3s said:
    @Yorinaga said:

    Somehow, my earlier post linking to the sourcecode got deleted...

    The author of the program even followed up my post with the flow-charts, so it's not like he was objecting..

     I saw the flowcharts, and I was wondering why nobody had posted the code.  It would be a shame if this one gets deleted because that code is more genuinely WTF than 90% of the stuff that gets posted here.



    So let me get this straight:  A guy comes to a forum about bad code and posts an off-topic post complaining about censorship.  The members of the forum post on-topic bad code from the guy's public website, and they get censored?  My irony detector is off the charts.
     

     

    You do realize that his post[s] and his site are a joke, right?  Right??? 

     If that's what you need to tell yourself to sleep at night...
     



  • so this is the link (http://www.telusplanet.net/public/stonedan/code.txt) to the source code ... ahmmmm

    i wonder why it looks "kwazy" 



  • Even most stoned programmers can manage to throw in a function every now and then...

    -cw



  • true



  • @ZeuC said:

    so this is the link (http://www.telusplanet.net/public/stonedan/code.txt) to the source code ... ahmmmm

    i wonder why it looks "kwazy" 

    Um, why not http://www.telusplanet.net/public/stonedan/code.txt for the nick name "Stone Dan"?

    What I mean is, this could mean everything or nothing. No hint for intentional irony for me.
     



  • Author a genius!

    Perhaps we're all missing that the author is really trying to provide a bad example.

    I suspect that the code has been ingeniously designed to demonstrate worst-practice.

    ...yes... that has to be it... this sort of code doesn't just come about from incompetence... 



  • @Yorinaga said:

    Perhaps we're all missing that the author is really trying to provide a bad example.

    I suspect that the code has been ingeniously designed to demonstrate worst-practice.

    ...yes... that has to be it... this sort of code doesn't just come about from incompetence... 


    I did that once - I hated Delphi and wanted to show everyone what a bad language and IDE it was and so a simple notepad-like app had gotos, no comments, obscure names and horrible indentation.

    OTOH I saw a MATLAB program that was written by a guy who didn't know how to use functions or auto-indentation... It was a mess, but not intentional.



  • @Yorinaga said:

    Perhaps we're all missing that the author is really trying to provide a bad example.

    I suspect that the code has been ingeniously designed to demonstrate worst-practice.

    ...yes... that has to be it... this sort of code doesn't just come about from incompetence... 

    I'm considering the possibility that the code, as well as some of the other post from that author, are nothing but a sophisticated troll attempt. 



  • @ammoQ said:

    @Yorinaga said:

    Perhaps we're all missing that the author is really trying to provide a bad example.

    I suspect that the code has been ingeniously designed to demonstrate worst-practice.

    ...yes... that has to be it... this sort of code doesn't just come about from incompetence... 

    I'm considering the possibility that the code, as well as some of the other post from that author, are nothing but a sophisticated troll attempt. 

    If it is, he's been doing it since at least early last year.  If you google SpectateSwamp or Spectate Swamp Search you will find all kinds of evidence that he's been putting a serious amount of effort into the program.  Who goes to the trouble of writing a program as a joke, and then spends a year promoting it, submitting it to shareware sites, and answering mundane questions about it?  I'm afraid the most likely conclusion is that SpectateSwamp actually writes code that looks like that.



  • My vote for representative line is this bit

     line_13640:
        'back to the logic after 12120 line
        GoTo line_12120

    1) It's got gotos.

    2) Redundant comment (I suppose we should be thankful there were comments at all).

    3) A label which leads to a goto.  Why not just replace GoTo line_13640 with GoTo line_12120.

    OK, I admit I only read the first dozen lines & the last dozen so there may be better bits in the middle but there's only so much I can take.  Is it time for a comment about inefficacy of eyewear yet?



  • Well, what's wrong with it. It works great. Take a look.


     



  • @ahf said:

    Well, what's wrong with it. It works great. Take a look.


     

     

    Why does his search program feature a picture slideshow? 



  • My all-time favorite has to be the comment 'should never happen.  Anytime I see this, I know I'm in for an extra-special maintenance contract.



  • @Yorinaga said:

    ... that has to be it... this sort of code doesn't just come about from incompetence... 

    It comes about from only having programmed in BASIC.

    -cw

     



  • @sir_flexalot said:

    My all-time favorite has to be the comment 'should never happen.  Anytime I see this, I know I'm in for an extra-special maintenance contract.

    Well, I have such comments in some of my programs too. In most cases, this checks for some condition that would have disastrous effects if I didn't catch it, but I do not currently know how this condition could be triggered. Many programming languages offer a feature called assertion to make sure that things that should never happen at least never happen unnoticed.



  • @CodeWhisperer said:

    @Yorinaga said:
    ... that has to be it... this sort of code doesn't just come about from incompetence... 

    It comes about from only having programmed in BASIC.

    Even people who only program in BASIC can make the mental tansition MS-Basic->QBasic->VisualBasic->VB.net, and also switch from spagetti code to structured programming and eventually to OO programming. It takes a special kind of stubborness to write original spagetti code two decades after the last computer that supported this programming style was built.



  • @ammoQ said:

    Even people who only program in BASIC can make the mental tansition MS-Basic->QBasic->VisualBasic->VB.net

    That's why I said "BASIC", in all caps no less.  Makes me pine for my TRS-80 days. :)

    -cw



  • @sycro said:

    @ahf said:

    Well, what's wrong with it. It works great. Take a look.


     

     

    Why does his search program feature a picture slideshow? 

     
    Because that way you can take many pictures and play them sequentially and it looks like a movie (I'm sure the flickering problem will be solved in a future version). Way better than making little drawings on your notebook and flipping through them...  Isn't this what computers are for???

     



  • @ahf said:

    @sycro said:
    @ahf said:

    Well, what's wrong with it. It works great. Take a look.


     

     

    Why does his search program feature a picture slideshow? 

     
    Because that way you can take many pictures and play them sequentially and it looks like a movie (I'm sure the flickering problem will be solved in a future version). Way better than making little drawings on your notebook and flipping through them...  Isn't this what computers are for???

     

     

    But.... it's a search program... 



  • @CodeWhisperer said:

    @ammoQ said:

    Even people who only program in BASIC can make the mental tansition MS-Basic->QBasic->VisualBasic->VB.net

    That's why I said "BASIC", in all caps no less.  Makes me pine for my TRS-80 days. :)

    -cw

    I still have a "laptop" TRS-80.  Seven whole lines of LCD goodness, and runs on only four AA batteries.



  • @sycro said:

    But.... it's a search program... 

    Next year he'll add word processor functionality to it.   It'll be the only piece of software your computer needs!

    -cw



  • @CodeWhisperer said:

    @sycro said:

    But.... it's a search program... 

    Next year he'll add word processor functionality to it.   It'll be the only piece of software your computer needs!

    -cw

    Oblig. Virtudyne quote: "Of course we'll do it in six months. We'll use Visual Basic 6.0" 



  • I don't think it's any mistake that OO sounds like uh-oh.



  • @sycro said:

    @ahf said:
    @sycro said:
    @ahf said:

    Well, what's wrong with it. It works great. Take a look.

    http://www.dropshots.com/day.php?userid=8192&cdate=20050901&ctime=140156


     

     

    Why does his search program feature a picture slideshow? 


    Because that way you can take many pictures and play them sequentially and it looks like a movie (I'm sure the flickering problem will be solved in a future version). Way better than making little drawings on your notebook and flipping through them...  Isn't this what computers are for???

     

     

    But.... it's a search program... 

    Yes its a search program, that searches through multimedia files:

    "Spectate Swamp - Video Search Engine & Multi-Media Player"

    I can't understand why anyone uses a word processor, or spreadsheet program, just do the following:

    "Reports are seldom run more than once. Do a screen capture and keep that.
    Same with word processors and other obscure formats. Do a screen capture or
    video those screens, along with a cut and paste of the text, then ditch them."

     



  • @sycro said:

    @ahf said:
    @sycro said:
    @ahf said:

    Well, what's wrong with it. It works great. Take a look.


     

     

    Why does his search program feature a picture slideshow? 

     
    Because that way you can take many pictures and play them sequentially and it looks like a movie (I'm sure the flickering problem will be solved in a future version). Way better than making little drawings on your notebook and flipping through them...  Isn't this what computers are for???

     

     

    But.... it's a search program... 

     

    But it's flickering pictures!!!! You can even do 360 degree panoramic movies with it!! It's like the surprise in the cereal box.. You can't eat it, but it's cool. :P (Just as cool).



  • Spectate Swamp Search kicks Butt.

    How come my "report abuse" button doesn't work. Some of that stuff was pure mean.

    The code has been patched since 1999 so it scares me to. Not to worry I have lots of

    comments and deactivated print statements, I will reactivate them if I need em.

     

     spaghetti code maybe. But I can dive in after being away from the code for a year

    or more and know there are lots of crumbs left behind.

     

    Most most of the comments are just too funny.

     

     

     

     

     



  • Ok this is an old post...

     

    But i actually looked at the site...

    And he even dared to post a link to his forum posts on his site...

    They are quite funny...



  • @ammoQ said:

    ... The author probably learned programming on a C64, Atari 800XL or Acorn BBC and never quite made the switch to structured programming.

    Humbug. The BBC Microcomputer had structured BASIC ;) That was one of the novelties of BBC BASIC, along with the inline macroassembler and primitive heap allocation. Just no deallocation, which was ... interesting. I later realised that one of my own apps leaks memory due to mistakenly assuming that heap allocations are freed once the pointer variable falls out of scope. Definitely doesn't. You really don't want to allocate local memory in a function, since it's not possible and will leak.

    IIRC, the BBC Micro's manual discourages the use of GOTO in favour of procedures and functions. Something I later realised was that functions in BBC BASIC have no return type. QBasic functions are typed; BBC BASIC functions are untyped. This code is valid:

     1000 DEF FNfoo(x)
     1010  IF x = 1 THEN =42 :REM return an integer
     1020  IF x = 2 THEN =0.5 :REM return a float
     1030  ="You didn't pick 1 or 2" :REM return a string otherwise

    Procedures were delimited by DEF PROC and END PROC. Functions were delimited by DEF FN and =, where = is the same as return. (MacASP, curiously, used a unary = instead of respose.write, so = "hello" would write "hello" into the output stream). The idea of a function delimited by its return is quite weird. Technically, functions are undelimited, since you can have conditional returns and multiple returns. Thus, there can be no formal end to a function definition.

    BBC BASIC is adorable.



  • @Daniel Beardsmore said:

    @ammoQ said:

    ... The author probably learned programming on a C64, Atari 800XL or Acorn BBC and never quite made the switch to structured programming.

    Humbug. The BBC Microcomputer had structured BASIC ;) That was one of the novelties of BBC BASIC, along with the inline macroassembler and primitive heap allocation. Just no deallocation, which was ... interesting. I later realised that one of my own apps leaks memory due to mistakenly assuming that heap allocations are freed once the pointer variable falls out of scope. Definitely doesn't. You really don't want to allocate local memory in a function, since it's not possible and will leak.

    In the context of the BBC, with no capacity for multitasking, you have to realise that there is no useful purpose in returning memory to the operating system, since nothing but your currently running program can use it. Therefore, it was presumed that deallocation was unnecessary. You are expected to reuse the memory that you allocated, by maintaining your own list of "allocated but currently unused" blocks. This was actually easier than you'd expect, given the relatively low complexity of most BBC BASIC programs.



  • @asuffield said:

    In the context of the BBC, with no capacity for multitasking, you have to realise that there is no useful purpose in returning memory to the operating system, since nothing but your currently running program can use it. Therefore, it was presumed that deallocation was unnecessary. You are expected to reuse the memory that you allocated, by maintaining your own list of "allocated but currently unused" blocks. This was actually easier than you'd expect, given the relatively low complexity of most BBC BASIC programs.

    I disagree. This is BASIC after all, a language where everything is supposed to be made easy for you :) I think proper discussion of the use of heap allocation was something that fell outside of the scope of the manual like many other things. (Many more things still fell out of scope of the Advanced User Guide, like just about anything to do with the 1 MHz bus.) When you got over to assembly you realised you had to write your own multiply and divide should your code ever need it, which startled me a bit. Fortunately it all came too late for me to have to learn 6502 assembly.

    What I imagine is that a proper heap manager was too complicated to fit into the 16 k BASIC ROM, so they didn't bother. I don't know to what extent BASIC itself needed to use the heap and whether it had a deallocator itself that user code could have made use of. BASIC 1 and 2 had all sorts of annoying limitations (I'd ask for block IF before I wanted heap deallocation), but the curious thing is how nice it had appeared to have become by version 5. Better than QBasic, although QBasic hinted that QuickBasic -- the complete version without all the goodies extricated -- was pretty nice. I did get QuickBasic and tried to compile Gorillas with it, but it keeps displaying gibberish and crashing. No idea.



  • @Daniel Beardsmore said:

    Better than QBasic, although QBasic hinted that QuickBasic -- the complete version without all the goodies extricated -- was pretty nice. I did get QuickBasic and tried to compile Gorillas with it, but it keeps displaying gibberish and crashing. No idea.
    There wasn't that much difference between QBasic and QuickBasic 4.5 (which it's based on) - IIRC, the only removed things were support for calling interrupts directly (but that could be worked around) and compilation. Also, for me Gorrilas compiled just fine (and ran considerably faster on 286).



  • Worse than that. I use line numbers for error trapping?

    I hated it when they took out line numbers.

     

    With my code. I put them back in, my own way.

    Saving a line number string at each key point. Then use that

    as part of the centralized error trapping.

     

    Spaghetti code is OK if you can flowchart. The logic was close close to working but not quite

    I revised the flowchart a number of times over the 3 days it took to resolve.

     

     



  • @ender said:

    There wasn't that much difference between QBasic and QuickBasic 4.5 (which it's based on) - IIRC, the only removed things were support for calling interrupts directly (but that could be worked around) and compilation. Also, for me Gorrilas compiled just fine (and ran considerably faster on 286).

    Well, QBasic pretended it was modular, but the only "module" you could have in memory was the main .bas file. I imagine QuickBasic was properly modular, allowing for code abuse^H^H^H^H^Hre-use. The snag with QuickBasic might be that it just doesn't like being run under NTVDM. FreeBASIC can be used instead, to compile the program for Win32, but that's another bundle of WTFs.



  • @SpectateSwamp said:

    Spaghetti code is OK if you can flowchart.



  • @Alchymist said:

      3) A label which leads to a goto.  Why not just replace GoTo line_13640 with GoTo line_12120.

     I used to see this a lot in Basic of the early 80s   Something tells me this guy learned Basic on a Commodore 64 and never got any farther.



  • @sir_flexalot said:

    My all-time favorite has to be the comment 'should never happen.  Anytime I see this, I know I'm in for an extra-special maintenance contract.

    I find myself adding those types of remarks to code that I'm maintaining.  For instance, switching on the protocol of a URI retrieved from a FileInfo object in .NET and handling protocols like HTTP in addition to (the only possible outcome) file.



  • I hated it when they took out line numbers.

     

    With my code. I put them back in, my own way.

    Saving a line number string at each key point. Then use that

    as part of the centralized error trapping.

     

    Spaghetti code is OK if you can flowchart. The logic was close close to working but not quite

    I revised the flowchart a number of times over the 3 days it took to resolve.

     

    You're an idiot.



  • @RaspenJho said:

    @SpectateSwamp said:

    Spaghetti code is OK if you can flowchart.

    Quoted for QFT. 



  • Line numbers can be handy, but they seem to bug people.

    @dhromed said:

    @RaspenJho said:

    @SpectateSwamp said:

    Spaghetti code is OK if you can flowchart.

    Quoted for QFT. 

    The flowcharts for the above spaghetti is:

    http://www.telusplanet.net/public/stonedan/pict01.jpg

    http://www.telusplanet.net/public/stonedan/pict02.jpg

    It works. Follow it through. I put line numbers on the charts too.



  • @SpectateSwamp said:

    @dhromed said:
    @RaspenJho said:

    @SpectateSwamp said:

    Spaghetti code is OK if you can flowchart.

    Quoted for QFT. 

    The flowcharts for the above spaghetti is:

    http://www.telusplanet.net/public/stonedan/pict01.jpg

    http://www.telusplanet.net/public/stonedan/pict02.jpg

    It works. Follow it through. I put line numbers on the charts too.

    Why can't you understand that no one will ever help you or follow your code?

    All we will ever do is point and laugh. You will never change that.



  • RANDOM Desktop Search FREES your data

    @MasterPlanSoftware said:

    Why can't you understand that no one will ever help you or follow your code?

    All we will ever do is point and laugh. You will never change that.

    At least I got you looking at the source. Maybe 1 or 2 of you have tried to build a new exe. I hope so.


Log in to reply