C++ Weekly - Ep 407 - C++98 Code Restoration

Поділитися
Вставка
  • Опубліковано 28 лип 2024
  • ☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
    Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
    ► ndctechtown.com/workshops/c-b...
    Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
    ► cppunderthesea.nl/workshops/
    Episode details: github.com/lefticus/cpp_weekl...
    00:00 Intro
    00:29 Background
    02:07 Historical Context
    03:06 Current Status
    04:26 Adding Tests / Making Code Testable
    12:53 Crash Analysis
    14:53 Best Practices in C++98
    19:39 Applying Best Practices in C++98
    37:26 Final Results!
    T-SHIRTS AVAILABLE!
    ► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-sprin...
    WANT MORE JASON?
    ► My Training Classes: emptycrate.com/training.html
    ► Follow me on twitter: / lefticus
    SUPPORT THE CHANNEL
    ► Patreon: / lefticus
    ► Github Sponsors: github.com/sponsors/lefticus
    ► Paypal Donation: www.paypal.com/donate/?hosted...
    GET INVOLVED
    ► Video Idea List: github.com/lefticus/cpp_weekl...
    JASON'S BOOKS
    ► C++23 Best Practices
    Leanpub Ebook: leanpub.com/cpp23_best_practi...
    ► C++ Best Practices
    Amazon Paperback: amzn.to/3wpAU3Z
    Leanpub Ebook: leanpub.com/cppbestpractices
    JASON'S PUZZLE BOOKS
    ► Object Lifetime Puzzlers Book 1
    Amazon Paperback: amzn.to/3g6Ervj
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Object Lifetime Puzzlers Book 2
    Amazon Paperback: amzn.to/3whdUDU
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Object Lifetime Puzzlers Book 3
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Copy and Reference Puzzlers Book 1
    Amazon Paperback: amzn.to/3g7ZVb9
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► Copy and Reference Puzzlers Book 2
    Amazon Paperback: amzn.to/3X1LOIx
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► Copy and Reference Puzzlers Book 3
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► OpCode Puzzlers Book 1
    Amazon Paperback: amzn.to/3KCNJg6
    Leanpub Ebook: leanpub.com/opcodepuzzlers_book1
    RECOMMENDED BOOKS
    ► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
    AWESOME PROJECTS
    ► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp-best-practices...
    ► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices...
    O'Reilly VIDEOS
    ► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/...
    ► Learning C++ Best Practices - www.oreilly.com/library/view/...
  • Наука та технологія

