The Art of Refactoring
-
Upload
drizzlo -
Category
Technology
-
view
302 -
download
2
description
Transcript of The Art of Refactoring
The Art of Refactoring
Me
Polyglot :C, C++, Ruby, Java, Scala, JavaScript, Go, Python, etc.
Senior Software Engineer :Over 13 years of experience professionally.
Math Nerd :Discrete Math (Graph Theory, Number Theory, Information Theory, Model Design, Algorithm Analysis & Design
Daniel Marvin [email protected] http://daniel.cloudpack.io
Amateur Wormhole Theorist :The speed of light just isn’t fast enough.
http://capterra.com
In Computer Science, as we evaluate and analyze algorithms, we consistently ask ourselves, “Can we do better?”
We will ask ourselves a similar question in regards to refactoring.
But what is our goal? Why does it even matter?
Can We Do Better?
What’s the Point?
While huge improvements have been made with tools for solving problems, we want to ensure we get the most out of our code.
We want to make sure that others can read and understand our code — including others like our abstract self — days, weeks, or months after having written the code.
We want to make sure that we can easily add to our code without needing to re-write massive portions.
OMG, that code is $&*#ing horrible!!!
Best PracticesWorst Practices
Many Ways
Ruby offers us numerous ways to accomplish the same tasks and this can be good or bad. !
All code is just code. !
However, somethings you don’t want to do.
Improper use of globals
Class name in class methods
One or two character variables
Unnecessary iterations
Multi-purpose methods
Things to NOT do
Classes as data structures
Object misuse
Memory abuse
Less Obvious Bad
Memory Usage
Because Ruby makes things very easy for us, it is also easy to take for granted not needing to manage memory, however when it comes to optimizing code, it is very helpful to have an understanding of the Garbage Collector, how Ruby objects function under-the-hood, and how data is moved around.
PbV or PbR? Not beer.
One important thing to know is how Ruby passes around data. !Pass-by-value : A copy is made. Pass-by-reference: The memory location is referenced. !C Ruby does a combination of both! !J Ruby does a combination of both too, however the way the JVM’s GC works is a bit different, so we’re not going to chat about that today.
value = “this is a string”
value.class #=> String
value.object_id #=> 70198768696180
printf("0x%X", (value.object_id << 1)) #=> 0x7FB0D957F6E8
MEMORY BLOCK: 0x7FB0D957F6E8
We Have Ways Of Making Objects Talk
Homework: Ruby Fiddle!!
Thanks and keep hacking!