Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Version control of software refactoring

What is the best way of doing version control of large scale refactoring?

My typical style of programming (actually of writing documents as well) is getting something out as quickly as possible and then refactoring it. Typically, refactoring takes place at the same time as adding other functionality. In addition to standard refactoring of classes and functions, functions may move from one file to another, files get split and merged or just reordered.

For the time being, I am using version control as a lone user, so there is no issue of interaction with other developers at this stage. Still, version control gives me two aspects:

  1. Backup and ability to revert to a good version "in case".
  2. Looking at the history tells me how the project progressed and the flow of ideas.

I am using mercurial on windows using TortoiseHg which enables selections of hunks to commit. The reason I mention this is that I would like advice on the granularity of a commit in refactoring. Should I split refactoring from functionality added always in committing?

I have looked at the answers of Refactoring and Source Control: How To? but it doesn't answer my question. That question focuses on collaboration with a team. This one concentrates on having a history that is understandable in future (assuming I don't rewrite history as some VCS seem to allow).

like image 392
Muhammad Alkarouri Avatar asked May 29 '10 17:05

Muhammad Alkarouri


People also ask

What does refactoring mean in software?

Refactoring is the process of restructuring code, while not changing its original functionality. The goal of refactoring is to improve internal code by making many small changes without altering the code's external behavior.

What is the process of refactoring?

Refactoring is the process of changing a software system so the software's structure and performance are improved without altering the functional behavior of the code. Refactoring is used to improve system maintainability and extend its usable life span.

What is refactoring in software maintenance?

A refactoring action modifies the structure of the code without changing the external functionality of the program. When the refactorings are applied to the software they may either improve or impair the quality, but regardless, they act as tools used to modify the solution.


3 Answers

I suppose that there is no one size fits all answer to you question :)
Personally I prefer to keep the finer sensible granularity in my commits: in your case I would split the action in two phases: each one independent:

  1. refactoring (and following commit)
  2. new functionalities (and commit).

The best thing to do is to add and commit each item on its own: break up the refactoring in localized changes, and commit them one by one, and add the functionalities one by one, committing them along the way.

There is a little more overhead, but in this way when you go back seeking for differences it is clear what was changed for refactoring and what for adding new functionalities. It's also easier to rollback only a particular problematic addition.

like image 146
garph0 Avatar answered Nov 02 '22 23:11

garph0


Should I split refactoring from functionality added always in committing?

I tend to check-in frequently; and each check-in is either refactoring, or new functionality. It's a cycle of:

  1. Refactor existing code (without changing its functionality) to ready it to accept the new code
  2. Add new code (which implements additional functionality).
like image 23
ChrisW Avatar answered Nov 02 '22 23:11

ChrisW


I would recommend separating refactoring from adding functionality. Perhaps by alternating checkins. This is from my experiences after I discovered uncrustify and would reformat source files while also making code changes. It became very difficult to figure out a real change from just a reformat. Now uncrustify gets its own dedicated commits.

like image 25
Amardeep AC9MF Avatar answered Nov 02 '22 23:11

Amardeep AC9MF