Are You Refactoring Code For The Right Reasons?

Поділитися
Вставка
  • Опубліковано 11 чер 2024
  • It's tempting to jump into refactoring code that's hard for you to understand.
    But before you take that leap, you should probably consider that there can be some dangers.
    Whether you're coding in Java, Javascript, C#, Python, or any other language - there are non-technical implications of code refactoring that you should be aware of.
    In this video, I share 6 considerations to take into account that will help you avoid getting into a stressful situation when you choose to refactor code.
    Subscribe for more videos about Healthy Software Development: ua-cam.com/users/JaymeEdward...
    Related Videos:
    "Are You A Perfectionist Programmer?"
    • Are You A Perfectionis...
    "Software Developer vs Consultant - What's Better For YOU?"
    • Software Developer vs ...
    "An Agile Budget Keeps You From Being A Code Monkey"
    • An Agile Budget Keeps ...
    "Technology Addiction: Materialism in Software Development"
    • “New Framework Disease...
    "Democratic Software Architecture: Sharing Technology Decisions"
    • Democratic Software Ar...
    #programming #refactoring #code
  • Навчання та стиль

КОМЕНТАРІ • 38

  • @MajeureX
    @MajeureX Рік тому +12

    There's no harm in refactoring code if you helps you understand it. For me I find this more effective than reading the code with touching it. Nothing says you have to commit or push the code to the master branch. Some call this 'scratch refactoring'.

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

      Haha. Funny comment. Sorry of an oxymoron haha. You make a good point though 😊.

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

    Just tripped over this older video, but it's something relevant constantly. One thing might be "do I need to understand the code" - rewriting it can help me check that my understanding is correct. Another is how much churn does the code have - if it's being constantly touched, i'd ask if those changes are particularly hard and if restructuring the code would make that easier. If it's not being touched, I'd ask if anybody understands it and if they're avoiding touching it in ways that create more work.
    Sometimes it's ok to have a box of black magic in the corner that does what it's supposed to do and nobody has to change it.

  • @ShotgunAFlyboy
    @ShotgunAFlyboy 6 місяців тому +4

    Sometimes the code *is* just wrong though. Like, I've had to rewrite code occasionally written by people that did not really understand the language they were assigned to work in (and had no interest in learning). This is on a product I'm the final butt on the line for though. My goal is never to make it so I can understand it, it's to make it so my juniors can understand it. Gotta make sure the code is well tested beforehand though, so you don't miss those edges.

  • @HealthyDev
    @HealthyDev  6 років тому +1

    Have you had to decide whether to refactor code you don't understand? How did you deal with it?
    Skip to points:
    02:12 How Valuable Is The Change?
    03:19 How Much Time Is Needed To Refactor?
    05:29 Will Refactoring Handle The Hardest Case?
    08:30 Are You Willing To Get Permission?
    10:42 Are You Willing To Teach Everybody?
    11:59 Are You Willing To Work Overtime?

  • @pavelperina7629
    @pavelperina7629 3 роки тому +10

    Without watching more than one minute:
    * If code works and seems maintainable, don't touch it
    * If code somewhat works and seems hard to maintain, it may have some many dependencies across the project that you can't refactor it and you are doomed to stick with it
    * If person who wrote this code is still in the company, try to understand his motivation for writing it "wrong" and try to explain your points how to write it "better" and educate him, maybe the biggest piece of code he wrote was his diploma thesis with 2000 lines and he will respect you.
    * it's much more important to have clear and easy to understand interfaces than clean code. If this is messed up, it's failure or lack of experience of the whole team

    • @ianedmonds9191
      @ianedmonds9191 3 роки тому +1

      Most of my software projects has been teams of 1-4 programmers with a varying amount of designers, PM or POs .
      Some have felt like a team (pivotal engagement) Others have felt like a bunch of competing groups covering their arse.
      The teams work better.
      I'm not sure what the take away is for management but it has something to do with Creative people (devs) don't work well in a culture of pressure, monitoring and blame.
      To be actually creativity writing code we need to feel free and creative. That only happens when we don't feel threatened.
      Luv and Peace

    • @brendanalberto8196
      @brendanalberto8196 3 роки тому

      @Benedict Ali definitely, I've been using flixzone for months myself :D

    • @paulcullen5712
      @paulcullen5712 3 роки тому

      @Benedict Ali yea, I have been watching on flixzone for since november myself =)

  • @ianedmonds9191
    @ianedmonds9191 3 роки тому +5

    I got handed an access database which had no constraints or keys and a bunch of remote tables in an SQL database that also had no restraints or keys.
    I'd been a .net and SQL programmer for ten years.
    It had been made by some nut job outside of IT and when he left it was passed to IT to support.
    It was responsible for bidding for work based on part cost plus a multiplier. Really shonky stuff that was flawed in every way.
    I looked at the access database and it was truly horrible. The amount of duplicate records was crazy and the disconnect between tables was off the scale.
    There were orphaned records everywhere and even the records that were connected via crappy natural keys were suspect.
    Loads of duplicates and no sense of normalisation.
    Some of the queries were so badly written they were counting some of the costs multiple times.
    I have no idea how we stayed afloat with such a dysfunctional tender system for so many years.
    I told my boss it was a dead duck and that we should not maintain it.
    She insisted we need to fix but asked me to do a report on how broken it was.
    I did the report with a scenario where we over bid at 4 pieces for every one in the spec. It could have gone to more if more tables were involved. The database was a complete shitshow.
    They retired the app shortly after.
    Absolute nightmare of a project.
    Luv and Peace.

    • @akivaliaho5166
      @akivaliaho5166 3 роки тому +2

      Ahh, I've seen this same crap many times. 10k row long sql queries with hundreds of tables being joined. No normalization, high amounts of duplication. Most of the tables didn't even have primary Keys. This was the most vital database system in the whole company. Everything depended on it to be correct. Management was hovering behind our backs asking for statuses every 15 minutes.

    • @akivaliaho5166
      @akivaliaho5166 3 роки тому

      The funny thing is: when you're building new stuff the shittiness of this situation reeks through the ancient integration points and spoils your new code too.

  • @SlimAgnus
    @SlimAgnus 6 років тому +6

    In 02:12 you ask how valuable the change is, if it's in a piece of code that doesn't have much impact.
    If the code is too complex or too messy for you to understand what it does, without refactoring it, how will you determine if it's worth refactoring?

    • @HealthyDev
      @HealthyDev  6 років тому +6

      Great point. You really can’t in this case. I guess my point was even if you can only go by guy feel, to tread lightly on a low value product. As always, just my opinion.

  • @pavloburyanov5842
    @pavloburyanov5842 День тому

    Sometimes its impossible to go further without refactoring.

  • @xTwisteDx
    @xTwisteDx 7 днів тому

    Oh I’ve 100% come upon code that was unreadable, and determined it was hot garbage, and refactored it. Absolutely they had done it wrong xD

  • @leapingblackcat
    @leapingblackcat 3 роки тому +2

    You change it. You own it.

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

    Hello good sir. Are you familar with C++ and if so, how comfortable are you with utilizing the languages pros such as inheritence, encapsulation, and polymorphism?

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

      Hey Jason. I haven’t done much C++ since the early 2000s. I’m very familiar with those basic OO patterns but primarily used them with Java and C#.

  • @kdietz65
    @kdietz65 3 роки тому +1

    Oh this sounds like a super fun game. Let's play "would you refactor this code?"

    • @kdietz65
      @kdietz65 3 роки тому

      Ah, dangit, UA-cam doesn't allow image attachments in comments. That will make it harder for me to attach sample code.

    • @kdietz65
      @kdietz65 3 роки тому

      Does it do any code formatting?
      ```
      Line 1
      Line 2
      Line 3
      ```

    • @kdietz65
      @kdietz65 3 роки тому

      Well here is just one very short example:
      string strCssClass = (Height == 230 ? "HBCirChartLayout" : "HBCirTopChartLayout");
      I gotta find some way to share this. It's just too good.

    • @kdietz65
      @kdietz65 3 роки тому +1

      For starters, color me this here Batman - Shouldn't the CSS class applied to an element control the height of the element? No sir. Here, they look for the height of the element, and based on the height, they apply one CSS class or another. They check if the height is 230, and if it is, well that's obviously one of the charts that goes in the bottom row, so it gets HBCirChartLayout styles. But if the height isn't 230 (which is to say the height is 178, because that's the only other height a chart could be -- oh, there were checks for that around the code too) then it must be in the top row, and if its in the top row, then it should get the HBCirTopChartLayout styles. Makes perfect sense, right?
      This is just the beginning of tens of thousands of lines of some of the weirdest, most fragile, most backwardly organized code that I've ever seen. It had not just dynamically generated HTML, but dynamically generated JAVASCRIPT! Code generated from the server, with logic that could vary based on server-side parameters, that was then sent to the browser and executed. Have fun debugging that.

    • @kdietz65
      @kdietz65 3 роки тому +1

      Oh, I just remember this too, this is good. How do you suppose the code ever figured out whether the Height should be 230 or 178? That got set in the chart's constructor. Now the way that worked was based on the rendering sequence of the 4 charts, which were rendered in a 2x2 matrix. The rendering order was 1) Top Left, 2) Bottom Left, 3) Top Right, 4) Bottom Right.
      So there was an integer session variable that got modified in the charts constructor. They'd flip the sign of it back and forth from negative to positive. If it was positive, they knew it was the top row, and if it was negative, they knew it was the bottom row.
      Therefore, the constructor code looked something like:
      Height = (Session["flag"] == 1) ? 178 : 230;
      .
      . More stuff
      .
      Session["flag"] = -Session["flag"];

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

    To refactor code you have to understand it first anyway 😅

  • @bradenbigham2769
    @bradenbigham2769 2 роки тому

    yandare dev needs refactoring.

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

    If you don't understand the code, read it first and play around with it to understand it.

  • @JM-jc8ew
    @JM-jc8ew 6 років тому +2

    Great Video Jayme,
    Good to have you back, was worried, you were releasing videos less often.
    I got in trouble in the past when refactoring, if I do not think the same way, as the programmer who did it.
    I got issues with female programmers ha ha ha!
    Before refactoring codes, I hit the "blame/show annotation" feature, and if the committer is a female then "Abandon ship!! I repeat, Fall back! fall back! retreat! retreat!".
    (Believe me, I've seen twisted things out there, this will save you, a lot of time and trouble, specially on a legacy codebase with poor implementation of encapsulation/basic OOP concepts).

    • @HealthyDev
      @HealthyDev  6 років тому +9

      Thanks for the feedback. I’m not sure why you’d have any more issues with programmers who are women than men.

    • @HealthyDev
      @HealthyDev  6 років тому +2

      Oh by the way I’m moving to a Tuesday and Thursday publishing schedule starting next week (if I do live interviews or have guests this would be on Saturday).
      Yeah last week we had some family in town and I had to put the channel on hold for a bit.

    • @hanktothehendrix
      @hanktothehendrix 3 роки тому +9

      This comment is bizarre.

  • @alexxx4434
    @alexxx4434 Рік тому

    If it works - don't touch :D

    • @godmode3611
      @godmode3611 5 місяців тому +1

      This is how a Junior thinks 😅

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

      @@godmode3611 Quite the opposite, it's how a veteran pragmatically thinks 😂. Junior's naive idealism drives him to redo things, thinking they can do better (while often not actually comprehending the full complexity).

    • @cadaankaa
      @cadaankaa 4 місяці тому +1

      I rarely find code to be classifiable in such a black/white form. Don't be too quick to refactor, especially if you're new to the system. But sometimes it's worth the effort and risk.

    • @pavloburyanov5842
      @pavloburyanov5842 День тому

      but what if you need?)