Comments by themselves are not bad. The problem is that we often do not write clear code (because it is hard), so we take a shortcut (use comments) to hide the problem. Later, as the code is updated, commonly nobody updates the comments accordingly. In time, opposed to making the code easier to read, these outdated comments will actually mislead the readers. At the end of the day, what we have is: Some code that is unclear by itself, mixed with some incorrect comments.
Therefore, whenever we see a comment or would like to write one, we should think twice: Can the comment be turned into code, making the code as clear as the comment? You will find that in most of the time the answer is yes. That is, every comment in the code is a good opportunity for us to improve our code.
This is one of the many refactoring techniques out there that help us to make our code to look good.
The process of doing it is very simple but the why behind it sometimes make people to think twice about executing it. Extract method as the names suggest extract a piece of code and create a new method from it.
Probably like most people you are saying to yourself, create a new method just for one line of code? And the answer for that question is ‘maintainability’. Remember that the art of refactoring refers to improve the design of existing code for better maintainability and if you have a code base that is easy to read then you will accomplish that goal.
The following are the steps to do an extract method:
- Identify the code you want to extract, and comment it out.
- Think of a name for the new method and create it as an empty method.
- Place a call to the new method in the old method.
- Copy the code that you want to extract into the new method.
- Rely on the compiler to find out what parameters you’ll have to pass and what values you’ll have to return.
- Adjust the method declaration to accommodate the parameters and return value (if any).
- I hope you have test (This is the time to run it).
- Delete the commented code.
Most modern IDE have this refactor in it’s refactoring menu, just hilight the code and hit a shortcut. In eclipse it’s SHIFT+ALT+M. Till next time.
This is one of many more techniques that i will try to share every time i find a new one, stay tune for more. But first we all should know what is code refactoring.
Code Refactoring is the process of changing a computer program’s internal structure without modifying its external functional behavior or existing functionality, in order to improve internal quality attributes of the software. Reasons include to improve code readability, to simplify code structure, to change code to adhere to a given programming paradigm, to improve maintainability, to improve performance, or to improve extensibility.
The first technique i will show is called Chain Constructor. It’s consists in the elimination of duplication in constructors in a class through chain calls to one constructor in the class.
When constructors in a class implement duplicate work, the code fails to communicate what is specific from what is general. This mean is easier for us to communicate intention. Duplicate code in your constructors makes your classes more error-prone and harder to maintain. If more than one constructor contains the same code, is harder to see how each constructor is different, simplify your constructors by chaining calls to one general purpose constructor.
Here are the steps to do a chain constructor refactor in your code:
- Find two constructors that contain duplicate code. Determine if one of the constructor can call the other one, so that the duplicate code in one constructor can be easily erase from one of the two constructors.
- Compile (And i hope you have test) and test.
- Repeat the first and second step for all constructors in the class, including the one you already touched in order to remove all possible duplication.
- Change visibility of all constructor that may not be public.
- Compile and test.
And that’s all, happy refactoring.
History is what people don’t study this days and i think is the best way to make predictions about the future. We tend to repeat our mistakes over and over just because we tend to forget what happen in the past, without noticing that we are losing our own perception of the future.
Who controls the past controls the future: who controls the present controls the past. -George Orwel
I noticed a behavior from a friend, she likes to read about the author past while reading one of his/her novels/poems (My friend is into Literature). The best way to learn about the author intention on any subject is locating our selfs in the exact place the author was and that we can just do it through his/her history. I thought that probably i can learn new things about what’s happening now in computer science if i do the same with good programmers. Maybe even do my own predictions for the future :). I’m planning to take at least 10 to 20 minutes of my day to read an article of any good craftsman in computer sciences. Probably i will not get wisdom right away from that activity but i know that i will get inspiration.