Code Refactoring 2

[ad_1]
Get the Code Here:

In this part of my code refactoring tutorial I talk about some basic concepts you have to understand. I’ll cover the process of method extraction. This will help you break down many lines of code into understandable blocks. I’ll also cover when not to extract methods and how to handle temporary and local variables.

This tutorial series starts off slow, but by the end many complicated code refactorings will be covered.


Posted

in

by

Tags:

Comments

46 responses to “Code Refactoring 2”

  1. PrinzCharming123456 Avatar

    Cool tutorial, thanks. But why is everything public? Defenetly not clean code. And why don´t you use refactoring features of the IDE?

  2. Kushal Patel Avatar

    Would a good reason to use temp variables be when they act as intermediate values for some algorithm? I think a well-named temp could make code more readable if each method in an algorithm is assigned a variable with a good name.

  3. Boris Grunwald Avatar

    refactoring is so satisfying!

  4. another1bitethedust Avatar

    Am I late for questions? 🙂
    When extracting methods, it makes sense to set support function (like print dashes function) to private instead of public? What's the best practice, if any? THX

  5. vocalists5555 Avatar

    avgdashtime is a noun. Add "get"

  6. Amanze Ogbonna Avatar

    Saving the returned value from a method into a temp, so you don't have to call that method all the time. I don't understand why that's a bad thing

  7. Ali Hakem Avatar

    u use so difficult example in u explain

  8. poo foo Avatar

    Wow, was only 2nd tutorial in I realized this was Java…. Pretty much the same syntax as C#

  9. Riju Nayak Avatar

    Would be helpful to mention when temp variables are useful such as to store old values of variables which will certainly change in the future.

  10. Martin Main Avatar

    For the part about Temporary variables, I would definitely agree when there is a single occurrence, but I would argue that it may be good to use them with even 2 occurrences or more, if performance is important, and the function being called takes significant processing power. If the function takes 2 minutes to run and will be returning the same value each time, you might as well store that in a temp, even if it's only called twice. 2 minutes is better than 4.

  11. KaletheQuick Avatar

    This is delicious.

  12. ThundersLeague Avatar

    Interesting topics. While I agree with most of what you say, I don't really agree with the removal of temporary variables.

    For example, if you have to apply a condition to the return value. Soccer example: Print the average number of goals scored per match of the teams who have an average number of goals scored  per match less than 1.
    if (team.averageNumberOfGoals() < 1.0) {
        print(team.getName() + team.averageNumberOfGoals());
    }

    Now there are 2 calls to that function.

    Yeah, in this case you could say: only use a temporary if you would call the function more than once. But maybe later the code changes so I only need to execute the function one (Ex: Only print the name of the teams). Then a day later I need the average again. So you'd be adding and removing temporary variables each time. This is the same as the "{ }" dilemma all over again. (Should I add brackets or not if I only execute a single line after an if-statement).

    A further reason to why I like to use temporaries is that most IDEs, when paused at a breakpoint, usually list the local variables, so I can quickly see the received value and I don't have to rely on other ways of checking that value, like adding a print statement or add a breakpoint before the return statement of function I'm calling (difficult, when working with frameworks).

  13. Vahx Avatar

    Its nice that you show examples of when to use this but also when not to use this.

  14. hulsevictoria Avatar

    You have made code refactoring easy to understand. Cheers.
    I love your accent too 🙂

  15. Amounx Avatar

    Wouldn't this make your code more confusing in the end because then you always have to try and find that method and in the end your going back and forth between all these methods with a few lines?

  16. Nicolas Magee Avatar

    @Kevin Bigler
    I'm not sure but I think the compiler might make the functions inline functions and make it exactly equivalent in the end as far as speed goes.

  17. Nicolas Magee Avatar

    Do you really need so much whitespace. Is there a reason to only write on even lines?

  18. Derek Banas Avatar

    That is very nice to share that 🙂 I'm very happy to help.

  19. Karthik Rangaraju Avatar

    Everyday morning, when I come to office, I learn something from you for half an hour, and then kick off my day !

  20. Derek Banas Avatar

    You're very welcome 🙂 Yes I should have mentioned more in this series that these are just a few ideas and that they shouldn't be used if they make the code less flexible or less understandable

  21. minusSeven Avatar

    Thanks for the great videos . 1 thing I would like to point out about replacing temp variables with method calls is that it is applicable as long as every new call to the method does not change or return a different value. Sometimes you need to add temp variables when a new method call might return a different value. This is point is crucial.

  22. Derek Banas Avatar

    Thank you 🙂 I try to do my best

  23. andefghi Avatar

    I'm surprised by the quality of these videos

  24. Derek Banas Avatar

    Thank you very much 🙂 Yes you had a great idea

  25. Subba Rao Thirumoorthy Avatar

    Hi Derek,

    Thank you for the awesome videos 🙂
    I have question though, rather than hard coding the 4.41 wouldn't it be better to have returned as a call from a method [MinAvgTime()] or make it a read only variable. That way it makes it easier to change in the future.

  26. Derek Banas Avatar

    I'm not sure why I did that? You're right that it would have probably made more sense as static

  27. John Morales Avatar

    Hi Derek, I just noticed, ur not utilizing the use static(variable/method) an example is the printTitles() that should be a static method.

  28. Derek Banas Avatar

    Thank you very much 🙂 I try to do my best. Many more videos are coming

  29. Dmitry Avgustis Avatar

    You are a digital Jesus! Please, never stop doing these tutorials 🙂

  30. Derek Banas Avatar

    Thank you for a great comment that adds to the overall content! I greatly appreciate that 🙂

  31. Derek Banas Avatar

    Thank you very much 🙂 I do my best to cover topics ignored by most seriously. I'm glad you like them

  32. Herbi Shtini Avatar

    Your playlists are really priceless.

  33. Derek Banas Avatar

    Yes I agree. I just wasn't focused on that in this video for some strange reason?

  34. Trevor Elkins Avatar

    Came in to mention this. The methods you made in this video really should be private to increase encapsulation, and later made public if there is an actual need for that. Right now you are polluting the API with these helper methods. Perhaps this can be changed in the example code?

  35. Hedi Radhouane Avatar

    You should use a debugger so you don't have the need for temp variables and you can see a bunch of details such as data value of course but also call stack or memory usage.

  36. Yolo Selski Avatar

    These series are amazing.

  37. Derek Banas Avatar

    Starting and stopping threads can get quite expensive. I'm not quite sure what you are doing, but saving data in a variable on occasion would be better than executing a separate method constantly in a loop. Sorry, I'm confused

  38. Kevin Bigler Avatar

    I knew that going into it, that was kind of the point. Its fairly accurate, well I use it any way, but my question was regarding code refactoring. Would it be more efficient to call the method that calculates note duration every loop instead of storing the value in a final long variable until it changes? When the value changes the thread stops and restarts so the value of the field is replaced by the new note duration.

  39. Derek Banas Avatar

    You may want to go with a language like c for this type of work. Java isn't particularly good for precise work like this

  40. Kevin Bigler Avatar

    What if speed is a factor? I made a metronome that required very precise timing, I assumed it would be faster to store the note duration in nanoseconds in a final long variable before the loop started rather than calling my caclulateSleepTime(int BPM) method every loop. Is that not the case?

  41. Derek Banas Avatar

    I have a ton of composition books filled with notes from numerous books, websites, etc. sort of like the Kevin Spacey character in Seven 🙂 That is where my videos come from

  42. PlanetUber Avatar

    Other books? I will take a stab in the dark and say Clean Code by Robert Martin (uncle Bob)? Another awesome book. If you haven't read it yet, I would suggest you do. I think these sort of practices are what can turn a good programmer into an awesome programmer. I know it has changed the way I code and also changed the way I think about code before I even write the first line of a new project.

  43. Derek Banas Avatar

    Thank you 🙂 I took some from Fowler and the other books and mixed in my own examples and notes. I'm glad you like it

  44. PlanetUber Avatar

    Nice videos so far. I am really enjoying them. I am currently reading Refactoring by Martin Fowler so this is an awesome compliment to the text. Well done!

Leave a Reply

Your email address will not be published. Required fields are marked *