Count 'em up



  • <FONT face=Arial color=#000000>My coworker's code (verbatim).</FONT>

    <FONT face=Arial color=#000000>How many can you find?</FONT>

    <FONT face=Arial color=#000000>//should not be used outside this control
    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
            public string Concatenate(string string1, string string2)
    <o:p></o:p>
            {
    <o:p></o:p>
                DateTime dt = Convert.ToDateTime(string1);
    <o:p></o:p>
                string str = string.Format("{0:MM/dd/yyyy h🇲🇲ss tt}", dt);
    <o:p></o:p>
                return str + " " + string2;

    <o:p></o:p>
            }<o:p></o:p>

    </FONT> 



    <blink>[mod - touched up tag-soup - PJH]</blink>


  • Wow. The preview sucks. 

    Sorry about formatting. Try again:

    //should not be used outside this control

    public string Concatenate(string string1, string string2)

    {

        DateTime dt = Convert.ToDateTime(string1);

        string str = string.Format("{0:MM/dd/yyyy h🇲🇲ss tt}", dt);

        return str + " " + string2;

    }

     



  • @jpaull said:

    //should not be used outside this control

            public string Concatenate(string string1, string string2)

            {

                DateTime dt = Convert.ToDateTime(string1);

                string str = string.Format("{0:MM/dd/yyyy h🇲🇲ss tt}", dt);

                return str + " " + string2;

            }

    FTFY.



    Here's my chain of reactions:

    @jpaull said:

    public string Concatenate(string string1, string string2)

    Okay, not seeing a problem so far, apart from maybe conventional failures or the fact that this probably shouldn't be in its own method... but nothing that would make me go...

    @jpaull said:

    DateTime dt = Convert.ToDateTime(string1);

    <font size="20">WTF!?!</font>



  • But everyone knows "Concatenate" means you should interpret one of the strings as a Date and then convert it back into a string!

    I was digging through the intern's code this morning and found an interesting method called PerformQuery(). All it does is draw some polygons on the screen. It doesn't query anything.



  • @jpaull said:

    <font face="Arial" color="#000000">My coworker's code (verbatim).</font>

    <font face="Arial" color="#000000">How many can you find?</font>

    <font face="Arial" color="#000000">//should not be used outside this control
            public string Concatenate(string string1, string string2)
            {
                DateTime dt = Convert.ToDateTime(string1);
                string str = string.Format("{0:MM/dd/yyyy h🇲🇲ss tt}", dt);
                return str + " " + string2;

            } </font>

    <font face="Arial" color="#000000"></font> 



    [mod - touched up tag-soup - PJH]

    Ok...

    1) Function name only vaguely relates to what the function does. Parameter names are equally mysterious. There's no Intellisense hinting (presuming C# and Visual Studio).
    2) One wonders why you don't just pass in a DateTime to start with, especially considering:
    3) Convert.ToDateTime is not wrapped in a TryParse, nor does it have any exception handling, and finding malformed date strings is very very common.
    4) (Although in this case, I suspect the date string would never be malformed because it's passed in by doing a DateTime.ToString()... am I right? Otherwise even a moron developer would have realized the need for error handling when this was exposed to natural data.) (Oh wait, it just occurred to me that the lack of error handling is probably why this "should not be used outside this control")
    5) DateTime has its own Format(), which would save some finger-leather (although that's a minor nitpick)
    6) Of course this entire function could be removed, and done in a single line wherever it's called... if the date format needs to be consistent, I'd put it in a const string and pass that into DateTime.Format() whenever needed.
    7) Normally I wouldn't recommend inlining something like this (assuming there's an actual need for it), but give the other gripes and poor naming it would actually make the code *more* readable to inline this.
    8) Edit: doh, just noticed... if it shouldn't be used outside this control, why the frelling shit is it declared "Public?"



  • @jpaull said:

    //should not be used outside this control
            public string Concatenate(string string1, string string2)
           

    Beat him to death for this comment (your co-worker of course)



  • @hallo.amt said:

    @jpaull said:

    //should not be used outside this control
            public string Concatenate(string string1, string string2)
           

    Beat him to death for this comment (your co-worker of course)

     

    I generally agree, but there are times comments like that are good. The Silverlight app I maintain is made up of a core project/API with a bunch of modules that can be imported and use the Core API to interact with the application. Sometimes Silverlight controls have to be public (I use internal where possible so modules can't access them, but for technical reasons that can't be done everywhere) so I usually comment the class with something saying "Don't use this outside the Core." Of course, TRWTF is Silverlight won't let you import internal controls into a XAML file, though you can do it using pure code.

    Hmm, that comment is a plain comment, not the triple-backslash XML doc thingy, so it's not even visible unless you open that code file. You wouldn't know not to use it because Intellisense won't show you that comment when you access the method.

     


  • 🚽 Regular

    @jpaull said:

    //should not be used outside this control
     

    Does that mean outside of this class, or outside of this package? If the former, declaring the function public is a wtf. If the latter, then depending on the language, internal or perhaps some other keyword might suffice.

    @jpaull said:

    DateTime dt = Convert.ToDateTime(string1);


    This, of course, is the 600lb gorilla.

    @jpaull said:

    string str = string.Format("{0:MM/dd/yyyy h🇲🇲ss tt}", dt);

    Depending on implementation (without context, I don't know for sure) the format should not be hard coded here, and instead come from another source, such as a settings file, localization, or at least a constant.

    @jpaull said:

    return str + " " + string2;

    Besides not expecting the first string to be converted to a date, I also do not expect concatenate to add a space in between.

    I propose the following. It's hard to know the really reasonable name for this function without any context, so I'll use my best judgement:

    [public|private|internal] string FormatDateLabel(DateTime dt, string suffix)

    {

        string str = string.Format(MySettings.DATE_FORMAT_DEFAULT, dt);

        return str + " " + suffix;

    }



  • @jpaull said:

    //should not be used outside this control
            public string Concatenate(string string1, string string2)
     

    It's what "private" and "protected" are for.

    @jpaull said:

                string str = string.Format("{0:MM/dd/yyyy h🇲🇲ss tt}", dt);

    Specifying a high precision for the datetime (to miliseconds, apparently) when the original date (passed in string1) probably does not have anywhere near this precision.  (I got into trouble with this all the time in MIDDLE SCHOOL -- measuring a value then putting it down on paper with more precision than I measured -- measuring 11 inches on a ruler, then specifying it as 11.0000 inches. )

    Not including string2 as part of the string.Format 

     



  • @dogbrags said:

    (I got into trouble with this all the time in MIDDLE SCHOOL -- measuring a value then putting it down on paper with more precision than I measured -- measuring 11 inches on a ruler, then specifying it as 11.0000 inches. )
    I should probably get therapy, because it makes my head explode every time I see newspaper write something like "... 20 miles (32.18688 km) ..." -- if the original measurement was approximated to 1sf, the converted figure should be as well. It's actually less accurate, because it implies an erroneous level of precision.



  • @intertravel said:

    I should probably get therapy, because it makes my head explode every time I see newspaper write something like "... 20 miles (32.18688 km) ..." -- if the original measurement was approximated to 1sf, the converted figure should be as well. It's actually *less* accurate, because it implies an erroneous level of precision.

    Therapy is not the first thing that comes to mind, you should see a doctor, run a show or try to get into the Guiness Record book. Hey if you have a video, please share it. We will use SSDS to view it to our heart content



  • @intertravel said:

    @dogbrags said:
    (I got into trouble with this all the time in MIDDLE SCHOOL -- measuring a value then putting it down on paper with more precision than I measured -- measuring 11 inches on a ruler, then specifying it as 11.0000 inches. )

    I should probably get therapy, because it makes my head explode every time I see newspaper write something like "... 20 miles (32.18688 km) ..." -- if the original measurement was approximated to 1sf, the converted figure should be as well. It's actually less accurate, because it implies an erroneous level of precision.

    In fact, that is exactly why many (imperial) people think that a tenth of a degree away from 98.6 °F is not a normal oral temperature. The original metric measurement only had two significant digits (37 °C). IIRC, it's something more like 98.2 +/- 1.3 °F. (Or in my dad's case, more like 97ish. 98.6 for him is approaching a fever.) Of course, it depends on how you measure, too.


  • 🚽 Regular

    @Xyro said:

    Of course, it depends on how you measure, too.
     

    And the time of day, and whether you just ate some popping hot soup or hot chocolate, or had alcohol, or ice cream...

    That being said, where are the people you speak of who somehow believe 98.7 is a fever and 98.5 is hypothermia? I haven't encountered that kind of paranoia before. Even when I was in middle school and a nurse would take my temperature, if it came out to be 98.8 degrees I knew the most she'll say is to come back later in the day to see if it gets worse.



  • @intertravel said:

    @dogbrags said:
    (I got into trouble with this all the time in MIDDLE SCHOOL -- measuring a value then putting it down on paper with more precision than I measured -- measuring 11 inches on a ruler, then specifying it as 11.0000 inches. )
    I should probably get therapy, because it makes my head explode every time I see newspaper write something like "... 20 miles (32.18688 km) ..." -- if the original measurement was approximated to 1sf, the converted figure should be as well. It's actually less accurate, because it implies an erroneous level of precision.

    The problem is really a psychological one. The conversion between miles and kilometers is exact, and 20 miles is indeed the same as 32.18688 kilometers. But when a human being [i]sees[/i] a nice round number like 20, he or she normally assumes that there is a certain natural amount of "slop" to it (technically, error, but one doesn't have to know the term to get a feel for it). One assumes that the reason the guy stopped writing down digits is because he became unsure of their values after that point. So it's completely fine to say that 20 miles equals 32.18688 kilometers [i]so long as you keep in mind some natural amount of "slop"[/i] in the measurement. Because most people are unable to remember that consistently, we make up a rule that we keep the same number of digits as we had before the conversion.

    But this isn't a law of nature, and it's only a [i]rough[/i] rule in the first place. For instance, a transformation of a quantity that isn't linear, such as taking the log or squaring it, [i]compresses[/i] some ranges of values and [i]stretches[/i] some other ranges of values. To get a true idea of how the error propagates, we need to use calculus, but for common phenomena over small ranges the linear approximation is good enough and the "significant figures" method works quite well.



  • @intertravel said:

    I should probably get therapy, because it makes my head explode every time I see newspaper write something like "... 20 miles (32.18688 km) ..." -- if the original measurement was approximated to 1sf, the converted figure should be as well. It's actually less accurate, because it implies an erroneous level of precision.

    I agree that 32.18688 km is way too precise (and now that you've mentioned it, my head will explode too every time I see something like this. Thanks! :-) )

    But IIRC 1 significant figure would be 30 km, which seems too imprecise. I'd be a bit squishy on this one, mathematically speaking, and use 32. Seems like a decent level of precision for 20 miles x 1.6 km/mile, even if it's not mathematically correct.


  • 🚽 Regular

    @smxlong said:

    The problem is really a psychological one. The conversion between miles and kilometers is exact, and 20 miles is indeed the same as 32.18688 kilometers.

    That's only true if you explicitly said "exactly 20 miles." (or, to be more correct, 20.00000 miles) If you say "approximately 20 miles" or "around 20 miles" you are using a level of precision that's only rounded to the nearest whole number (that is, assuming it isn't actually rounded to the tens place). Thus, all of your conversions must be in that whole number, so around 20 miles is the same as saying around 32km, 32000 meters. In scientific research, since all measuring devices can only be precise to a certain point, you can only express those values in those significant figures, everything else is grounds for miscalculations and other mistakes, and that includes making conversions. Now, obviously, that isn't such a disasterous thing for a Canadian who is following directions to some hotel in Chicago, but it's still a tad silly to say, "It's around 20 miles" to an American, but then give a Canadian this false sense of preciseness.



  • I like your term "slop" rather than "error".

    I learned in school that any measurement has a slop factor, usually not stated. 1.000000 miles is 1.609344 kilometers. So 20 (plus or minus .5) miles is 32.18688 (plus or minus 0.804672) kilometers. So 32 kilometers is reasonably good. Now if there were 20 (plus or minus 5) then 30 would bea better conversion.

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.

     



  • @AndyCanfield said:

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.

    Either that, or the temple fixture being described had no existence outside the imagination of the book's author, making any discussion of its precise shape moot.



  • @__moz said:

    @AndyCanfield said:

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.

    Either that, or the temple fixture being described had no existence outside the imagination of the book's author, making any discussion of its precise shape moot.

    Or it's in a PARALLEL UNIVERSE where our LAWS OF SPACE AND TIME do not apply! oooOOOooOOOooOOOooo



  • @AndyCanfield said:

    Of course that doesn't mean that PI was 3.00000
    Of course not. π is 4.


  • Garbage Person

    @AndyCanfield said:

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
     

    I know of at least one engineering professor who uses pi=3 in all his examples, and allows its use on tests because 'it makes the math cleaner, and my job is to teach you to be an engineer, not do decimal math. If you're doing real work and do the math yourself, you're already being criminally negligent.'


  • @Weng said:

    @AndyCanfield said:

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
     

    I know of at least one engineering professor who uses pi=3 in all his examples, and allows its use on tests because 'it makes the math cleaner, and my job is to teach you to be an engineer, not do decimal math. If you're doing real work and do the math yourself, you're already being criminally negligent.'

     

    In theoretical physics, pretty much, pi=1.



  • @cfgauss said:

    @Weng said:

    @AndyCanfield said:

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
     

    I know of at least one engineering professor who uses pi=3 in all his examples, and allows its use on tests because 'it makes the math cleaner, and my job is to teach you to be an engineer, not do decimal math. If you're doing real work and do the math yourself, you're already being criminally negligent.'

     

    In theoretical physics, pretty much, pi=1.

     

    In trig, pi = 180°.  In gematria, it's 80.



  • @AndyCanfield said:

    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
    Maybe it isn't circular. Anyways, the value of pi is always the same, regardless of curvature or anything else. Pi isn't only for circles. The circumference of a circle is one thing where the value of pi is used.



  • @zzo38 said:

    @AndyCanfield said:
    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
    Maybe it isn't circular. Anyways, the value of pi is always the same, regardless of curvature or anything else. Pi isn't only for circles. The circumference of a circle is one thing where the value of pi is used.

    It wouldn't change the value of pi, but sufficiently noneuclidean geodesics would change the measurements of the objects such that ratio of the measured circumference to the measured diameter would not be 3.14159265etc. ... Also, were they measuring the outer diameter or the inner diameter?



  • @Xyro said:

    @zzo38 said:
    @AndyCanfield said:
    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
    Maybe it isn't circular. Anyways, the value of pi is always the same, regardless of curvature or anything else. Pi isn't only for circles. The circumference of a circle is one thing where the value of pi is used.

    It wouldn't change the value of pi, but sufficiently noneuclidean geodesics would change the measurements of the objects such that ratio of the measured circumference to the measured diameter would not be 3.14159265etc. ... Also, were they measuring the outer diameter or the inner diameter?

    WTF is wrong with you people



  • @blakeyrat said:

    WTF is wrong with you people

    Put on your imagination hat and play along!



  • @Xyro said:

    @zzo38 said:
    @AndyCanfield said:
    This arises in the Bible, Old Testament, 2 Chronicles 4:2, where the circular "sea" has a circumference of 30 cubits and a diameter of 10 cubits. Of course that doesn't mean that PI was 3.00000 (which would require major gravitional curvature), but that the cubit measurement itself has a plus or minus one slop factor.
    Maybe it isn't circular. Anyways, the value of pi is always the same, regardless of curvature or anything else. Pi isn't only for circles. The circumference of a circle is one thing where the value of pi is used.

    It wouldn't change the value of pi, but sufficiently noneuclidean geodesics would change the measurements of the objects such that ratio of the measured circumference to the measured diameter would not be 3.14159265etc. ... Also, were they measuring the outer diameter or the inner diameter?

    If you measure the circumference of a circle and divide by the diameter and you don't get pi, it doesn't mean pi has changed, it means space must be curved. The constancy of pi is the whole reason you can say that.



  • @smxlong said:

    .....

    If you measure the circumference of a circle and divide by the diameter and you don't get pi, it doesn't mean pi has changed, it means space must be curved. The constancy of pi is the whole reason you can say that.

    Yes, that is it.



  • That was something that really annoyed me in Greg Bear's otherwise good [i]Eon[/i]. You want a pi-meter? Don't know what good it will do you, but hey, my calculator has one, so it can't be hard to build. On the other hand a device to measure space-time curvature would be a bit trickier.

    If you want pi to change, you have to come up with a way to make all of those different mathematical sequences that converge to it start converging to something else. I'm not quite sure how you'd manage that.



  • Getaway from Getawehi

    "In all my books, twice one is two - and it's never before been in dispute" "But your books were written on Terra, not Getawehi. On Getawehi, they don't apply" "But that's insane!". Mathematics is merely a system for expressing the properties and relationships of quantities. It's universal, not a local phenomenon. Once one is one, twice one is two...." "Not on Getawehi. It seems to be different here. Once one is one but twice one is only a bit over one and a half. One point five seven zero eight, to be more exact. And three times one is about two point three six."

    [...]

    "It's long been suspected that our mathematics may not be universal. Dimensionless numbers, for example, although have an accepted value in the part of the universe where we customarily use them, are more likely to be local coincidences than physical absolutes. But on Getawehi, we seem to have hit on something even more fundamental [...] It has something to do with unity." "Unity?"

    "Yes. Unity...one...a whole. I'm no mathematician but it seems to me there's a darn great hole in our idea of the structure of numbers. We've explored number structure up to infinity and several orders beyond - but something we've always taken for granted is the constant mathematical value of unity. "But it has to have a constant mathematical value!. Once one is one...it can't be otherwise by its very definition."

    "So we've always assumed. But what if we happened to be wrong? What if there is a difference between the value of one as representing a whole thing and the value of one as a mathematical factor? They both seem to be the same in our corner of the universe but one used as a factor on Getawhei is demonstrably only 0.5785 of what it was on Terra"


Log in to reply