The Art of Refactoring

21
The Art of Refactoring

description

The slides for a presentation of mine on various aspects of Ruby code and things to look for to help improve the readability, efficiency, and maintainability of ones code.

Transcript of The Art of Refactoring

Page 1: The Art of Refactoring

The Art of Refactoring

Page 2: 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

Page 3: The Art of Refactoring

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?

Page 4: The Art of Refactoring

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.

Page 5: The Art of Refactoring

OMG, that code is $&*#ing horrible!!!

Page 6: The Art of Refactoring
Page 7: The Art of Refactoring
Page 8: The Art of Refactoring

Best PracticesWorst Practices

Page 9: The Art of Refactoring

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.

Page 10: The Art of Refactoring

Improper use of globals

Class name in class methods

One or two character variables

Unnecessary iterations

Multi-purpose methods

Things to NOT do

Page 11: The Art of Refactoring

Classes as data structures

Object misuse

Memory abuse

Less Obvious Bad

Page 12: The Art of Refactoring
Page 13: The Art of Refactoring
Page 14: The Art of Refactoring
Page 15: The Art of Refactoring
Page 16: The Art of Refactoring

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.

Page 17: The Art of Refactoring

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.

Page 18: The Art of Refactoring
Page 19: The Art of Refactoring

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

Page 20: The Art of Refactoring

Homework: Ruby Fiddle!!

Page 21: The Art of Refactoring

Thanks and keep hacking!