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. Strategies to upgrade antiquated C and C++ code to some more modern coding practice.

Strategies to upgrade antiquated C and C++ code to some more modern coding practice.

Scheduled Pinned Locked Moved The Lounge
c++graphicsdevopscollaborationquestion
24 Posts 12 Posters 1 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.
  • M Maximilien

    I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

    CI/CD = Continuous Impediment/Continuous Despair

    0 Offline
    0 Offline
    0x01AA
    wrote on last edited by
    #4

    I don't see any bigger problems, as long you do not need to switch from 32 to 64 bit and there is pointer arithmetics involved in the code ;) Btw. 'const ref' vs. 'const pointers', I think it makes no big difference, but I can be wrong.

    M 1 Reply Last reply
    0
    • 0 0x01AA

      I don't see any bigger problems, as long you do not need to switch from 32 to 64 bit and there is pointer arithmetics involved in the code ;) Btw. 'const ref' vs. 'const pointers', I think it makes no big difference, but I can be wrong.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #5

      It's still a 32bit application. The way I see it, we'll update to 128bit version before 64bit.

      CI/CD = Continuous Impediment/Continuous Despair

      N 1 Reply Last reply
      0
      • M Maximilien

        It's still a 32bit application. The way I see it, we'll update to 128bit version before 64bit.

        CI/CD = Continuous Impediment/Continuous Despair

        N Offline
        N Offline
        Nelek
        wrote on last edited by
        #6

        Update or re-write it? :rolleyes:

        M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

        1 Reply Last reply
        0
        • N Nelek

          Maximilien wrote:

          We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work.

          Then... the classic: do not touch a running system.

          M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #7

          Quote:

          do not touch a running system.

          Good advice unless it has, or is causing, lots of bugs or hindering development.

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          N 1 Reply Last reply
          0
          • M Maximilien

            I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

            CI/CD = Continuous Impediment/Continuous Despair

            Greg UtasG Offline
            Greg UtasG Offline
            Greg Utas
            wrote on last edited by
            #8

            Introducing const to a system that didn't use it, or used it haphazardly because some developers never bothered, can be quite the exercise. It's like trying to pull a shirt out of a dryer--the chain reaction can be quite messy. I've even gone down that rathole in code that I've written. "Hmm, this should probably have been const." And then giving up after lots of unexpected compile errors or deciding that mutable should be used far more sparingly.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
            <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

            1 Reply Last reply
            0
            • M Maximilien

              I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

              CI/CD = Continuous Impediment/Continuous Despair

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #9

              You can't compartmentalize parts of it? If you could then you create a different project/build and create a library from just that. Then the rest of the code uses that library (not the code.) For maintenance you then decide whether to update the library (nothing else) or not. The API layer to the library would remain exactly the same during the primary update. You might update the API layer after that to make it more consistent.

              M 1 Reply Last reply
              0
              • Greg UtasG Greg Utas

                Quote:

                do not touch a running system.

                Good advice unless it has, or is causing, lots of bugs or hindering development.

                Robust Services Core | Software Techniques for Lemmings | Articles
                The fox knows many things, but the hedgehog knows one big thing.

                N Offline
                N Offline
                Nelek
                wrote on last edited by
                #10

                Greg Utas wrote:

                Good advice unless it has, or is causing, lots of bugs or hindering development.

                I agree with that specially the second part, but (big but) only if you have someone that knows what is going in the underwears... if not, leave it running and replace it by something new, taking care that the new is covering the same functionality before replacing and having a good backup (just in case).

                M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                1 Reply Last reply
                0
                • J jschell

                  You can't compartmentalize parts of it? If you could then you create a different project/build and create a library from just that. Then the rest of the code uses that library (not the code.) For maintenance you then decide whether to update the library (nothing else) or not. The API layer to the library would remain exactly the same during the primary update. You might update the API layer after that to make it more consistent.

                  M Offline
                  M Offline
                  Maximilien
                  wrote on last edited by
                  #11

                  Probably, but with difficulty. I'm also trying to instill some modern techniques to the team without everyone going bonkers. I'm looking for basic things that we need to do codewize to go forward into more modern ways of coding. There's also the cost/benefit that I need to take into account and demonstrate that to the higher authorities at some point.

                  CI/CD = Continuous Impediment/Continuous Despair

                  1 Reply Last reply
                  0
                  • M Maximilien

                    I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                    CI/CD = Continuous Impediment/Continuous Despair

                    B Offline
                    B Offline
                    BernardIE5317
                    wrote on last edited by
                    #12

                    have you measured for memory leaks ? if leaky code my advice replace malloc/free w/ the various smart pointers .

                    1 Reply Last reply
                    0
                    • M Maximilien

                      I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                      CI/CD = Continuous Impediment/Continuous Despair

                      T Offline
                      T Offline
                      tharkaway
                      wrote on last edited by
                      #13

                      I am also working in an old C++ MFC project that was developed in the late 90's. I am using Reshaper from JetBrains. It gives a lot of suggestions when looking at old C++ code. Adding a 'const' shows up a lot. It includes a lot of suggestions to use the newer memory/string functions that require providing the buffer size. I don't take all of it's suggestions since the code is very reliable.

                      1 Reply Last reply
                      0
                      • M Maximilien

                        I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                        CI/CD = Continuous Impediment/Continuous Despair

                        W Offline
                        W Offline
                        WPerkins
                        wrote on last edited by
                        #14

                        I have done this many times, doing it now for my current company. Old C code from the early 90s written to run on another OS (embedded) tweaked and tweaked into unmaintainability extreme. At the end of each phase the programming should produce exactly the same output/changes. My description here is for C, what I am working on now; it will work for any language(s). Phase 1 - find and deal with global variables. This is a judgement call as some things are properly global, some are just laziness on the part of some programmer. One tactic, make a structure, move them there to get them all organized. Refer to the globals only as part of the structure and pass only pointers to the structure. Control, you want to get control of the globals or at least make it clear where they are getting changed. In C/C++ a function that gets the structure read only will be "const", if it changes a value then not const, get it? Phase 2 - find duplicate (or near duplicate) code and create functions or subs to perform. Replace this code with calls. I call this "mining functions', you are digging them out of the code. Repeat till there are not more easy targets to mine. You can repeat this later in the process. Phase 3 - now look for unexecutable code - in large systems there will often be some. In the system I am working on now there were subs that never get called, functions that got called but the return values were ignored or tossed away. These really were changing global variables - that would be discovered in Phase 2, right? Phase 4 - look for bad algorithms and processes. for loops, while loops are the first targets. Repeat until you've done enough. Each phase makes it easier to see what to do int the next.

                        M C 2 Replies Last reply
                        0
                        • M Maximilien

                          I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                          CI/CD = Continuous Impediment/Continuous Despair

                          I Offline
                          I Offline
                          Ian Brockbank
                          wrote on last edited by
                          #15

                          Michael Feathers wrote a great book "Working with Legacy Code" for exactly this. Step 1: wrap anything you are going to change in tests so you know what it does now and can ensure it continues to do that after your changes... Step 2...whatever is necessary to fix problems you are having. The strangler fig pattern can be helpful - gradually wrap/replace sections until the whole codebase has been replaced, like a strangler fig strangling a tree. Ian

                          M C 2 Replies Last reply
                          0
                          • W WPerkins

                            I have done this many times, doing it now for my current company. Old C code from the early 90s written to run on another OS (embedded) tweaked and tweaked into unmaintainability extreme. At the end of each phase the programming should produce exactly the same output/changes. My description here is for C, what I am working on now; it will work for any language(s). Phase 1 - find and deal with global variables. This is a judgement call as some things are properly global, some are just laziness on the part of some programmer. One tactic, make a structure, move them there to get them all organized. Refer to the globals only as part of the structure and pass only pointers to the structure. Control, you want to get control of the globals or at least make it clear where they are getting changed. In C/C++ a function that gets the structure read only will be "const", if it changes a value then not const, get it? Phase 2 - find duplicate (or near duplicate) code and create functions or subs to perform. Replace this code with calls. I call this "mining functions', you are digging them out of the code. Repeat till there are not more easy targets to mine. You can repeat this later in the process. Phase 3 - now look for unexecutable code - in large systems there will often be some. In the system I am working on now there were subs that never get called, functions that got called but the return values were ignored or tossed away. These really were changing global variables - that would be discovered in Phase 2, right? Phase 4 - look for bad algorithms and processes. for loops, while loops are the first targets. Repeat until you've done enough. Each phase makes it easier to see what to do int the next.

                            M Offline
                            M Offline
                            Maximilien
                            wrote on last edited by
                            #16

                            Thanks.

                            CI/CD = Continuous Impediment/Continuous Despair

                            1 Reply Last reply
                            0
                            • I Ian Brockbank

                              Michael Feathers wrote a great book "Working with Legacy Code" for exactly this. Step 1: wrap anything you are going to change in tests so you know what it does now and can ensure it continues to do that after your changes... Step 2...whatever is necessary to fix problems you are having. The strangler fig pattern can be helpful - gradually wrap/replace sections until the whole codebase has been replaced, like a strangler fig strangling a tree. Ian

                              M Offline
                              M Offline
                              Maximilien
                              wrote on last edited by
                              #17

                              Thanks for the book reference.

                              CI/CD = Continuous Impediment/Continuous Despair

                              1 Reply Last reply
                              0
                              • M Maximilien

                                I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                                CI/CD = Continuous Impediment/Continuous Despair

                                C Offline
                                C Offline
                                charlieg
                                wrote on last edited by
                                #18

                                Changing pointers to references may have very interesting consequences. Before I'd be making API changes like that, I would want a solid set of test cases to prove I hadn't broken anything. - glancing at project on new laptop with 20 year old mfc code and limited comments.

                                Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                                1 Reply Last reply
                                0
                                • I Ian Brockbank

                                  Michael Feathers wrote a great book "Working with Legacy Code" for exactly this. Step 1: wrap anything you are going to change in tests so you know what it does now and can ensure it continues to do that after your changes... Step 2...whatever is necessary to fix problems you are having. The strangler fig pattern can be helpful - gradually wrap/replace sections until the whole codebase has been replaced, like a strangler fig strangling a tree. Ian

                                  C Offline
                                  C Offline
                                  charlieg
                                  wrote on last edited by
                                  #19

                                  yeah, what he said. I was lazy and didn't bother to scroll to the bottom. :)

                                  Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                                  1 Reply Last reply
                                  0
                                  • M Maximilien

                                    I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                                    CI/CD = Continuous Impediment/Continuous Despair

                                    E Offline
                                    E Offline
                                    etkid84
                                    wrote on last edited by
                                    #20

                                    Where I work that would be cost prohibitive, because of the amount of V,V & T involved.

                                    ~d~

                                    C 1 Reply Last reply
                                    0
                                    • W WPerkins

                                      I have done this many times, doing it now for my current company. Old C code from the early 90s written to run on another OS (embedded) tweaked and tweaked into unmaintainability extreme. At the end of each phase the programming should produce exactly the same output/changes. My description here is for C, what I am working on now; it will work for any language(s). Phase 1 - find and deal with global variables. This is a judgement call as some things are properly global, some are just laziness on the part of some programmer. One tactic, make a structure, move them there to get them all organized. Refer to the globals only as part of the structure and pass only pointers to the structure. Control, you want to get control of the globals or at least make it clear where they are getting changed. In C/C++ a function that gets the structure read only will be "const", if it changes a value then not const, get it? Phase 2 - find duplicate (or near duplicate) code and create functions or subs to perform. Replace this code with calls. I call this "mining functions', you are digging them out of the code. Repeat till there are not more easy targets to mine. You can repeat this later in the process. Phase 3 - now look for unexecutable code - in large systems there will often be some. In the system I am working on now there were subs that never get called, functions that got called but the return values were ignored or tossed away. These really were changing global variables - that would be discovered in Phase 2, right? Phase 4 - look for bad algorithms and processes. for loops, while loops are the first targets. Repeat until you've done enough. Each phase makes it easier to see what to do int the next.

                                      C Offline
                                      C Offline
                                      charlieg
                                      wrote on last edited by
                                      #21

                                      "some are just laziness on the part of some programmer" your post is excellent advice and I suspect you have scars :)

                                      Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                                      1 Reply Last reply
                                      0
                                      • E etkid84

                                        Where I work that would be cost prohibitive, because of the amount of V,V & T involved.

                                        ~d~

                                        C Offline
                                        C Offline
                                        charlieg
                                        wrote on last edited by
                                        #22

                                        ah yes IVV. The best job I despised.

                                        Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                                        1 Reply Last reply
                                        0
                                        • M Maximilien

                                          I'm deep into some antiquated C and C++ code (some here would say antediluvian ) I'd like to start instructing my team to clean up code as they go. We have ton of low level libraries that cannot be easily updated (time and budget concerns) And from what I can see, they work. Do you know of any documents and/or white papers that discuss this particular topic ? For now, I'm telling people to use reference instead of passing everything by pointers and use const as much as possible. To at least indicate code intent. I can't even use STL (string and vector) at this point. :-(

                                          CI/CD = Continuous Impediment/Continuous Despair

                                          R Offline
                                          R Offline
                                          rxantos
                                          wrote on last edited by
                                          #23

                                          Code does not rot. If it works it works. Only when it has or need to be interfaced with something new, does it need a rewrite. And by then he it may be better to write the function from scratch. Time is life. Are you willing to exchange life, yours or someone else, in order to change something that works for something that may or not work? That said in the rare occasions. Make sure you understand the requirements and don't go into change for the sake on change.Then change what needs to be changed.

                                          M 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