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. "...using the best programming practice..." yes sure

"...using the best programming practice..." yes sure

Scheduled Pinned Locked Moved The Weird and The Wonderful
hardwarehelp
13 Posts 9 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.
  • A Offline
    A Offline
    Andres Cassagnes
    wrote on last edited by
    #1

    Porting some C code to one microcontroller to another I found this:

    if((error == 0)
    #ifdef SENSAR
    || (V_OK == 0))
    #else
    )
    #endif

    This work is unhealthy

    P B P K 4 Replies Last reply
    0
    • A Andres Cassagnes

      Porting some C code to one microcontroller to another I found this:

      if((error == 0)
      #ifdef SENSAR
      || (V_OK == 0))
      #else
      )
      #endif

      This work is unhealthy

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

      How else would you do it? Other than, of course, eliminating the #else by putting the right-parenthesis outside the #if .

      A 1 Reply Last reply
      0
      • P PIEBALDconsult

        How else would you do it? Other than, of course, eliminating the #else by putting the right-parenthesis outside the #if .

        A Offline
        A Offline
        Andres Cassagnes
        wrote on last edited by
        #3

        leaving V_OK = 0 whenever you don't define SEANSAR maybe. If I could I would put all the code here for you to see what I have to strugle every day. There are violations to every single "good programing practice", starting for the encapsulation, with a lot of variables and defines mixed in separated and "isolated" modules.

        P 1 Reply Last reply
        0
        • A Andres Cassagnes

          leaving V_OK = 0 whenever you don't define SEANSAR maybe. If I could I would put all the code here for you to see what I have to strugle every day. There are violations to every single "good programing practice", starting for the encapsulation, with a lot of variables and defines mixed in separated and "isolated" modules.

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

          But this way eliminates a test when you don't have SENSAR; ergo it's FFFAAASSSTTTEEERRR... :cool: Why test something that's always true?

          A 1 Reply Last reply
          0
          • P PIEBALDconsult

            But this way eliminates a test when you don't have SENSAR; ergo it's FFFAAASSSTTTEEERRR... :cool: Why test something that's always true?

            A Offline
            A Offline
            Andres Cassagnes
            wrote on last edited by
            #5

            Maybe, but this things makes the code unclear and dificults the scalability. Anyway you are right in the fact that it is faster, innecesarily and almost insignificaly faster

            N 1 Reply Last reply
            0
            • A Andres Cassagnes

              Porting some C code to one microcontroller to another I found this:

              if((error == 0)
              #ifdef SENSAR
              || (V_OK == 0))
              #else
              )
              #endif

              This work is unhealthy

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              You're absolutely right. You ought to write it more concise:

              if((error == 0)
              #ifdef SENSAR
              || (V_OK == 0)
              #endif
              )

              Much cleaner now!

              1 Reply Last reply
              0
              • A Andres Cassagnes

                Porting some C code to one microcontroller to another I found this:

                if((error == 0)
                #ifdef SENSAR
                || (V_OK == 0))
                #else
                )
                #endif

                This work is unhealthy

                P Offline
                P Offline
                Patrice T
                wrote on last edited by
                #7

                I would have write it that way.

                #ifdef SENSAR
                if((error == 0) || (V_OK == 0))
                #else
                if((error == 0))
                #endif

                Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                B R 2 Replies Last reply
                0
                • A Andres Cassagnes

                  Maybe, but this things makes the code unclear and dificults the scalability. Anyway you are right in the fact that it is faster, innecesarily and almost insignificaly faster

                  N Offline
                  N Offline
                  Nicholas Marty
                  wrote on last edited by
                  #8

                  But when working with microcontrollers performance and memory management are important, aren't they? And many small things might lead to a noticeable gain in that.

                  M 1 Reply Last reply
                  0
                  • P Patrice T

                    I would have write it that way.

                    #ifdef SENSAR
                    if((error == 0) || (V_OK == 0))
                    #else
                    if((error == 0))
                    #endif

                    Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                    B Offline
                    B Offline
                    BillW33
                    wrote on last edited by
                    #9

                    I have to agree, it is easier to read that way.

                    Just because the code works, it doesn't mean that it is good code.

                    1 Reply Last reply
                    0
                    • A Andres Cassagnes

                      Porting some C code to one microcontroller to another I found this:

                      if((error == 0)
                      #ifdef SENSAR
                      || (V_OK == 0))
                      #else
                      )
                      #endif

                      This work is unhealthy

                      K Offline
                      K Offline
                      KarstenK
                      wrote on last edited by
                      #10

                      Such messie code is dangerous as gasoline. One error and everything blows up. I wont accept such stuff from my collegues. :mad:

                      Press F1 for help or google it. Greetings from Germany

                      1 Reply Last reply
                      0
                      • N Nicholas Marty

                        But when working with microcontrollers performance and memory management are important, aren't they? And many small things might lead to a noticeable gain in that.

                        M Offline
                        M Offline
                        Mohibur Rashid
                        wrote on last edited by
                        #11

                        I was going to say that. Sometime had to do weird trick, that looked really ugly, just to save program memory :D (PIC16F84)

                        I do not fear of failure. I fear of giving up out of frustration.

                        1 Reply Last reply
                        0
                        • P Patrice T

                          I would have write it that way.

                          #ifdef SENSAR
                          if((error == 0) || (V_OK == 0))
                          #else
                          if((error == 0))
                          #endif

                          Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                          R Offline
                          R Offline
                          RugbyLeague
                          wrote on last edited by
                          #12

                          still has double parentheses on the 2nd case. That will slow the compiler massively :)

                          P 1 Reply Last reply
                          0
                          • R RugbyLeague

                            still has double parentheses on the 2nd case. That will slow the compiler massively :)

                            P Offline
                            P Offline
                            Patrice T
                            wrote on last edited by
                            #13

                            I didn't try to optimise, just to make it easier to read.

                            Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                            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