Code Refactoring 3

[ad_1]
Get the Code:

In this part of the code refactoring tutorial, I show you examples of how you can make your code more understandable using something called an Explaining Variable.

We also explore many short cuts people take when writing code that will cause many problems later. I want to get these basic rules out of the way so we can concentrate on refactoring to design patterns very soon in this tutorial series.


Posted

in

by

Tags:

Comments

37 responses to “Code Refactoring 3”

  1. bhargav makwana Avatar

    Won't writing the code into more readable form won't increase the no of variables and then increase the size of code? I mean if the code is big enough.

  2. poo foo Avatar

    hahah, you mean soccer?

  3. Lester Avatar

    Code Should Read Like a Sentence. I like this idea !!

  4. ameer adel Avatar

    i am C# programmer and man these videos are useful! 
    thanks Derek.

  5. Ahmed Abida Avatar

    Thank you Derek for these great tutorials.
    In these video you declare final variables inside of a for loop. That declaration force the JVM to create multiple pointers that are used just one and it could influence application performance. What do you think Derek about this? Are your code is aware about that?
    Thank you for answering

  6. мирослава кияко Avatar

    Sorry, but I disagree with your variable names here. The names like over25products don't show here their real meaning, because such names don't consider a price which is used during calculation of conditions. Sorry for my english

  7. Vahx Avatar

    Doesn't the rule of "not re-using temp variables" lead to the use of more temp values which means that your system will need more memory to process?
    And for the simple example that you gave, would you really add extra temp variables?

  8. WNxLigerzx Avatar

    What's a good rule of thumb for balancing between not refactoring these variables vs. having a ton of temporary final variables at the top of a method?

  9. Sasori Fuwa Avatar

    So, what is the rule of thumb for introducing method vs introducing variable and the swapping between two of them?

  10. terroare Avatar

    This topic is much closer to real life, then the football one.

  11. Federico Jimbo Smithson Avatar

    Since the goal of refactoring is entirely human-centric (easy to edit, read, etc.) and as other comments said, oftentimes counter-efficient (allocating temporary multiple variables), why not just put a comment on top expressions for explanations??

  12. Derek Banas Avatar

    You're very welcome 🙂 Thank you for the tips

  13. Hard Yakka Programmer Avatar

    Thanks Mate, for heaps of wonderful kung-fu tutorials for programming.

    Also sometimes it can be quite CPU friendly to save the output of a function in a temp variable and use them rather than calling exactly the same function over and over and over again. Basically when calling functions from slow dynamic libraries or calling any function that does heavy computing.

    I reckon it is a very good practice. Thanks Again.

  14. Derek Banas Avatar

    I don't know how to get the text from the videos. I'd love to help do that. I wouldn't care if you embedded my videos and changed them to arabic and the code as well. I just have no idea how to do that. Sorry, but I'm not very good with translation

  15. Waseem Abuzenah Avatar

    Hey Derek can you email me all of your subtitle ?
    so i can translate it into Arabic so you can embed the new cc into your videos
    and embed the translated videos into Arabic blogs as well
    so we can reach large Arabic audience and i can help you with that
    if you like the idea you can email me
    wase39 at gmail 😀

    thanks

  16. Marinca Gheorghe Avatar

    if you have 3 variables in a local scope it doesn't mean your program uses more memory necessarily. When compilers generate the assembly code that gets executed on the target processor (or in this case the JVM) they usually optimize things like making use of registry, so no actual memory location is being used.

  17. Hedi Radhouane Avatar

    and the compiler takes care of the optimizations way much better than most of the programmer. refactoring is about having a code more understandable hence less bug. a compiler can't find the bugs.

  18. Derek Banas Avatar

    Thank you 🙂 refactoring is all about making code easy to understand so it can be extended without causing performance issues in the future. I normally optimize code once I'm sure the code doesn't need to change

  19. Young .Krieger Avatar

    Another great tutorial !

    One question though
    10:40 indeed makes it more understandable, however doesn't it mean that we are allocating memory for 3 variables instead of one, i.e. using more resources, which I guess is bad practice, if it can be avoided.

  20. Derek Banas Avatar

    Thank you 🙂 we all have a great deal to learn. I'm very happy if I'm able to help

  21. Mahesh M Avatar

    Derek, You are just awesomeeeee!!! I am sad that i did not find your tutorials much before 🙁
    Now i undestood thr is lot of bad smell code i have written 😉 Atleast now on i am gonna fix it right.. Thanks a ton 🙂

  22. Derek Banas Avatar

    Thank you 🙂 this is high level thinking, but I love teaching it

  23. SynforgeGames Avatar

    Awesome series, I can see it being very useful

  24. Mladen Uzelac Avatar

    There is a course Algorithms I on Coursera that starts on 4th of February.
    All lectures and assignments are based in Java. 😉
    The course is off course FREE. 🙂

  25. Derek Banas Avatar

    Thank you 🙂 It all comes down to personal preference. I like you, like long descriptive variable names. They however drive some people crazy. I'd say if you are working on your own, do what works best for you, but in teams you sometimes have to satisfy the group

  26. Steven Liekens Avatar

    Great series, but the one thing that keeps bothering me is the use of abbreviations for variable and method names. For example, personally I find it much easier to read "getTotalPrice" than "getTotPrice", and this is still well below the maximum allowed length. So why do people do this?

  27. Derek Banas Avatar

    Thank you very much 🙂

  28. HGeneAnthony Avatar

    I do love your stuff, very high quality. Looking forward to algorithms. Loved your stuff on design patterns.

  29. Derek Banas Avatar

    That is the next tutorial 🙂

  30. Derek Banas Avatar

    Thank you 🙂 I'm glad my community enjoys these topics

  31. Anderson Neo Avatar

    Thanks Derek :)..Take Your Time!

  32. azhd210 Avatar

    Fantastic video clear and to the point 😀

  33. Derek Banas Avatar

    It is in the works right now 🙂 I can't wait

  34. Anderson Neo Avatar

    I shall be waiting for c and java potpourri for games 🙂

  35. Anderson Neo Avatar

    You are awesome Derek!! … Thanks a lot your vids.. 🙂

  36. Derek Banas Avatar

    I'm very happy you see the value in these videos 🙂

Leave a Reply

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