Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. MILLISEC_PER_SEC is never needed

MILLISEC_PER_SEC is never needed

Scheduled Pinned Locked Moved The Lounge
76 Posts 31 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PIEBALDconsult

    Surely that's a constant?

    K Offline
    K Offline
    kalberts
    wrote on last edited by
    #45

    Constants ain't. Variables won't.

    1 Reply Last reply
    0
    • C Chris Maunder

      I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

      cheers Chris Maunder

      K Offline
      K Offline
      kalberts
      wrote on last edited by
      #46

      But you wouldn't believe how many people talk about 'kWh per hour' Sure, it could be meaningful if the power varies, but then 'kWh per hour' might vary, too, and cannot be treated as a single value. And lots of people refer to kWh/hour even when the power is constant.

      C 1 Reply Last reply
      0
      • C Chris Maunder

        I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

        cheers Chris Maunder

        K Offline
        K Offline
        kalberts
        wrote on last edited by
        #47

        But we really need a constant - or is that a variable? - ZEROES_IN_ONE_BILLION.

        P C 2 Replies Last reply
        0
        • W W Balboos GHB

          Or, perhaps it's needed to differentiate in regions where they use an Imperial Second instead of a regular one.

          Ravings en masse^

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #48

          Like a dueling second?

          1 Reply Last reply
          0
          • K kalberts

            But we really need a constant - or is that a variable? - ZEROES_IN_ONE_BILLION.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #49

            Is that a leading question?

            K 1 Reply Last reply
            0
            • P PIEBALDconsult

              Is that a leading question?

              K Offline
              K Offline
              kalberts
              wrote on last edited by
              #50

              Consider it as a "Constants ain't, varaiables won't" sample.

              P 1 Reply Last reply
              0
              • K kalberts

                Consider it as a "Constants ain't, varaiables won't" sample.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #51

                I suppose that's the long and short of it.

                1 Reply Last reply
                0
                • J Joe Woodbury

                  I actually disagree; I often deal with code using second, milliseconds, microseconds and even nanoseconds, sometimes with more than one unit in a function. Having a const (of some sort) declares what conversion was intended.

                  C Offline
                  C Offline
                  Chris Maunder
                  wrote on last edited by
                  #52

                  A counterargument. Excellent. My counter-counter argument, and the reason my brain popped: I would always prefer to have something like

                  var timeInSeconds = X;
                  var timeInMilliseconds = timeInSeconds / 1000;

                  My point is that the 1000 is always 1000. The variables surrounding that should be the ones clearly named. The "1000" can never be anything other than 1000. At least that's what I think.

                  cheers Chris Maunder

                  J K 2 Replies Last reply
                  0
                  • K kalberts

                    But you wouldn't believe how many people talk about 'kWh per hour' Sure, it could be meaningful if the power varies, but then 'kWh per hour' might vary, too, and cannot be treated as a single value. And lots of people refer to kWh/hour even when the power is constant.

                    C Offline
                    C Offline
                    Chris Maunder
                    wrote on last edited by
                    #53

                    So: What if you then had

                    var kiloWattPerHourPerHour = X;
                    var kiloWattPerHourPerMinute = kiloWattPerHourPerHour / 60;

                    Should the 60 be a constant or a literal?

                    cheers Chris Maunder

                    K 1 Reply Last reply
                    0
                    • K kalberts

                      But we really need a constant - or is that a variable? - ZEROES_IN_ONE_BILLION.

                      C Offline
                      C Offline
                      Chris Maunder
                      wrote on last edited by
                      #54

                      I blame the Brits for that one.

                      cheers Chris Maunder

                      J 1 Reply Last reply
                      0
                      • C Chris Maunder

                        A counterargument. Excellent. My counter-counter argument, and the reason my brain popped: I would always prefer to have something like

                        var timeInSeconds = X;
                        var timeInMilliseconds = timeInSeconds / 1000;

                        My point is that the 1000 is always 1000. The variables surrounding that should be the ones clearly named. The "1000" can never be anything other than 1000. At least that's what I think.

                        cheers Chris Maunder

                        J Offline
                        J Offline
                        Joe Woodbury
                        wrote on last edited by
                        #55

                        Aaaaggghhhh, Magic Number..... "It's supposed to be microseconds? Shit." (Near accurate quote.)

                        C 1 Reply Last reply
                        0
                        • J Joe Woodbury

                          Aaaaggghhhh, Magic Number..... "It's supposed to be microseconds? Shit." (Near accurate quote.)

                          C Offline
                          C Offline
                          Chris Maunder
                          wrote on last edited by
                          #56

                          <small pause> OK. That argument I will accept in that the number may appear in more than one place, making refactoring painful. Except, except, except: the calculation should only be done in one place:

                          var variable = ConvertToMilliFurlongs(X);

                          ...

                          function ConvertToMilliFurlongs(furlongs)
                          {
                          return furlongs / 1000;
                          }

                          So if it's "Dammit, Janet. It should be microFurlongs" then you rename ConvertToMilliFurlongs to ConvertToMicroFurlongs and change the 1000 to 1000000. Everything's still clear.

                          cheers Chris Maunder

                          J 1 Reply Last reply
                          0
                          • C Chris Maunder

                            <small pause> OK. That argument I will accept in that the number may appear in more than one place, making refactoring painful. Except, except, except: the calculation should only be done in one place:

                            var variable = ConvertToMilliFurlongs(X);

                            ...

                            function ConvertToMilliFurlongs(furlongs)
                            {
                            return furlongs / 1000;
                            }

                            So if it's "Dammit, Janet. It should be microFurlongs" then you rename ConvertToMilliFurlongs to ConvertToMicroFurlongs and change the 1000 to 1000000. Everything's still clear.

                            cheers Chris Maunder

                            J Offline
                            J Offline
                            Joe Woodbury
                            wrote on last edited by
                            #57

                            I thought you preferred cubits. Here's a webpage for you: Furlongs to Cubits | Kyle's Converter[^]

                            1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              Damn. I was hoping you knew of one! ;)

                              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                              J Offline
                              J Offline
                              Jim Meadors
                              wrote on last edited by
                              #58

                              U+035E s͞e͞e͞m͞s͞ to work... OH, not sure how it works in HTML....

                              <sig notetoself="think of a better signature"> <first>Jim</first> <last>Meadors</last> </sig>

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

                                cheers Chris Maunder

                                D Offline
                                D Offline
                                den2k88
                                wrote on last edited by
                                #59

                                It depends on what it is used for. It could be set to other values to speed up or slow down the time counting without changing other modules. If you develop PLC virtualizations, emulators or firmware that value may be fundamental instead of useless.

                                GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X

                                K 1 Reply Last reply
                                0
                                • C Chris Maunder

                                  I blame the Brits for that one.

                                  cheers Chris Maunder

                                  J Offline
                                  J Offline
                                  Jorgen Andersson
                                  wrote on last edited by
                                  #60

                                  You'd better blame the french, and the long scale was the original.

                                  Wrong is evil and must be defeated. - Jeff Ello

                                  1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    A counterargument. Excellent. My counter-counter argument, and the reason my brain popped: I would always prefer to have something like

                                    var timeInSeconds = X;
                                    var timeInMilliseconds = timeInSeconds / 1000;

                                    My point is that the 1000 is always 1000. The variables surrounding that should be the ones clearly named. The "1000" can never be anything other than 1000. At least that's what I think.

                                    cheers Chris Maunder

                                    K Offline
                                    K Offline
                                    kalberts
                                    wrote on last edited by
                                    #61

                                    So one hour, 3600 seconds, is 3.6 milliseconds.

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      So: What if you then had

                                      var kiloWattPerHourPerHour = X;
                                      var kiloWattPerHourPerMinute = kiloWattPerHourPerHour / 60;

                                      Should the 60 be a constant or a literal?

                                      cheers Chris Maunder

                                      K Offline
                                      K Offline
                                      kalberts
                                      wrote on last edited by
                                      #62

                                      Eeeeh... kW per hour per hour? In which situations does that unit occur? When does kW per hour occur? A constantly rising (or falling) power, at a rate of x kW/h? And then you want to a unit for how much, in kW, the power has risen in one hour. You don't want to simply call it kW, these are not 'absolute' kilowatts, but a change in power, and that is a change per hour over a period of an hour, which is a different kind of kW unit. Then you want the unit of the increase in power for each minute, right? You come across the strangest units if you look around :-) When I was a student, we did some filter calculations where I (after years of wondering) saw how a frequency correction could be given by a time value (European FM pre-emphashis is 50 us, US radios use 75 us - or is it the other way around?), but I never got it under my skin; it is just a strange artifact of unit arithmetics! If you change that 60 to NumberOfMinutesPerHour, the question is: Are these really the same MinutesPerHour as when you measure 'absolute' time progression? Or, do these MinutesPerHour have a slightly different semantics from the wall clock's minutes per hour (similar to a kW value indicating a change in kiloWattPerHourPerHour is different from an instantaneous, or constant, power kilowatt value)? Maybe it should be called NumberOfMinutesPerHourWhenCalculatingChangesInPowerOverTime? When we updated our programming guidelines, the project leader of our project immediately granted an exception from the 80 char maximum line length: Our rules for how to construct 'const' names (this was K&R C) led to serveral cases of identifiers of length >80. Finally, there is the famous Xerox Fortran manual quote:

                                      "The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable pi can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change."

                                      C 1 Reply Last reply
                                      0
                                      • D den2k88

                                        It depends on what it is used for. It could be set to other values to speed up or slow down the time counting without changing other modules. If you develop PLC virtualizations, emulators or firmware that value may be fundamental instead of useless.

                                        GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X

                                        K Offline
                                        K Offline
                                        kalberts
                                        wrote on last edited by
                                        #63

                                        In such a situation, the constant is badly mis-named. Calling it a constant, if you intend to vary it, is a small detail. Setting MILLISECONDS_PER_SEC to 2000? Seriously? Which time-dependent operations would that affect - those where milliseconds are used, those where other milliseconds are used? Or all? Would it double or half the speed? A properly named semi-constant value would be something like SLOWDOWN_FACTOR or VIRTUAL_TIME_TO_REAL_TIME. And while I am at it: I really hate this C style CONSTANTS_IN_ALL_UPPER_CASE rule - much because I have seen too many cases where functional extensions requiring the symbol defintion to be changed to a a variable to adapt to other situations - but the old UC name was used in so many source files and documentation that it cost too much to change it, and the old UC name was retained for the variable. (I also had my first serious programming training in Pascal, and when switching to C, I really missed the option to replace a semi-const/variable definition with a (parameterless) function call - parentheses were not required in Pascal, but in C you have to go through every use and add () to every use of the symbol.)

                                        D 1 Reply Last reply
                                        0
                                        • K kalberts

                                          In such a situation, the constant is badly mis-named. Calling it a constant, if you intend to vary it, is a small detail. Setting MILLISECONDS_PER_SEC to 2000? Seriously? Which time-dependent operations would that affect - those where milliseconds are used, those where other milliseconds are used? Or all? Would it double or half the speed? A properly named semi-constant value would be something like SLOWDOWN_FACTOR or VIRTUAL_TIME_TO_REAL_TIME. And while I am at it: I really hate this C style CONSTANTS_IN_ALL_UPPER_CASE rule - much because I have seen too many cases where functional extensions requiring the symbol defintion to be changed to a a variable to adapt to other situations - but the old UC name was used in so many source files and documentation that it cost too much to change it, and the old UC name was retained for the variable. (I also had my first serious programming training in Pascal, and when switching to C, I really missed the option to replace a semi-const/variable definition with a (parameterless) function call - parentheses were not required in Pascal, but in C you have to go through every use and add () to every use of the symbol.)

                                          D Offline
                                          D Offline
                                          den2k88
                                          wrote on last edited by
                                          #64

                                          Member 7989122 wrote:

                                          I really hate this C style CONSTANTS_IN_ALL_UPPER_CASE

                                          I really love it as it makes very clear what I'm looking at :D

                                          GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups