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. Other Discussions
  3. The Weird and The Wonderful
  4. Just amusing...

Just amusing...

Scheduled Pinned Locked Moved The Weird and The Wonderful
7 Posts 6 Posters 0 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.
  • D Offline
    D Offline
    dybs
    wrote on last edited by
    #1

    Not much of a horror, but it still made me chuckle...

    int x = 1 + y - 1;

    Dybs

    P J 2 Replies Last reply
    0
    • D dybs

      Not much of a horror, but it still made me chuckle...

      int x = 1 + y - 1;

      Dybs

      P Offline
      P Offline
      puromtec1
      wrote on last edited by
      #2

      ...if you replace the "magic numbers" with variables, this will make sense to do this way...in a readability sort of way.

      D D 2 Replies Last reply
      0
      • D dybs

        Not much of a horror, but it still made me chuckle...

        int x = 1 + y - 1;

        Dybs

        J Offline
        J Offline
        Jeremy Tierman
        wrote on last edited by
        #3

        This may be a code signature

        1 Reply Last reply
        0
        • P puromtec1

          ...if you replace the "magic numbers" with variables, this will make sense to do this way...in a readability sort of way.

          D Offline
          D Offline
          dojohansen
          wrote on last edited by
          #4

          Well. Not if it's the SAME variable! int x = z + y - z;

          B 1 Reply Last reply
          0
          • D dojohansen

            Well. Not if it's the SAME variable! int x = z + y - z;

            B Offline
            B Offline
            BadKarma
            wrote on last edited by
            #5

            What if z is volatile? This would mean that the value of z could change between the add and the subtraction.

            Learn from the mistakes of others, you may not live long enough to make them all yourself.

            S 1 Reply Last reply
            0
            • B BadKarma

              What if z is volatile? This would mean that the value of z could change between the add and the subtraction.

              Learn from the mistakes of others, you may not live long enough to make them all yourself.

              S Offline
              S Offline
              supercat9
              wrote on last edited by
              #6

              What if z is volatile? If Z is volatile, then the code would go from being an idiosyncrasy to a horror, since the two reads would not be equivalent but there would be no sequence point to guarantee which would happen first. In some cases, it's fine to read a volatile variable twice without an intervening sequence point (e.g. using something like while (*mem != *mem); to determine whether a flash chip is busy). If the order in which the reads take place won't matter, then it won't matter if the compiler rearranges them. On the other hand, in an expression like foo = vol + something - vol, the effect on foo if vol changes will be indeterminate. Far better would be something like foo = vol + something; foo -= vol;. Under that scenario, the semantics if vol changes would be clear.

              1 Reply Last reply
              0
              • P puromtec1

                ...if you replace the "magic numbers" with variables, this will make sense to do this way...in a readability sort of way.

                D Offline
                D Offline
                dybs
                wrote on last edited by
                #7

                In this case, it was for setting the position for modifying a string, so it could certain make sense for clarifying exactly where the update is inserted/replaced. But still, the "magic numbers" were there, not variables.

                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