Trying to find a decent stristr is demoralizing



  • [I already have what I consider a canonical implementation of stristr(), based on another canonical implementation of strichr(), but I wanted some opinions on why I found what I found.]

    First, search out all the published implementations of stristr() and strichr() you can find on the web.  Notice that, at an almost 100% level of occurrence, they're utter crap.

    For example, strichr() takes only two lines of C/C++ code, none of which are that "special magic" unreadable code you see in contests.  Why are all these goobers writing loops involving toupper() and all that jazz, when only two lines are needed?  (Ask yourself--why write code to do what the standard library already does?)

    Likewise, stristr() is an exercise in horror most of the places you find it...and many of the implementations you see are on message boards, in which the respondents manage to write even WORSE versions of the original poster's code, thinking that somehow they've optimized the code.

    My version of stristr() is 20 lines (I'm a white-space freak, though), and doesn't involve gory crap like iterating the entire target string and scads of toupper()/tolower() calls.  Do the CS curricula in colleges these days no longer include teaching programming as a craft, I'm wondering?



  • @mrprogguy said:

      Do the CS curricula in colleges these days no longer include teaching programming as a craft, I'm wondering?

    I don't remember that they ever did.

    I have a degree in CS and a degree in music, and I always found it fascinating (and an indication of each field's self-image) that the ultimate textbook in one field is titled "The Art of Computer Programming" and in the other field is titled "The Craft of Musical Composition".



  • What's up with those awful function names?



  • @newfweiler said:

    @mrprogguy said:

      Do the CS curricula in colleges these days no longer include teaching programming as a craft, I'm wondering?

    I don't remember that they ever did.

    You're very right. A CS degree is supposed to prep you for further study in computer science; it's not about programming. That said, I remember at least having learn quite a bit about determining and explaining execution properties for various algorithms. If I were building a function just for a class, I'd be concerned about things like comparing the whole string and whatnot. 

    That said, I'd be curious as to what the background for all of these functions is.... I mean, I only know a small handful of programmers that actually have CS background. I'd be inclined to think that a significant portion of anything I found online would be from people with any non-CS background.



  • @webzter said:

    You're very right. A CS degree is supposed to prep you for further study in computer science; it's not about programming.

    I'm going to have to take some exception to that.  While it's true that CS curricula are woefully short on 'programming as a craft' type classes, a solid understanding of how software actually works is absolutely essential if you want to get past the code-monkey stage of software development. 

    I don't think I'd be exagerating at all to say that the biggest WTFs I've seen out in the real world are the result of people who can't tell their ass from an algorithm.  They could sling code with the best of them, but they had absolutely no theory to guide it.   As a result you get conversations like this (an actual, if abridged, example):

    Dev A: "I don't understand, this system was really fast a couple months ago, now it takes all night to run a report!"
    Dev B: "That's because microsoft sucks"

    In actuality, they had a nasty n-cubed algorithm in their report generator and they went from a test data set of 100s of items to real data with 20,000+ items; but instead of recognizing it they blamed the technology.   They had just about convinced the CTO to spend a huge amount of money to move over to Oracle when I arrived and fixed the problem with about 5 lines of code.

    These guys weren't dumb, they weren't newbies.  They were experienced "engineers"...for what that term is worth these days.

    Another true story, actually 2; back in about '99 or so:

     I worked for a company that did hardware to network devices over the power lines.  They had a windows (MFC) app that you could use to 'program' the devices... one day while updating the app, it started flipping out, strange crashes ensued.   The dev poked at it for a couple weeks, debugging wasn't helping figure out the problem, finally the dev declared "It must be this crappy new Windows 98...microsoft sucks".   Well, it turns out that debugging was helping -- they just had no idea how to interpret what they were seeing because they had any idea how function calls worked at the assembly level.  The instruction pointer was clearly set to some wonky value (it may even have been 0), and (big surprise) the app was crashing.  Once you could see that the instruction pointer was messed up, it wasn't a long trip to finding the macro (this was C++) that was being called, and there was a mismatch in the number of arguments somehow (memory is growing foggy in my dotage).  Another small fix.

    Same company: The powerline networking used a protocol known as CEbus.  But not a single software 'engineer' bothered to actually know anything about it.  They had a library written by someone a few years previous and treated it as a black box...a crappy black box that spewed out multiple redundant messages and flooded the line, reducing the already crappy performance of CEbus to a crawl.   Why didn't they bother to learn more?  Because not one of them knew anything about communications protocols, or even farking BNF which is what a lot of the documentation used.   At least they didn't blame this one on microsoft.

    I could go on -- literaly for pages -- with mini-wtfs like this, almost all based around a lack of what I would consider fairly basic knowledge: how to write a decent algorithm, how data moves around on the processor, how to read BNF, some basic networking background.  Not rocket science...it's >gasp< computing science.  The science...of...computing.   Funny how that might come in handy when you program computers for a living.

    So, to end my tirade, I'd say that the purpose of a computing science degree is to expose the student just enough to concepts like this so that when they see a problem in the real world they might just have a chance to say "oh, wait, I recognize this..."  

    I completely agree that they are doing their students a disservice by not teaching more "software as a craft" knowledge.  I also think it's a farce that they are typically required to take a calculus class (which comes up pretty infrequently), but something like graph theory is optional and it shows up all over the place.  Universities do not do a good job of teaching for computing professionals in general... but to say that a CS background is only for scientists is pretty much a license to print WTFs.

    IMO :)

    -cw



  • @CodeWhisperer said:

    So, to end my tirade, I'd say that the purpose of a computing science degree is to expose the student just enough to concepts like this so that when they see a problem in the real world they might just have a chance to say "oh, wait, I recognize this..."  

    I completely agree that they are doing their students a disservice by not teaching more "software as a craft" knowledge.  I also think it's a farce that they are typically required to take a calculus class (which comes up pretty infrequently), but something like graph theory is optional and it shows up all over the place.  Universities do not do a good job of teaching for computing professionals in general... but to say that a CS background is only for scientists is pretty much a license to print WTFs.

    IMO :)

    -cw

    YMMV, of course. The smartest programmer I know was a EE drop-out who briefly traded commodity futures. The smartest DBA I've ever met graduated with a finance degree and started life as a stock broker. Heck, we've got a really sharp guy on our team right now who was a research psychologist before he decided he liked computers more.

    Personally, I started my college life as a history major before I decided that might not be my best future. I then created a custom <FONT face=Arial size=2>curriculum that skipped all of the math courses required of the CS majors while retaining all of the CS courses; more than the CS majors were required to take, actually. My custom major / minor also included all of the CIS classes required of the CIS people. As a result, I had a strong introduction to 'IT as a craft' from the CIS courses as well as 'learn to think about execution flow, data structures, etc' from my CS classes. Even then, most of my education came from my jobs. Still, to a degree, I probably followed a path that would be beneficial to people that wanted to be programmers and not just follow the PhD prep material of a </FONT><FONT face=Arial size=2>standard CS degree.</FONT>

    I suppose it's up for debate on if I can actually program or not ;-)

    Mini-story time

    <FONT face=Arial size=2>I got to debug a wonderful error we were getting when our developers launched their code to the QA environment. We had some huge HP-UX boxes with the appropriate Java hotspot loaded. The box kept core dumping with an out of memory exception. When we instrumented the box, we saw it incrementally lock memory until it just ran off the edge (attempting to acquire memory address 0xffffffffffffffe.... dump).</FONT>

    <FONT face=Arial size=2>The developers, all CS majors mind you, had written a recursive routine to walk a product catalog and build the appropriate nav bar on the side. The function performed beautifully in dev, where they had only loaded 100 products. However, we had just completed half of our data load in QA and so the product catalog was up to about 400,000 products. We taught them how to use iterators instead and they went on their merry way.</FONT>

    <FONT face=Arial size=2>Anyway, all of this is a long way of saying that stupid can exist no matter the background, as can smart.

    </FONT>


  • @webzter said:

    <FONT face=Arial size=2>Anyway, all of this is a long way of saying that stupid can exist no matter the background, as can smart.

    </FONT>

    Don't get me wrong, I absolutely agree...and in fact that I don't have a CS degree myself, or any degree for that matter.  I spent a lot of time in college, but got lured away by the promise of getting paid for a day's work :)   

    What I am arguing, however, is that it's not good to say "CS is only useful if you're going to study CS".  I don't really care if the person learns it in class or through a mentor or just by reading about it, but there is a real danger in saying (as our old pal CPound did once) that the "academic stuff" isn't important compared to just grinding the code out.

    -cw



  • @CodeWhisperer said:

    What I am arguing, however, is that it's not good to say "CS is only useful if you're going to study CS".

    That wasn't the statement. CS courses are only designed for studying CS at higher levels. You may well learn things from them that are useful in software development work - but that's more or less a coincidence, and much of the stuff taught in a CS course wouldn't be much use at all. Most of your complaints about CS courses are because you're expecting them to be useful for things other than CS research, while the people who set the course never intended them to be. The math courses are in there because they're critical to research work, and it's not important that they aren't much use for software development, because they aren't supposed to be.

    There are no undergrad-level courses aimed at software development because nobody wants to pay for them. Most undergrad courses are financed by the university, at a net loss, in order to provide a pool of recruits for research work (which can actually break even). They won't pay for any courses that don't accomplish this, as they can barely afford to pay for the ones they currently run (usually with severe dependence on donations). If you don't run the course in a university, where research work can pay for facilities, equipment, and staff (which can then be used by students), then it costs about five to ten times more, and that's just impractical.



  • @CodeWhisperer said:

    I don't really care if the person learns it in class or through a mentor or just by reading about it, but there is a real danger in saying (as our old pal CPound did once) that the "academic stuff" isn't important compared to just grinding the code out.

    -cw

    I don't think we're really that far off from each other. I agree that theory is extremely important to being a great programmer. Depending on your field, a base knowledge of theory ranges from "critical, can't perform without it" to "I'm a much better programmer because I can answer the 'why' as well as the 'how". I think that's the basis of your argument, that you can't be a good programmer without a foundation in computational theory... how you get that background isn't as important as the fact that you need it; higher ed, mentoring, self-taught, etc, the path doesn't matter as much as the destination.

    I'm definately on board with that. My only observation is that the theory side of programming is only a fraction of the equation. There's a practical and pragmatic side to this all as well. The "best" Ruby developer in the world (or the best .NET dev, best Java dev, etc) probably isn't the "best" computer scientist. a computer science is laying the foundation for you to be the best computer scientist in the world, not the best programmer. Obviously, not everyone that enrolls in a CS track is intent on pursuing a PhD, and many students will enroll just because they like computers and many of them will go on to careers as programmers (and, most of them will be in IT fields where they generally get to use all of that theoretical training in programming challenges on forums like this one; they'll rarely think about it in their day-to-day jobs).

    There are a lot of alternate paths to being a developer. In fact, many 4-year colleges have long recognized the potential market and have degrees that attempt to blend both worlds.


Log in to reply