Tech Tutorial Tips to Train as a Code Cruncher. Extension of the Noble Career Hunter
Thursday, August 22, 2013
Code Concepts: What is Refactoring?
Basically, this makes it so that writing code DOESN'T have to be redundant and confusing. Imagine you had a robot that knew how to move forward, but could ONLY "turnLeft" (in hard, 90 degree angles for the sake of making this metaphor simple).
And lets say he's in some sort of maze like hall.
To get it out of that maze, say you had to beam it instructions to turn LEFT to begin -> move down the hall -> turn RIGHT at the fork it'll find -> keeping moving till the next fork -> turn right one more time -> and finally move forward to FREEDOM!
Since this gimped 'what-if' bot can only make left turns, you could mimic the effect of turning right manually in this manner (in psuedo-code. Again, for simplicity's sake):
Main Method <-where the actual instructions are followed
moveDownHall();
turnLeft();
turnLeft();
turnLeft(); //the robot is now facing to where his right should be
moveDownHall();
turnLeft();
turnLeft();
turnLeft(); //the robot is now facing to where his right should be
moveForwardToFREEDOM(); //so ends the maze
How turning LEFT three times leads to one facing to their RIGHT isn't too hard to imagine I'm hoping. And by the example written above, I hope its equally clear that, regardless of the language we're using, typing turnLeft(); over and over can get very redundant, VERY QUICKLY.
Hence the Object Oriented Programming (OOP) concept of REFACTORING to rescue us -
whereas to save us time, we write instructions we know we'll repeat a lot into what's generally known as private "helper" methods.
This enables us to beam the instructions as follows.
Refactoring Method "turnRight()"
turnLeft();
turnLeft();
turnLeft(); //the robot is now facing to where his right should be
Main Method <-where the actual instructions are followed
moveDownHall();
turnRight();
moveDownHall();
turnRight();
moveForwardToFREEDOM(); //so ends the maze
Talk about headaches saved, am I right?
~David Noble Morris~
*Please share and like if this has been helpful. Leave comments below if there's anyway I can help clarify.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment