Doing something every time is always faster than sometimes doing nothing...


  • Discourse touched me in a no-no place

    ...especially with your triangular wheel.



    Spotted this out of the corner of my eye while fixing a problem elsewhere in the library. The language is C:

    const char* ordSuffix(int num) {
    	 // make num always positive. This is faster than having to check for
    	 // negative cases.
    	 num = num < 0 ? -num : num; // same as num = abs(num); but faster, so there.
    	 // Numbers from 11 to 13 don't have st, nd, rd
    	 if(10 < (num%100) && (num%100) < 14) return "th";
    	 [snipped switch statement to return 'st', 'nd', 'rd' or ,'th']
    

  • ♿ (Parody)

    Hmmm....negative ordinals. Taking Asimov to the next logical step, I suppose, but unorthodox.



  •  Using a ternary when nothing happens in one of the cases is a pet peeve of mine. I swear they discover that operator and then they have to use it everywhere.


  • Discourse touched me in a no-no place

    @DOA said:

    Using a ternary when nothing happens in one of the cases is a pet peeve of mine. I swear they discover that operator and then they have to use it everywhere.
    None of the suspects who could have written this is 'new' and ?: shouldn't be a novelty for any of them. In fact I'm inclined to think that the prime suspect simply copy-pasted this code from somewhere without thinking, and thought it might get used elsewhere so put in in the library. (As opposed to what should be happening: upon noticing something being used twice or more, refactor a version to the library, and change the originals when something else needs doing to them (rather than immediatly changing all of them just because of the refactoring) - the latter method tends to reduce library bloat and duplication. Usually.)



    Apropos: There is, as I recall, an ex-(before I started)employee who was somewhat over-fond of using (nested, no less) ?: instead of if/then/else, and the resultant source was... well...

    Not really fit for an environment where there is more than one programmer. (Though I get the impression there was only 2, possibly 3, programmers then - it was a few years ago.)



    If I remember, and if it's still in the current codebase (though I'm certain it's obsoleted, as opposed to merely deprecated, and not actually being used) I'll dig some out and post it.


  • Discourse touched me in a no-no place

    @boomzilla said:

    Hmmm....negative ordinals. Taking Asimov to the next logical step, I suppose, but unorthodox.
    In light of my last reply, and reviewing the thread, it appears my earlier thoughts in reply boomzilla went missing (or I simply didn't post it.. which is more likely.)



    The library function concerned is used in a very small fraction of the total codebase that uses the whole library, and in the instances where it is used (including debug statements) are neither

    • time sensitive,
    • bottleneck/inner-loop suspects where the difference between ?: and abs() could ever be an issue or
    • where the parameter passed in would be (sensibly) negative

    In short, it's used for user presentation for one small aspect of the system where you can't have a negative number of 'foo'.

    Hence my copy/paste suspicion mentioned in my last post.

    (And the aforementioned absence of my un-posted suggestion that "simply requiring the parameter to be unsigned instead of signed, and requiring callers that may require its use on negative values to figure out the signage before/when calling it would be better.")

Log in to reply