Книга: Embedded Linux development using Eclipse
4.8 Refactoring
4.8 Refactoring
Refactoring is generally described as a process of restructuring code for the purpose of readability, performance improvements, reuse, or simply for the “regular evolutionary quest for elegance.”[4] Most programmers engage in a process of refactoring without consciously realizing it. As a project progresses we often realize that the approach westarted with isn’t quite working and it’s time to do some prudent restructuring and reorganization.
Eclipse supports an automated approach to refactoring that makes sure that changes are propagated properly throughout a project. The nature of refactoring support is somewhat language-dependent. Eclipse offers extensive refactoring support for Java involving operations on classes, interfaces, and methods. For the moment, anyway, C/C++ refactoring is limited to renaming. While this may seem trivial, propagating a name change accurately throughout a large project can be burdensome if done by hand.
We’ll use the record_sort project to illustrate the refactoring support in CDT. Go back to the C/C++ perspective if you’re not already there and open record_sort.c
if it isn’t already. In line 23 select read_file
. Now from the main menu, select Refactor?Rename…. This brings up the dialog box in Figure 4.14. Let’s change the name read_file
to file_read
by changing it in the Rename to: field. The default scope is related projects. This is what we want, because we do in fact have a related project that includes this symbol name. The rename dialog is also accessible from the editor’s context menu.
Figure 4.14: Renaming dialog.
With the Rename to: field modified, the Preview button is now active. Click it to bring up the dialog in Figure 4.15. This shows all of the places in both projects where read_file
can be changed to file_read
and offers the option to selectively change any or all of them. Click OK and read_file
will be magically changed to file_read
throughout both projects.
Figure 4.15: Rename change dialog.
Clearly, this example is trivial, but in a large project involving hundreds, perhaps thousands of files, the ability to automatically rename a symbol throughout the project can be quite powerful.