Time Flies

  • ColdHeart 2012-12-12 08:12
    Ahh, the wonders of bugs turning into features...
  • Andrew 2012-12-12 08:16
    I once had to work on a system where the timing for a scheduled execution was done by subtracting 900 from a counter. The counter was initialized to 86400 (total seconds in 24h), and every 15mins it subtracted 900 from that number.

    Reaching zero didnt necessarily mean it actually executed; the condition's sometimes slipped to FALSE and the counter went far into the negatives.

    Resetting timing involved working out how many 15min increments we were away from what was considered 'scheduled' execution time. I managed to replace it, at least.
  • johnckirk 2012-12-12 08:18
    if(msec==100)
    
    {
    msec=0
    sec--
    }


    I always thought that there were 1000 milliseconds in a second.
  • Frank 2012-12-12 08:20
    What happened to the zeroth comment?
  • Ralph 2012-12-12 08:23
    Instead of futzing around with the status bar, wouldn't it be more in keeping with modern script usage and GUI design to have the timer appear in a pop-up every second? Besides, that would make the page "more interactive" which is always a good thing right?
  • Rodnas 2012-12-12 08:26
    Frank:
    What happened to the zeroth comment?


    you mean the zreoth comment.
  • Myself 2012-12-12 08:27
    setTimeout(func(),1) ??

    setTimeout(func(),1000) ??
  • Rodnas 2012-12-12 08:27
    "Time flies like an arrow, fruit flies like a banana" - G. Marx
  • letatio 2012-12-12 08:29
    Who the hell indents like that?
  • Pietie 2012-12-12 08:34
    "Time flies like an arrow. Time birds like a bullet." - ??
  • Jeff 2012-12-12 08:34
    An indentured servant?
  • Kent 2012-12-12 08:35
    You see, early on, there was a bunch of deadlocking as it gained more widespread use. So, to make sure that users kept their time to a minimum, I added a sense of urgency
    Code smell is often merely a hint emanating from the buried corpse of rational thought. When the brave, armed with suitable quantities of holy water, detect code smell and pursue it to its source, the genesis of abomination is likely to be found.

    If your database is having deadlock problems, the solution is to give users 40 minutes (your mileage may vary) to submit the form?

    First of all, deadlocks arise from two (ignorantly written) updates occurring at roughly the same time. Not 40 minutes apart.

    Second, if your database is lumbering under the burden of 100 updates per day, the solution is not to prod the users to submit 100 updates faster. I'll leave it to the geniuses who haunt this board to work out The Real Answer but That's Not It (TM).
  • Zreoth 2012-12-12 08:35
    Rodnas:
    Frank:
    What happened to the zeroth comment?


    you mean the zreoth comment.

    Sorry, I was busy. Now that I'm not Frist, though...
  • silverwizard 2012-12-12 08:36
    The Full Article:
    To be sure, yes, i was a web page


    And now we see that Mark is just a web page, silently generating WTFs for us to read.
  • Bring Back Nagesh 2012-12-12 08:37
    letatio:
    Who the hell indents like that?

    Probably the same kind of person who thinks there are 59 seconds in a minute, and thinks the browser, let alone the operating system, can keep up with timer events at a granularity of 1ms and remain accurate.

    Captcha: validus - both the above assumptions will become validus in the future, as computers become even faster, and the economy gets so bad that we have to sell our zeroth second to China.
  • Oscar 2012-12-12 08:42
    Kent:
    if your database is lumbering under the burden of 100 updates per day
    I missed the part where they said it was running under Peoplesoft.
  • Mark Bowytz 2012-12-12 08:56
    Actually, I'm closer to an implementation of ELIZA.
  • dkf 2012-12-12 08:58
    letatio:
    Who the hell indents like that?
    I used to work with someone who indented like that. As far as I could work out, it was a style they learned when they started out on FORTRAN (but I don't know which version, 66 or 77)…
  • mike5 2012-12-12 09:02
    Implementing a countdown timer by separating the time into hours, minutes and seconds? And then writing convoluted (and erroneous) logic to count down to zero?

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?

    So people know how to debug deadlocks, but are unable to perform simple arithmetics?
  • ColdHeart 2012-12-12 09:10
    msec++
    if(msec==100)
    {
    msec=0
    if(sec==0)
    {
    sec=59
    min--
    }
    else
    {
    sec--
    }

    Seems like the correct version didn't need much changing.
  • g 2012-12-12 09:13
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats

    In some web-survey code I inherited I found code which measured the time people took for each question with setTimeout(function() { time += 1},1); And divided the resulting time by 100 to get the number of seconds, I guess they used 100 just to get the values to "look" right.

    It looked right(ish) due to most browser's timer-resolution being around 10ms (they're mostly in the 4-16ms range).
  • urza9814 2012-12-12 09:18
    Alan apparently didn't watch the timer very long, since he missed the part where at 5 minutes remaining it resets to 40 and stops counting down...

    Wish the timer for our internal pages did that. It's got a five minute timeout. So obnoxious...
  • Doctor_of_Ineptitude 2012-12-12 09:27
    Kent:
    You see, early on, there was a bunch of deadlocking as it gained more widespread use. So, to make sure that users kept their time to a minimum, I added a sense of urgency
    Code smell is often merely a hint emanating from the buried corpse of rational thought. When the brave, armed with suitable quantities of holy water, detect code smell and pursue it to its source, the genesis of abomination is likely to be found.

    If your database is having deadlock problems, the solution is to give users 40 minutes (your mileage may vary) to submit the form?

    First of all, deadlocks arise from two (ignorantly written) updates occurring at roughly the same time. Not 40 minutes apart.

    Second, if your database is lumbering under the burden of 100 updates per day, the solution is not to prod the users to submit 100 updates faster. I'll leave it to the geniuses who haunt this board to work out The Real Answer but That's Not It (TM).


    If you had read the article, you would have realized that the original coder had realized and fixed the deadlocks. So you can take your snobbish Real Answer and basically *uck it.
  • RichP 2012-12-12 09:27
    Andrew:
    I once had to work on a system where the timing for a scheduled execution was done by subtracting 900 from a counter. The counter was initialized to 86400 (total seconds in 24h), and every 15mins it subtracted 900 from that number.

    Reaching zero didnt necessarily mean it actually executed; the condition's sometimes slipped to FALSE and the counter went far into the negatives.

    Resetting timing involved working out how many 15min increments we were away from what was considered 'scheduled' execution time. I managed to replace it, at least.


    Pshaw. Your code was clearly doing:
    if (timer == 0) ...
    The fix was clearly:
    if (timer <= 0) ...

    No maths required.
  • Steve The Cynic 2012-12-12 09:30
    Mark Bowytz:
    Actually, I'm closer to an implementation of ELIZA.

    It's better than being like PARRY, I suppose...
  • anonymouse 2012-12-12 09:32
    It's not the "speed of the computer", it's that setTimeout has a minimum time associated with it.

    setTimeout("start()",1)

    I believe it used to be a 30ms minimum and anything below would default to that. I think chrome does 15ms, not sure on the others anymore.
  • Singular 2012-12-12 09:33
    RichP:
    No maths required.
    Yes, but if maths were allowed, could you do it using only three of them?
  • Code Slave 2012-12-12 09:41
    letatio:
    Who the hell indents like that?


    My co-worker... they could definitely benefit from a slight tap on the cranium with the clue-bat.
  • TGV 2012-12-12 09:55
    Rodnas:
    "Time flies like an arrow, fruit flies like a banana" - G. Marx

    And of course: time flies with a stop watch (Hint: it's an imperative).
  • Andrew 2012-12-12 10:09
    RichP:
    Andrew:
    I once had to work on a system where the timing for a scheduled execution was done by subtracting 900 from a counter. The counter was initialized to 86400 (total seconds in 24h), and every 15mins it subtracted 900 from that number.

    Reaching zero didnt necessarily mean it actually executed; the condition's sometimes slipped to FALSE and the counter went far into the negatives.

    Resetting timing involved working out how many 15min increments we were away from what was considered 'scheduled' execution time. I managed to replace it, at least.


    Pshaw. Your code was clearly doing:
    if (timer == 0) ...
    The fix was clearly:
    if (timer <= 0) ...

    No maths required.


    Firstly: MY code?! I was the one who had to support it!
    Plus it was a major effort to convince people that it needed changing.

    Also: You're assuming the condition execute was "Counter less than zero". There were other conditions in play.

    So no. There was no timer <= 0
  • Daniel 2012-12-12 10:42
    Doctor_of_Ineptitude:
    Kent:
    You see, early on, there was a bunch of deadlocking as it gained more widespread use. So, to make sure that users kept their time to a minimum, I added a sense of urgency
    Code smell is often merely a hint emanating from the buried corpse of rational thought. When the brave, armed with suitable quantities of holy water, detect code smell and pursue it to its source, the genesis of abomination is likely to be found.

    If your database is having deadlock problems, the solution is to give users 40 minutes (your mileage may vary) to submit the form?

    First of all, deadlocks arise from two (ignorantly written) updates occurring at roughly the same time. Not 40 minutes apart.

    Second, if your database is lumbering under the burden of 100 updates per day, the solution is not to prod the users to submit 100 updates faster. I'll leave it to the geniuses who haunt this board to work out The Real Answer but That's Not It (TM).


    If you had read the article, you would have realized that the original coder had realized and fixed the deadlocks. So you can take your snobbish Real Answer and basically *uck it.


    Eventually, but only after first burying this corpse of irrational thought.
  • neminem 2012-12-12 10:51
    TGV:
    Rodnas:
    "Time flies like an arrow, fruit flies like a banana" - G. Marx

    And of course: time flies with a stop watch (Hint: it's an imperative).

    My favorite creative misparsing of the statement "time flies like an arrow" is, "measure the timing of flies in the same way that an arrow would measure that timing", though yes, "measure the timing of flies in the same way that you would measure the timing of an arrow" is also a legitimate (if equally inappropriate semantically) parse.
  • redtetrahedron 2012-12-12 10:52
    Code Slave:
    letatio:
    Who the hell indents like that?


    My co-worker... they could definitely benefit from a slight tap on the cranium with the clue-bat.


    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!
  • Mark 2012-12-12 11:05
    Kent:
    Code smell is often merely a hint emanating from the buried corpse of rational thought. When the brave, armed with suitable quantities of holy water, detect code smell and pursue it to its source, the genesis of abomination is likely to be found.

    If your database is having deadlock problems, the solution is to give users 40 minutes (your mileage may vary) to submit the form?

    First of all, deadlocks arise from two (ignorantly written) updates occurring at roughly the same time. Not 40 minutes apart.

    Second, if your database is lumbering under the burden of 100 updates per day, the solution is not to prod the users to submit 100 updates faster. I'll leave it to the geniuses who haunt this board to work out The Real Answer but That's Not It (TM).


    There are a couple ways I interpret this article beyond how the developer described the problem or how the support engineer interpreted what he said. I think that, given the intent of the code, there was no lock at all and it was last in wins so that stale values on a web form the user kept open all day would overwrite new values. Or, there was actually a lock and it was perpetual until the user submitted the form. Either way, still pretty bad.
  • Mark 2012-12-12 11:07
    Global Javascript function named "start" and global vars named "min" and "sec". No semi-colons. Indentation is the least of this codes problems.
  • Bring Back Nagesh 2012-12-12 11:10
    redtetrahedron:
    Code Slave:
    letatio:
    Who the hell indents like that?


    My co-worker... they could definitely benefit from a slight tap on the cranium with the clue-bat.


    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!

    Wow, you would make an excellent salesman. You managed to convince your boss to spend money on a tool that only half-works, whereas if you'd done some research, you could have got a much better tool for free.

    Eclipse (with the web developers plugins) is one such product that can format JS source code without semicolons, plus it will highlight all the places where they should be, among other potential programming problems. Heck, even jsFiddle will do it for you, plus it comes with JSLint.
  • pjl 2012-12-12 11:12
    Time's fun when you're having flies (say the frogs).
  • TRWTF 2012-12-12 11:31
    The correct version actually shouldn't count on javascript timeout to increment a timer, since timeout is not accurate (It blocks on quite a few things and just queues up). A correct timer saves the start time, and the timeout just updates the timer using the current time and the start time to find out how long it has run.
  • fwip 2012-12-12 11:55
    redtetrahedron:
    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!

    Step 1. vim code.js
    Step 2. gg=GZZ
    Step 3. That was all the steps, you're done already.
  • redtetrahedron 2012-12-12 12:13
    Bring Back Nagesh:
    redtetrahedron:
    Code Slave:
    letatio:
    Who the hell indents like that?


    My co-worker... they could definitely benefit from a slight tap on the cranium with the clue-bat.


    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!

    Wow, you would make an excellent salesman. You managed to convince your boss to spend money on a tool that only half-works, whereas if you'd done some research, you could have got a much better tool for free.

    Eclipse (with the web developers plugins) is one such product that can format JS source code without semicolons, plus it will highlight all the places where they should be, among other potential programming problems. Heck, even jsFiddle will do it for you, plus it comes with JSLint.


    This was 11 years ago.. not sure those tools existed back then? I did look, could not find one for free at the time.
  • redtetrahedron 2012-12-12 12:17
    fwip:
    redtetrahedron:
    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!

    Step 1. vim code.js
    Step 2. gg=GZZ
    Step 3. That was all the steps, you're done already.


    OH! Of course! That's so self-evident and intuitive


    /s
  • Ardik 2012-12-12 12:33
    For me
      go=setTimeout("start()",1)
    
    [...]
    if(msec==100)
    is the funniest part.

    (As mentioned before by others) why not just
      setTimeout("start()",1000)
    and drop msec completely?!
    Would've resulted in a quite accurate result with any client (apart from skipping "0", of course).

    A timeout of 1 msec doesn't work in any browser.

    Probably that developer did some trial and error which timing somewhat fits the clock and based on that decided that 1 second has 100 msecs.


    btw: I'm not surprised this went unnoticed for several years.
    In my experience most users aren't aware that something like a status bar even exists, let alone watch a countdown timer there. They'll notice the 5-minutes-alert, though, and therefore this thing worked as intended ...
  • Mason Wheeler 2012-12-12 12:39
    g:
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats


    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.
  • jay 2012-12-12 12:49
    letatio:
    Who the hell indents like that?


    At my last job, my boss established a company standard that we were NOT ALLOWED to change indenting of existing lines of code. So if, for example, you added an IF statement that encompassed an existing block of code, you couldn't move those lines to the right to line up under the IF. The stated reason for this was that when we committed code to the library, this would make those lines look changed when they really weren't.

    (Of course, we were using SVN, which has a flag to say whether changes in spacing should show up as changed lines in output, so basically his problem was that he didn't want to have to check a box.)
  • foad 2012-12-12 12:53
    redtetrahedron:
    fwip:
    redtetrahedron:
    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!

    Step 1. vim code.js
    Step 2. gg=GZZ
    Step 3. That was all the steps, you're done already.


    OH! Of course! That's so self-evident and intuitive


    /s
    I missed the part where you were guaranteed that everything in life would be easy and require no knowledge or thought on your part.
  • jay 2012-12-12 12:57
    ColdHeart:
    Ahh, the wonders of bugs turning into features...


  • da Doctah 2012-12-12 12:58
    Ardik:
    (As mentioned before by others) why not just
      setTimeout("start()",1000)
    and drop msec completely?!
    Would've resulted in a quite accurate result with any client (apart from skipping "0", of course).

    A timeout of 1 msec doesn't work in any browser.

    Probably that developer did some trial and error which timing somewhat fits the clock and based on that decided that 1 second has 100 msecs.


    Everyone is making the totally unwarranted assumption that "msec" in the OP's code is supposed to stand for "millisecond". It's not. In this context, it's "minisecond".
  • old timer 2012-12-12 13:04
    I've written and maintained a lot of FORTRAN over the years. That indenting style is not from FORTRAN.
  • jay 2012-12-12 13:05
    Mason Wheeler:
    g:
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats


    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.


    Right, because who could possibly imagine a real-world problem in which an answer involving a fraction is not valid? Like, we have 5 people in our department and 2 offices. How many should we put in each office? Funny how last time we had this problem, the productivity of the guy we cut in half really fell off, and there was an unexplainable dip in the morale of the other four.
  • Ardik 2012-12-12 13:14
    da Doctah:
    Ardik:
    (As mentioned before by others) why not just
      setTimeout("start()",1000)
    and drop msec completely?!
    Would've resulted in a quite accurate result with any client (apart from skipping "0", of course).

    A timeout of 1 msec doesn't work in any browser.

    Probably that developer did some trial and error which timing somewhat fits the clock and based on that decided that 1 second has 100 msecs.

    Everyone is making the totally unwarranted assumption that "msec" in the OP's code is supposed to stand for "millisecond".

    Not me!
    da Doctah:
    It's not. In this context, it's "minisecond".

    csec would've been more appropriate then.
  • Mark 2012-12-12 13:30
    jay:

    At my last job, my boss established a company standard that we were NOT ALLOWED to change indenting of existing lines of code. So if, for example, you added an IF statement that encompassed an existing block of code, you couldn't move those lines to the right to line up under the IF. The stated reason for this was that when we committed code to the library, this would make those lines look changed when they really weren't.

    (Of course, we were using SVN, which has a flag to say whether changes in spacing should show up as changed lines in output, so basically his problem was that he didn't want to have to check a box.)


    Substituting one small hassle for a very large hassle is, unfortunately, something some managers excel at. Usually it means the small hassle is off their plate and the very large hassle is on someone else.
  • erat 2012-12-12 13:32
    Ardik:
    da Doctah:
    Ardik:
    (As mentioned before by others) why not just
      setTimeout("start()",1000)
    and drop msec completely?!
    Would've resulted in a quite accurate result with any client (apart from skipping "0", of course).

    A timeout of 1 msec doesn't work in any browser.

    Probably that developer did some trial and error which timing somewhat fits the clock and based on that decided that 1 second has 100 msecs.

    Everyone is making the totally unwarranted assumption that "msec" in the OP's code is supposed to stand for "millisecond".

    Not me!
    da Doctah:
    It's not. In this context, it's "minisecond".

    csec would've been more appropriate then.


    Well, if a vaginal birth is not possible, I guess...Oh, you meant something else. Sorry.
  • redtetrahedron 2012-12-12 13:33
    foad:
    redtetrahedron:
    fwip:
    redtetrahedron:
    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!

    Step 1. vim code.js
    Step 2. gg=GZZ
    Step 3. That was all the steps, you're done already.


    OH! Of course! That's so self-evident and intuitive


    /s
    I missed the part where you were guaranteed that everything in life would be easy and require no knowledge or thought on your part.


    I was commenting on the apparent tendency of UNIX commands to be obscure for the sake of obscurity. To each their own, though...
  • urza9814 2012-12-12 13:37
    Mason Wheeler:
    g:
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats


    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.


    So you believe all statically typed languages are insane? If you do 5/2 in C, Java or pretty much any statically typed language it's going to give you 2. You've gotta explicitly cast those ints to floats if you want a float as a result.
  • Steve HoldowaySteve 2012-12-12 13:45
    dkf:
    letatio:
    Who the hell indents like that?
    I used to work with someone who indented like that. As far as I could work out, it was a style they learned when they started out on FORTRAN (but I don't know which version, 66 or 77)…


    It would have to be fortran 77 as that's when block ifs were introduced
  • Broke my fingers on a teletype 2012-12-12 15:05
    redtetrahedron:
    ...

    I was commenting on the apparent tendency of UNIX commands to be obscure for the sake of obscurity. To each their own, though...


    I take it you have never typed a significant amount of text on the teletype machines that Unix originally ran on.
    Slow, clumsy keyboards with giant mechanical keys that (slightly exaggerated) you had to mash with your fist to use.

    5 minutes with one of those and you will quickly realize that those commands are not "obscure for the sake of obscurity" but rather "shortened for your fingers' sake".
  • PG4 2012-12-12 15:47
    Broke my fingers on a teletype:


    I take it you have never typed a significant amount of text on the teletype machines that Unix originally ran on.
    Slow, clumsy keyboards with giant mechanical keys that (slightly exaggerated) you had to mash with your fist to use.

    5 minutes with one of those and you will quickly realize that those commands are not "obscure for the sake of obscurity" but rather "shortened for your fingers' sake".


    So explain to me how the "biff" command got named when something else with 4 characters would have been much better.
  • Mason Wheeler 2012-12-12 16:10
    urza9814:
    Mason Wheeler:
    g:
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats


    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.


    So you believe all statically typed languages are insane? If you do 5/2 in C, Java or pretty much any statically typed language it's going to give you 2. You've gotta explicitly cast those ints to floats if you want a float as a result.


    Not "pretty much any statically typed language." Just the C family, which is insane for many reasons, including this one.

    In Pascal, on the other hand, (which is also statically typed,) "5/2" returns 2.5; if you want a result of 2, you use the integer division operator: "5 div 2". Which is the right way to do it: division behaves as actual division, and if you want something that's not quite real division but is similar, you use a different operator.
  • Jack 27 2012-12-12 16:10
    johnckirk:
    if(msec==100)
    
    {
    msec=0
    sec--
    }


    I always thought that there were 1000 milliseconds in a second.


    It doesn't matter. They aren't actually counting milliseconds, anyway.
  • redtetrahedron 2012-12-12 16:14
    Broke my fingers on a teletype:
    redtetrahedron:
    ...

    I was commenting on the apparent tendency of UNIX commands to be obscure for the sake of obscurity. To each their own, though...


    I take it you have never typed a significant amount of text on the teletype machines that Unix originally ran on.
    Slow, clumsy keyboards with giant mechanical keys that (slightly exaggerated) you had to mash with your fist to use.

    5 minutes with one of those and you will quickly realize that those commands are not "obscure for the sake of obscurity" but rather "shortened for your fingers' sake".


    No, thankfully. But, surely not needed now? I guess it's a matter of not changing anything you don't have to...
  • PedanticCurmudgeon 2012-12-12 16:37
    Mason Wheeler:
    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.
    8/10. Would cheerfully flame (actually, someone already did).
  • qbolec 2012-12-12 16:44
    So TRWTF is nobody has noticed for 6 years, that this clock was skewed by around 30% to 50% because 15ms is not equal 1cs?
  • HoHum 2012-12-12 17:07
    Errr...The issue with the 0 ain't got nought to do with speed of a computer....

    There's a bug in the code that means 0 will ALWAYS appear to be missed. But noone else noticed that, right?
  • PiisAWheeL 2012-12-12 17:10
    Am I the only one who starts with a timestamp and then just polls the difference for intervals? If you have to roll your own then thats probably the most reliable way I can see to do it.
  • Broke my fingers on a teletype 2012-12-12 17:17
    redtetrahedron:
    ...
    No, thankfully. But, surely not needed now? I guess it's a matter of not changing anything you don't have to...


    That and several decade's worth of legacy programs, scripts, and software...

    Of course you can customize the shell's commands with the alias command if you feel that the defaults are too cryptic

    Original
    ls


    Shorter
    alias l='ls -lart'


    Windows Flavour
    alias dir='ls'


    Politely Verbose
    alias Please_list_all_the_files_in_this_folder='ls'


    Whimsical
    alias hunt='ls && [ $[$RANDOM % 12] == 0 ] && echo "I smell a Wumpus"'
  • Mie 2012-12-12 17:18
    jay:
    Mason Wheeler:
    g:
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats


    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.


    Right, because who could possibly imagine a real-world problem in which an answer involving a fraction is not valid? Like, we have 5 people in our department and 2 offices. How many should we put in each office? Funny how last time we had this problem, the productivity of the guy we cut in half really fell off, and there was an unexplainable dip in the morale of the other four.
    Hmmm...but it seems two is not the right answer either - that is, unless you lay off anyone who doesn't fit in an office.
  • sr 2012-12-12 17:22
    Mason Wheeler:
    urza9814:
    Mason Wheeler:
    g:
    mike5:

    Not like (hours = timeInSecods / 3600 ), and minutes = (timeInSeconds % 3600 ) / 60 and seconds = (timeInSeconds % 60) ?


    hours = Math.floor(timeInSeconds / 3600);
    minutes = Math.floor((timeInSeconds % 3600) / 60);

    Division in javascript results in floats


    Division in any sane language results in floats. (Or fractions, if your language does that.) It's simple mathematics. If you can say 5/2 and get 2 as a result, there's something wrong with your language.


    So you believe all statically typed languages are insane? If you do 5/2 in C, Java or pretty much any statically typed language it's going to give you 2. You've gotta explicitly cast those ints to floats if you want a float as a result.


    Not "pretty much any statically typed language." Just the C family, which is insane for many reasons, including this one.

    In Pascal, on the other hand, (which is also statically typed,) "5/2" returns 2.5; if you want a result of 2, you use the integer division operator: "5 div 2". Which is the right way to do it: division behaves as actual division, and if you want something that's not quite real division but is similar, you use a different operator.

    why is 5/2 any different to "5 div 2"? Both / and div are arbitrary symbols....why should 1 be more correct than the other?
  • PseudoBovine 2012-12-12 17:23
    PedanticCurmudgeon:
    Mason Wheeler:
    If you can say 5/2 and get 2 as a result, there's something wrong with your language.
    8/10. Would cheerfully flame


    So you're giving it a zero?

    jay:
    Right, because who could possibly imagine a real-world problem in which an answer involving a fraction is not valid? Like, we have 5 people in our department and 2 offices. How many should we put in each office?


    If you have 5 people and 2 offices, and you put 5/2 (integer valued) in each office, you're *still* not going to be getting the correct answer. ("Well, Tom, the good news is we're not going to cut you in half. The bad news is that you've been truncated out of an office.")
  • Jimmy 2012-12-12 17:24
    Broke my fingers on a teletype:
    redtetrahedron:
    ...
    No, thankfully. But, surely not needed now? I guess it's a matter of not changing anything you don't have to...


    That and several decade's worth of legacy programs, scripts, and software...

    Of course you can customize the shell's commands with the alias command if you feel that the defaults are too cryptic

    Original
    ls


    Shorter
    alias l='ls -lart'


    Windows Flavour
    alias dir='ls'


    Politely Verbose
    alias Please_list_all_the_files_in_this_folder='ls'


    Whimsical
    alias hunt='ls && [ $[$RANDOM % 12] == 0 ] && echo "I smell a Wumpus"'

    As well as saving fingers, I thought some of the idea was saving sapce. Back in the day when we didn't have massive hard disk, 1 character was a lot, so wasting characters having a command like "copy" when you could call it "cp" seemed a silly thing to do.
  • foo 2012-12-12 18:22
    neminem:
    My favorite creative misparsing of the statement "time flies like an arrow" is, "measure the timing of flies in the same way that an arrow would measure that timing", though yes, "measure the timing of flies in the same way that you would measure the timing of an arrow" is also a legitimate (if equally inappropriate semantically) parse.
    Why inappropriate? I can just see the Mythbusters doing their experiments (except they'd need to find something to blow up in the end).
  • Groucho Marx 2012-12-12 19:36
    Time wounds all heels.
  • Watson 2012-12-12 19:59
    Mark Bowytz:
    Actually, I'm closer to an implementation of ELIZA.
    How do you feel about being closer to an implementation of ELIZA?
  • herby 2012-12-12 20:50
    PG4:
    Broke my fingers on a teletype:


    I take it you have never typed a significant amount of text on the teletype machines that Unix originally ran on.
    Slow, clumsy keyboards with giant mechanical keys that (slightly exaggerated) you had to mash with your fist to use.

    5 minutes with one of those and you will quickly realize that those commands are not "obscure for the sake of obscurity" but rather "shortened for your fingers' sake".


    So explain to me how the "biff" command got named when something else with 4 characters would have been much better.

    The program 'biff' was named after a dog that would bark at the postman. No other explanation necessary!

    p.s. Programming on an ASR33 will humble many a young programmer quite quickly!
  • Hired Mind 2012-12-12 21:13
    Please forgive my ignorance here - years ago I was forced at gunpoint to modify a large Java program and swore to never touch it again - but, is there seriously no way to get the local time in seconds since the epoch (or other reference point), in JavaScript?
  • Cat 2012-12-12 21:48
    Hired Mind:
    Please forgive my ignorance here - years ago I was forced at gunpoint to modify a large Java program and swore to never touch it again - but, is there seriously no way to get the local time in seconds since the epoch (or other reference point), in JavaScript?


    Is there a way?


    var start=new Date();
    // later
    var end=new Date();
    var intervalInSeconds = (end-start)/1000;


    Is there a way that the original developer knew about? Maybe not. He also doesn't seem to know about setInterval() in place of setTimeout().

    Worse, using a 1ms timeout instead of a 1s timeout when the lowest resolution your timer needs is at the second level shows not just a lack of knowledge, but a lack of understanding.
  • TK 2012-12-12 23:06
    sr:
    Mason Wheeler:

    Not "pretty much any statically typed language." Just the C family, which is insane for many reasons, including this one.

    In Pascal, on the other hand, (which is also statically typed,) "5/2" returns 2.5; if you want a result of 2, you use the integer division operator: "5 div 2". Which is the right way to do it: division behaves as actual division, and if you want something that's not quite real division but is similar, you use a different operator.

    why is 5/2 any different to "5 div 2"? Both / and div are arbitrary symbols....why should 1 be more correct than the other?
    It's not that one symbol is more correct than the other. The point is that they are different symbols for different operators. Use whatever symbol you want.

    I really like this change in Python 3. Rather than inheriting the approach of C like Python 2, they now treat "/" as floating point division and "//" as integer division. This makes code more reusable because the operation is the same all the time; the type of the arguments doesn't matter.

    Sure you could cast to get the same effect. But it's nice that you don't have to.
  • mt@ilovefactory.com 2012-12-13 03:46
    "If your database is having deadlock problems, the solution is to give users 40 minutes (your mileage may vary) to submit the form?

    First of all, deadlocks arise from two (ignorantly written) updates occurring at roughly the same time. Not 40 minutes apart. "

    Deadlocks can easily come from 2 different events far from each other in time. A deadlock is not always a race condition.

    I guess that in this case, the system locked some entries in the database, from the user started his action, and until he finished his work. In that case, forcing the user to work faster would decrease the risk of deadlock, because the time the user held the problematic locks could be decreased.

    Or maybe the developer is just an idiot.
  • Steve The Cynic 2012-12-13 04:06
    TK:
    I really like this change in Python 3. Rather than inheriting the approach of C like Python 2, they now treat "/" as floating point division and "//" as integer division. This makes code more reusable because the operation is the same all the time; the type of the arguments doesn't matter.

    Sure you could cast to get the same effect. But it's nice that you don't have to.

    The idea of having different symbols for different operations (division-giving-long-double versus division-giving-integers) is good. Deliberately choosing several other languages' rest-of-line comment introducer, not so much.

    Oh, and does it coerce floats to integers before or after the // division? (i.e. does 0.41 // 0.1 give 4 or a div-by-zero trap?)
  • Earlchaos 2012-12-13 05:18
    That piece of crap is not even beginners code, author could've used google to search for a working version...
  • Hatshepsut 2012-12-13 06:48
    erat:
    Ardik:

    csec would've been more appropriate then.


    Well, if a vaginal birth is not compatible with the doctor's tee-off time, I guess...

    FTFY.

  • RFmich 2012-12-13 07:40
    Steve HoldowaySteve:
    dkf:
    letatio:
    Who the hell indents like that?
    I used to work with someone who indented like that. As far as I could work out, it was a style they learned when they started out on FORTRAN (but I don't know which version, 66 or 77)…


    It would have to be fortran 77 as that's when block ifs were introduced


    Don't need no Steeenking block ifs:

    C Assume some code above that sets I,B,C,D

    IF (I) 100,200,300
    100 CONTINUE
    A = B
    GOTO 400
    200 CONTINUE
    A = C
    GOTO 400
    300 CONTINUE
    A = D
    GOTO 400
    400 CONTINUE

    C ..and more code below. The extra GOTO allows us to
    C insert code between it and 400 that we can
    C 'spaghetti into' if we want to be really perverse.

  • PedanticCurmudgeon 2012-12-13 08:45
    PseudoBovine:
    PedanticCurmudgeon:
    Mason Wheeler:
    If you can say 5/2 and get 2 as a result, there's something wrong with your language.
    8/10. Would cheerfully flame


    So you're giving it a zero?
    That was mildly funny, but don't quit your day job.
  • TK 2012-12-13 11:09
    Steve The Cynic:
    TK:
    I really like this change in Python 3. Rather than inheriting the approach of C like Python 2, they now treat "/" as floating point division and "//" as integer division. This makes code more reusable because the operation is the same all the time; the type of the arguments doesn't matter.

    Sure you could cast to get the same effect. But it's nice that you don't have to.

    The idea of having different symbols for different operations (division-giving-long-double versus division-giving-integers) is good. Deliberately choosing several other languages' rest-of-line comment introducer, not so much.

    Oh, and does it coerce floats to integers before or after the // division? (i.e. does 0.41 // 0.1 give 4 or a div-by-zero trap?)
    >>> 0.41 // 0.1
    4.0

    Sure "//" isn't going to fly in languages that use it for comments, but since Python uses "#" for that, who cares?
  • Robert 2012-12-13 14:00
    I guess sometimes the fix can be worse than the bug.
  • ysap 2012-12-13 17:54
    Well, I think there's more to this code when examined carefully - there are a 1,000 milliseconds in a second, not a 100 !
  • QJo 2012-12-17 08:43
    neminem:
    TGV:
    Rodnas:
    "Time flies like an arrow, fruit flies like a banana" - G. Marx

    And of course: time flies with a stop watch (Hint: it's an imperative).

    My favorite creative misparsing of the statement "time flies like an arrow" is, "measure the timing of flies in the same way that an arrow would measure that timing", though yes, "measure the timing of flies in the same way that you would measure the timing of an arrow" is also a legitimate (if equally inappropriate semantically) parse.


    I always read it as "measure the timing (indirectly ascertaining their speed) of the flies with the alacrity of the flight of an arrow through the air" - that is, "I want you to time those flies - get a move on!"
  • Neil 2012-12-19 05:31
    redtetrahedron:
    Bring Back Nagesh:
    redtetrahedron:
    Code Slave:
    letatio:
    Who the hell indents like that?
    My co-worker... they could definitely benefit from a slight tap on the cranium with the clue-bat.
    At a previous job, I inherited a javascript app with 15K-line files from a fired co-worker who had a completely haphazard indenting style, enough to make the code virtually unreadable. Also - there were no semicolons ending the lines, just like this code. I convinced my supervisor to buy a code-reformatting tool, but before I could use it, I still had to add semicolons to the end of each statement before the reformatting tool would be able to parse the code. Ugh!
    Wow, you would make an excellent salesman. You managed to convince your boss to spend money on a tool that only half-works, whereas if you'd done some research, you could have got a much better tool for free.

    Eclipse (with the web developers plugins) is one such product that can format JS source code without semicolons, plus it will highlight all the places where they should be, among other potential programming problems. Heck, even jsFiddle will do it for you, plus it comes with JSLint.
    This was 11 years ago.. not sure those tools existed back then? I did look, could not find one for free at the time.
    Gecko (or Netscape 6.x, as you would have known it back then) could do it. However the feature was discontinued and Firefox 17 is no longer capable of reformatting the source.
  • Neil 2012-12-19 05:38
    g:
    In some web-survey code I inherited I found code which measured the time people took for each question with setTimeout(function() { time += 1},1); And divided the resulting time by 100 to get the number of seconds, I guess they used 100 just to get the values to "look" right.
    Did you mean setInterval (or some sort of fake interval created by setting a timeout in the timeout handler)?

    By the way, Firefox has a preference for the minimum interval, so you can make survey time pass more slowly. (Or just switch tabs, as the default for background tabs is higher.) The preference also applies to self-resetting timeouts, although obviously the first one is unaffected. Before that preference existed, there was a 10ms minimum on timeouts and intervals.
  • ab 2013-01-02 16:04
    jay:
    At my last job, my boss established a company standard that we were NOT ALLOWED to change indenting of existing lines of code. So if, for example, you added an IF statement that encompassed an existing block of code, you couldn't move those lines to the right to line up under the IF. The stated reason for this was that when we committed code to the library, this would make those lines look changed when they really weren't.

    Assuming your boss couldn't learn how to type "-b", the right solution was to check in some helper tool written in Python or Haskell or some other language where indentation is significant. Once someone needed to, e.g., add a conditional somewhere in that tool, your boss would be unmasked as a 1960s robot as he shouted "does not compute" until his head exploded.
  • ab 2013-01-02 16:18
    Mason Wheeler:
    In Pascal, on the other hand, (which is also statically typed,) "5/2" returns 2.5; if you want a result of 2, you use the integer division operator: "5 div 2". Which is the right way to do it: division behaves as actual division, and if you want something that's not quite real division but is similar, you use a different operator.

    Complaining that "5/2 == 2" is wrong because that's not how the division operator works in grade-school math is like complaining that "for (i =0; i != 10; ++i) {}; i == 10;" is wrong because that's not what the word "each" means in English. There is no mathematical division operator there. Have you ever seen "5 divided by 2" spelled as "5/2" in any context except programming? That's not how division is written. (Of course it is how you write a fraction in some contexts, but in that case, it's the fraction "5/2", not an expression to be evaluated as 2, or 2.5, or anything else.)

    Since "5/2 == 2" is clearly a programming expression, not a mathematical expression, it only makes sense if you understand what language it's written in. In most C-family languages, it's true. In, say, Python 3, it's false. Neither one of those is "right" outside of context.

    The right answer, of course, is to use Applescript (or Hypercard), where you write "5 ÷ 2", just as you did in grade school, and by the time you've figured out how to type the "÷" on a keyboard with no such symbol, you no longer care what the answer was.
  • ab 2013-01-02 16:24
    Hired Mind:
    Please forgive my ignorance here - years ago I was forced at gunpoint to modify a large Java program and swore to never touch it again - but, is there seriously no way to get the local time in seconds since the epoch (or other reference point), in JavaScript?

    Given that Java and JavaScript are completely different and unrelated languages, it looks like Netscape's marketing ploy 17 years ago paid off by making sure people like you never learned their language. After all, who wants programmers who understand, admit, and ask for forgiveness of ignorance while asking simple questions, when you can attract programmers who are absolutely sure that the one tool they know how to use must be the right one?
  • Anone 2013-01-08 20:51
    ab:
    Have you ever seen "5 divided by 2" spelled as "5/2" in any context except programming? That's not how division is written.


    That's how pretty much everyone wrote division that I'm aware of in high school and university.

    a la

    e/m = c²

    or

    e
    - = c²
    m

    Why would you ever use the basic division symbol when it takes up more room and takes longer to write out? Especially since it's just a pictograph for the slash version anyway, with the dots representing the numerator/denominator.
  • Samwise 2013-01-22 16:09
    Lets make time fly even faster:

    var sec = 0, min = 40;
    var interval = setInterval(function(){
    if(min==5 && sec == 0){ alert("Save yo content, foo!");}
    if (sec == 0){min--;sec = 59;}
    if (min < 0){ min = 0; sec = 0; clearTimeout(interval);}
    var fnPad = function(v){ v=v.toString(); while(v.length<2){v = "0" + v;} return v;};
    self.status=min + ":" + fnPad(sec--);
    }, 100);