• (cs) in reply to Fizzl
    Fizzl:

    My thoughts exactly. it should be "void setNull(char &variable)" to pass by reference.
    I shut (shat? wtf, english preparser is broken) my hole earlier[...]
    Shat is past tense for opening another hole.
    Fizzl:

    because I thought I was too drunk to remember my primary language. [...]
    Don't worry, it happens to the best of us.
  • nick (unregistered)

    "-3e4" is valid for parseFloat i think? at least its a valid number in vb.net, sometimes have to use own validation

  • Morbii (unregistered) in reply to Fizzl

    loneprogrammer:
    richleick:
    While we are reinventing the wheel, I thought I would share this little function that was written for a common C library.  It's been a while, so forgive any syntax errors.  But I'm sure you get the point.

    void setNull(char *variable)
    {
       variable = null;
    }

    Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.

    Personally, I thought that WAS the point... that it was in this library and did NOTHING - guess everyone else had a different take on it :O

    Fizzl:

    My thoughts exactly. it should be "void setNull(char &variable)" to pass by reference.
    I shut (shat? wtf, english preparser is broken) my hole earlier because I thought I was too drunk to remember my primary language.

     

    I am surprised at the number of people actually got this wrong :O

    Don't you mean void setNull(char &*variable);?

  • Morbii (unregistered) in reply to Morbii

    errr... char *&, rather

     

    <-- feeling dumb

  • loneprogrammer impersonator (unregistered) in reply to Morbii
    Anonymous:

    Fizzl:

    My thoughts exactly. it should be "void setNull(char &variable)" to pass by reference.
    I shut (shat? wtf, english preparser is broken) my hole earlier because I thought I was too drunk to remember my primary language.

     

    I am surprised at the number of people actually got this wrong :O

    Don't you mean void setNull(char &*variable);?


    Yes, but, if we are limiting the discussion to the C language, then no.  C++ has pass-by-reference with the & character, but C does not.

    In C, you would have to pass a pointer to the pointer that you wanted to set to null.  But what is easier, this . . .

    char *str;
    setNull(&str);

    or . . .

    char *str = NULL;

    ?

  • Morbii (unregistered) in reply to loneprogrammer impersonator

    Anonymous:
    Yes, but, if we are limiting the discussion to the C language, then no.  C++ has pass-by-reference with the & character, but C does not.

    In C, you would have to pass a pointer to the pointer that you wanted to set to null.  But what is easier, this . . .

    char *str;
    setNull(&str);

    or . . .

    char *str = NULL;

    ?

     

    Obviously it's easier to just set the pointer to NULL.  I was amazed at the amount of people who didn't understand how to properly write a function to do it, however.  My reply was specific to the person I quoted, who's answer was akin to setting a character to NULL using *C++* (which would throw a compiler error if you didn't cast NULL, assuming of course NULL is defined as ((void *)0) instead of just 0).

  • Xarium (unregistered) in reply to richleick
    richleick:
    void setNull(char *variable)
    {
       variable = null;
    }

    Given just this information, we can't know for certain that this is a worthless function.  Here's a scenario where it's justified;
    You have a function pointer that is used to point to the correct function for some behaviour that changes in non-deterministic but constrained way (this is the whole reason why C++ has "virtual" - because it happens a lot in C).  The function pointer is called frequently but occasionally it is not necessary for it to actually do anything.  Checking that the function needs to be called via an "if" condition will actually be slower most of the time because it has the potential to stall the CPU - this is especially important in FPS games.  So what do you do?
    Answer; you write a function that does nothing and reference that.
    The function must follow the declaration prototype of the pointer, so it will have that one parameter - but because the code doesn't reference it, the compiler will emit a warning message (about an unreferenced parameter) that looks like a mistake.
    variable = NULL does nothing, and the compiler knows this because there can be no alias.  The compiler optimizes the line out of existence but does not emit a warning anymore.
    The only WTF for this type of code is not leaving a comment that tells the maintenance programmer why the function appears to be pointless.

  • Ann Onymous (unregistered) in reply to Xarium

    Curiosity, only. . . 

    How many of you commenters are gainfully employed writing real programs?

    Has it occurred to anyone that there are compilor implemtations that require rolling your own?

    (ever tried a sprintf of more than 128 chars in Turbo C) ?

    Sigh.





  • Xarium (unregistered) in reply to Ann Onymous
    Anonymous:
    How many of you commenters are gainfully employed writing real programs?

    *waves*

    Actually, at the moment I don't do a real lot of programming myself - end up just operating as a consultant and fixing others disasters.
    Seems there is a point where one reaches a thershold and becomes more valuable directing others than doing the work themselves...  or some shit.

  • (cs) in reply to Xarium

    I write Javascript that mangles the DOM, and I write ASP/JScript and itbits of PHP that mangle the output strings.

    I don't consider myself a true programmer until I've actually written an OS application, or preferrably, a game -- since games are about behaviours and so is programming.

  • Marvin (unregistered)

    So, '...', '---' and even ';.-0;' are all valid Numbers, eh?

  • (cs) in reply to Ann Onymous
    Anonymous:
    Curiosity, only. . . 

    > How many of you commenters are gainfully employed writing real programs?

    Yes.

    Anonymous:

    Has it occurred to anyone that there are compilor implemtations that require rolling your own?

    (ever tried a sprintf of more than 128 chars in Turbo C) ?

    Is Compilor a villain from an 80s children's show?

    And the issue here is not JUST that the idiot rolled his own function for doing this, but that he did it in a horribly inefficient way, with a tool that could have done it a lot easier, AND to top it off the function doesn't really do anything useful, and certainly doesn't do what it's supposed to.

  • (cs) in reply to Ann Onymous
    Reun:
    Alex Papadimoulis:
      <font color="#000099">var</font> theRegExp = <font color="#666666">/[0;1;2;3;4;5;6;7;8;9;.;-]/</font>
    


    I don't use the language in question, but WTF? What kind of regular expression is that?

    Javascript, RegEx are specific objects using a perl-like syntax.

    That, or a string if you use the "new RegExp()" construct, but that one sucks, no one in his right mind would use it unless he has to.

    Anonymous:
    Has it occurred to anyone that there are compilor implemtations that require rolling your own?

    (ever tried a sprintf of more than 128 chars in Turbo C) ?

    Except that this is fucking javascript, not TurboC or whatever insane language/compiler you may enjoy, and this code therefore has roughly a WTF per line

  • !WTF Coder (unregistered) in reply to trollable

    Ah yes, laugh if you will, but I wrote my own number parser. The standard paraser will fail on something like "1,234.56", where a comma causes an exception.

    The parser gracefully handles things like:
    "-1,234.56" = -1234.56
    "1,234.56-" = -1234.56
    "1,,,,2,,3,,4,,5.6.7.8" = 12345.678
    "12oops34"= 1234
    "-123.34-" = -123.34

    and so on. Of course I deal with Web users.....

  • (cs) in reply to !WTF Coder

    Anonymous:
    Ah yes, laugh if you will, but I wrote my own number parser. The standard paraser will fail on something like "1,234.56", where a comma causes an exception.

    The parser gracefully handles things like:
    "-1,234.56" = -1234.56
    "1,234.56-" = -1234.56
    "1,,,,2,,3,,4,,5.6.7.8" = 12345.678
    "12oops34"= 1234
    "-123.34-" = -123.34

    and so on. Of course I deal with Web users.....

    Sweet Jesus.

    Well, I guess everyone has their own philosophy of what constitutes input validation. But I'd have second thoughts about silently rewriting a user input into, say, a quantity field which reads "100 ha ha no i really meant 3" as "1003".

    (Unless it doesn't really do that, in which case.... well, no, sorry, it still looks sketchy to me.)

  • (cs) in reply to masklinn
    masklinn:
    Except that this is fucking javascript, not TurboC or whatever insane language/compiler you may enjoy, and this code therefore has roughly a WTF per line


    Then it would be nice if it was explained right from the beginning. It's been now a week I'm lurking on this web site and I really miss the fact that there is barely no mention of the languages used. Some silly things in a language may not be that silly in other ones.

    What I can see is some people laughing at others while they have no clue about the programming language used and think with the one they use all the time.

  • greywar (unregistered) in reply to fmr

    What surprises me is that this is a WTF.  It DOES do some checking that a lot of the regular functions do not support.  Yes it does accept some bad input values.  That fact hardly makes it a WTF though.  Its entirely possible that in the context provided he just wants to avoid anything with basic text, but wants to accept values such as -9, and 9.5. 

    Without knowing the context this is used in it could be that this is a totally valid peice of code that works 100% of the time.

    Yes there are some c functions that would have made this work in a simpler manner, but again-thats hardly WTF worthy.

  • Kitsune (unregistered)

    Iit looks like JavaScript to validate a telephone number to me..  although if that is the case I'm surprised )( aren't included or stripped off . Besides not being very thorough, I'm not getting the WTF here in that context.

  • fatgeekuk (unregistered) in reply to richleick

    Sorry, but sometimes there are valid reasons to "re-invent" date formatting.

    Like for instance when working with VBA in Excel.

    Microsoft has an annoying habit of being "HELPFUL"

    for instance, in Excel, all date formats are in american format. ALWAYS, irrespective of what the locale settings are.

    BUT, if you try to interpret a date with a day greater than 12, the date format routine detects this and swaps the month and day. HOW HELPFUL...

    so, you write your code and test it on the 15th of the month, it goes through formal testing a week later on the 22nd and is rolled into production a week later on the 29th.

    Which gives you a day or so to "get out of dodge" before the fit hits the shan.

    Gee, Thanks M$!

    So, writing your own date formatting and validation can sometimes be your only defence.

  • fatgeekuk (unregistered) in reply to anon

    ^-?\d+.\d+$ How hard was that?

    Hmmm... If we ignore the unescaped "."

    42 is not a number.

    how about *-?\d+(.\d+)?$

  • fatgeekuk (unregistered) in reply to Omnifarious
    Omnifarious:
    GalacticCmdr:
    Yes, that is correct that C is strictly pass-by-value; however he did ask for forgiveness on syntax errors.

    He might have meant...

    void setNull(char **variable)

    {

       *variable = null;

    }

    Which when called as....

    char test = 't';
    setNull(&test);

    It should put a null into the test variable.

    I may be totally off-base on my function since I have not worked in C-land in many years.

    I believe most C compilers will error out on that code. &test is of type char *, not char **.

    char *test = "t";
    setNull(&test);

    The above would work. :-)

    Which illustrates nicely that C pointers are dangerous. Powerful, but dangerous.

Leave a comment on “Irregular Expressions”

Log In or post as a guest

Replying to comment #:

« Return to Article