КОМЕНТАРІ • 68

  • @sinom
    @sinom 7 місяців тому +24

    "NULL" is an undeclared identifier. That sure is something lmao

  • @flowname
    @flowname 7 місяців тому +29

    would be nice to see the same codebase migrated to C++20

    • @christer8964
      @christer8964 7 місяців тому +1

      That is the next step

    • @ohwow2074
      @ohwow2074 7 місяців тому +2

      Actually 23 would be interesting.

    • @cppweekly
      @cppweekly  7 місяців тому +12

      Stay tuned!

  • @mgostIH
    @mgostIH 7 місяців тому +7

    29:40 This is your brain on void* based polymorphism

  • @IndellableHatesHandles
    @IndellableHatesHandles 7 місяців тому +23

    I would've been scared to submit this to github before fixing it first. Given all the memory leaks it could probably be classified as malware

    • @marcs9451
      @marcs9451 7 місяців тому +1

      leaks should be avoided but they are not dangerous

    • @kodirovsshik
      @kodirovsshik 6 місяців тому

      ​@@marcs9451but it sounds funny

    • @Selendeki
      @Selendeki 6 місяців тому +1

      Modern operating systems are extremely robust and can handle leaky processes quite well. If the OS doesn't outright kill the leaky process and your system slows down you can still just go into task manager and kill it yourself.

  • @mjKlaim
    @mjKlaim 7 місяців тому +7

    That sent me through memory lane hahaha, VS6 was also my first C++ compiler acquired normally (I had a copy of some other compiler that someone gave me, couldnt manage to understand how to use it XD)

  • @not_ever
    @not_ever 7 місяців тому +1

    Even before you said Java out loud my brain had filled in the gap. I had a uni professor who taught Java before he taught C++, this code looks like something he would write. Memory leaks gallore in all the assignments because you weren't allowed to change his skeleton code.

  • @pkrysiak
    @pkrysiak 7 місяців тому +5

    27:05 sorry for the nitpicking, but std::stack has the equivalent of your "peek", it's just named differently - "top".
    Sure, infiz uses raw pointer instead of reference as a return type, but you're modernising anyway 😅

    • @cppweekly
      @cppweekly  7 місяців тому +4

      Fair, but I also had my `pop` return the popped object, so it would have been one more thing to wrap/change.

  • @trcwm
    @trcwm 6 місяців тому

    Great to see the code getting so much better, even in C++98! Can't wait for the next episode. Keep up the good work Jason. Regards, Niels.

  • @christer8964
    @christer8964 7 місяців тому +6

    Nice and you even kept it C++98!
    Creating pointers was so common back then. Did old C++ books even motivate when or when not to use pointers?
    I only remember pointers as a chapter in books. Best book I ever bought was Alexandrescu's Modern C++ Design. It truly was a game changer.

    • @axelBr1
      @axelBr1 7 місяців тому +1

      I certainly think that my initial C & C++ code, (started programming with these in early 2000s) heavily used pointers. When I got back to programming, 2019 time, I was surprised when reading up on heap vs stack that heap was preferred and faster than stack. I guess because C++ programming guides focused heavily on the use of pointers and all the dangers of using them that we kind of believed that we had to use pointers. Life is so much easier keeping everything on the stack.

  • @kinershah464
    @kinershah464 7 місяців тому +4

    I like the BGM in this video, nice music. And I liked the idea of cleaning up old code.

    • @mjKlaim
      @mjKlaim 7 місяців тому +5

      Thanks! :D (the music maker)

  • @hbobenicio
    @hbobenicio 7 місяців тому

    I really liked this format of code review!

  • @Skaaaaal
    @Skaaaaal 7 місяців тому

    Very instructive series

  • @LesleyLai
    @LesleyLai 7 місяців тому +2

    Nice! And you kept everything under VC6! I was wandering why you didn't use any auto for a while😅
    A couple of things I would do differeny:
    - I would pass rational numbers by value all the time. It is just two int
    - I would use hidden friends for operator overloading (though maybe VC6 doesn't even support friend)

  • @FryGuy1013
    @FryGuy1013 7 місяців тому +6

    I feel like I would've moved it to a modern platform before doing any of this refactoring.
    Also, core.autocrlf = false is the best setting. You don't need to mess with any .gitattributes files, checkout as-is, commit as-is, no weird CRLF mangling on windows.
    And man does this code need a formatting fix. I wish there were a universal opinionated formatter for c++ like there is with js with prettier/biome.

    • @bryce.ferenczi
      @bryce.ferenczi 7 місяців тому

      Some projects don't even use the .clang-format in the project root folder....

    • @not_ever
      @not_ever 7 місяців тому

      What do you find lacking in clang format or uncrustify?

    • @cppweekly
      @cppweekly  7 місяців тому +1

      I tried to be explicit that I left it in the old tools to prove that we COULD DO BETTER even back then!

  • @anon_y_mousse
    @anon_y_mousse 6 місяців тому

    This really brings me back to that time when I first started using C++. I too have a copy of VS6 from that time and one of the best features was having a local copy of MSDN. The first book I read on C++ didn't even mention templates since they weren't a standard language feature at that point. It was actually using templates and the STL, once they became a part of the standard, that convinced me to stick with C. I absolutely hated C++ for about a decade and a half, and it wasn't until seeing your videos and Rust that I started to like C++. While I don't use Windows anymore and would've used Vim to refactor this project, it was still a cool nostalgic trip seeing VS6 running.

  • @Yupppi
    @Yupppi 7 місяців тому +1

    I was thinking the "having to maintain this project" would've meant you get to upgrade your tools and everything, but you're stuck to C++98. But now I get why application folders are so full of garbage. It's like two calculation function files and a main and it expanded to multiple folders with their own set of files and possibly copies.

    • @cppweekly
      @cppweekly  6 місяців тому

      You might not necessarily get to update your tools, but you might still have to maintain an old project.
      I wanted to show what is possible before upgrading your tools. I'll get back to the project at some point in the future and let the tools get upgraded.

  • @DarKOscillator08
    @DarKOscillator08 7 місяців тому

    Oh waited for this vid :D

  • @PedroOliveira-sl6nw
    @PedroOliveira-sl6nw 7 місяців тому

    Brilliant!

  • @OlaInTheClouds
    @OlaInTheClouds 7 місяців тому +1

    This is interesting, you could do elegant things in C++98 as well and you were not forced to new things then either, and memory leaks wasn't untraceable, that was just bad code. But newer C++ helps in so many areas. Yea I've been stuck in old 98 code for a while and have been able to improve a lot.

    • @ohwow2074
      @ohwow2074 7 місяців тому

      C++98 was THE NEW thing back in 1998. The whole code you wrote was cutting edge stuff anyways so not many people could even do it. What do you mean by it didn't force us to newer things?!

    • @OlaInTheClouds
      @OlaInTheClouds 7 місяців тому +1

      @@ohwow2074 I mean there were no IDE support, static analysis and linters suggesting newer and better ways of doing things. Sorry if I was unclear.

    • @ohwow2074
      @ohwow2074 7 місяців тому

      @@OlaInTheClouds you see it all as a disadvantage?

  • @garrymarshall6331
    @garrymarshall6331 7 місяців тому

    I'll have to go look in the attic and see if I still have my Visual C++ 1.52 CD...

  • @simogasp
    @simogasp 7 місяців тому +2

    Now let's migrate to C++23! And everything must be constexpr. 🙂

  • @Lalasoth
    @Lalasoth 7 місяців тому +2

    I started with borland turbo c++ in i believe 95? I used borland up till 5.0 although I eventually switched to visual c++ 6 . I have projects from then that let me just say i cringe when i look at that code.

  • @kuberannaganathan5244
    @kuberannaganathan5244 5 місяців тому

    Great refactor but any tips on reviewing this type of code uplift?

  • @sqw33k
    @sqw33k 7 місяців тому +2

    Hmm, this music reminds me of something else but I just can't seem to remember. Anyone else's bells ringing?

    • @mjKlaim
      @mjKlaim 7 місяців тому +1

      If you remember I'm interested to know! :D (the music maker)

    • @phredrix-we7ks
      @phredrix-we7ks 7 місяців тому

      I'm not a gamer, but it has a bit of an 8-bit video game vibe to it.

    • @mjKlaim
      @mjKlaim 7 місяців тому +1

      @@phredrix-we7kshaha yeah that was the idea XD the sounds are chosen to give that vibe

  • @MichaelSchulzDaFritzmark
    @MichaelSchulzDaFritzmark 7 місяців тому

    So great! I love the music...

  • @ysakhno
    @ysakhno 7 місяців тому

    Cannot you open .dsp/.dsw files from VS60 in modern Visual Studios?

    • @Lalasoth
      @Lalasoth 7 місяців тому

      You can open them but you have to Migrate the project.. It won't compile anyway unless it's a very simple program. Guaranteed, there are problems. Then you will have to fix to get it to compile probably a lot

    • @ysakhno
      @ysakhno 7 місяців тому +1

      @@Lalasoth he had to do a lot of fixes anyway. All while working in sub par dev environment, by today's standards.

  • @GautamSharma-un3cr
    @GautamSharma-un3cr 7 місяців тому +2

    can you rewrite the entire thing in c++20/23?

  • @6754bettkitty
    @6754bettkitty 7 місяців тому

    20:52 Please const the things! 😂

  • @MenaceInc
    @MenaceInc 7 місяців тому +1

    "I'm just pretending like it's very bad code"
    You dont have to pretend :p

  • @6754bettkitty
    @6754bettkitty 7 місяців тому

    18:40 My answers aren't wrong, but they aren't right either. 😂

  • @garyp.7501
    @garyp.7501 5 місяців тому

    At 30:23 you ask yourself why you allocated a pointer to an int. The reason is that your old stack code held void *, so you needed a pointer to the heap not a pointer to a temporary on the stack.
    It's tough to follow all the things you do, because you move so fast, but I get it. Ya gotta keep us interested and a lot of this is just monkey work. ie any monkey at a keyboard could make these rote changes. Nice to see how far we have come though from "back in the days."
    My only other comment would be to add non existent copy constructors, assignment constructors and let the compiler show you where you are miss using the class, via the linker errors.

    • @cppweekly
      @cppweekly  4 місяці тому

      Tests! Start with the tests! Then you know the refactor is correct.

  • @jbs.
    @jbs. 7 місяців тому

    start with scan-make

  • @6754bettkitty
    @6754bettkitty 7 місяців тому

    29:42 😂

  • @EmperorPear
    @EmperorPear 7 місяців тому

    damn, im early

  • @srnissen_
    @srnissen_ 7 місяців тому

    One technique for adding header guards to A.hpp without changing it, is to write B.hpp, a file with only two things, (1) header guards and (2) #include . Then, include only B in your test.

  • @ClaytonAAlves
    @ClaytonAAlves 7 місяців тому

    Irritating music BTW

  • @ysakhno
    @ysakhno 7 місяців тому +1

    @39:23 why do you have 'break's after 'return's in 'switch' statements? You're doing a self-review before commit, which is a good practice (I wish some devs who I work with did it), but still you missed this obvious blunder.
    Also, I know how so many people with their "strong opinions" will come and tell me how I am wrong, but I think the first 'refactoring' thing that you should have done about this project, was to replace all tab-indents with space-indents. @39:55 lines 64 and 70 look just ridiculous. Not only they are indented poorly, but also use a mixture of tabs and spaces for indents. Ugh, yuck! 🤮

    • @not_ever
      @not_ever 7 місяців тому

      I thought this episode was about writing tests, not refactoring and presumably subsequent episodes are about more refactoring.

    • @anon_y_mousse
      @anon_y_mousse 6 місяців тому

      Hello, fellow space indentation user. Those are indeed tiny steps, and he could've done the first as a simple search and replace, which even VS6 had available. If it were me, I would've used Vim to refactor the project and downloaded a copy of gcc and checkout'd a period correct version. Then tabs to spaces would be an easy :%s/\t/ /g and done. But maybe that's just me that was thinking that.

    • @ysakhno
      @ysakhno 6 місяців тому

      @@anon_y_mousse tabs -> spaces is not as easy in this case, because a tab is such a 'unique' (by which I mean annoying) little character! 1 tab = 4/8/ constant spaces only when they appear at the beginning of the line (or are preceded by other tabs, that are at the beginning of the line). Otherwise, they just align at some tabstop interval, so can 'mean' anywhere from 1 to _n_ spaces (where _n_ is 4/8/ as mentioned above).
      VS60 did include the reformat feature, if memory serves, which would convert tabs to spaces (or vice versa) if the format style is configured to either use one or the other.

  • @HansJMicheelsen
    @HansJMicheelsen 7 місяців тому +2

    I can’t stand the music or whatever you call it. I’m out of here!😢