Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Programming Talk
This forum is for all programming questions.
The questions do not have to be directly related to Ubuntu and any programming language is allowed.

 
Thread Tools Display Modes
Old May 9th, 2008   #1
KIAaze
Fresh Brewed Ubuntu
 
KIAaze's Avatar
 
Join Date: Feb 2007
Location: lost
Beans: 1,259
Kubuntu
Code refactoring

Hi,

I'm trying to find a way to refactor code, i.e. rename a function everywhere it's used for example.

I heard that Eclipse does this, but I couldn't figure out how to import a C++ project into it. And it takes up too much RAM anyway.

KDevelop seems to offer refactoring according to this release announcement:
Quote:
KDevelop offers documentation generators, code debugging, memory checking, code refactoring, bookmark management, and dozens of tools to make development easy and convenient.
But I can't figure out where this option is.
The Find-Select-Replace option is pretty helpful, but it's not real refactoring since it doesn't search&replace according to the C/C++ syntax.

What code refactoring tools are available on GNU/Linux?
KIAaze is offline   Reply With Quote
Old May 9th, 2008   #2
dwhitney67
Ubuntu addict and loving it
 
dwhitney67's Avatar
 
Join Date: Jun 2007
Location: Maryland, US
Beans: 3,962
Ubuntu 9.10 Karmic Koala
Re: Code refactoring

As a s/w engineer, when I hear or read the term "refactor", the first thing that comes to mind is that a s/w application needs to be modified/simplified into a similar but better, perhaps more nimble, design. Renaming functions or variables has nothing to do with refactoring.

I personally do not use any IDEs, and merely rely on 'vim'. I've been successfully using vim (and its predecessor 'vi') for over 18 years. In the next 10 years it will be interesting to see if Eclipse, KDevelop etc are still around; I'm certain that 'vim' will be.
dwhitney67 is online now   Reply With Quote
Old May 9th, 2008   #3
KIAaze
Fresh Brewed Ubuntu
 
KIAaze's Avatar
 
Join Date: Feb 2007
Location: lost
Beans: 1,259
Kubuntu
Re: Code refactoring

Well, maybe I'll try to finish vimtutor one day.
All I can do for now is insert,search, dw, d$, yank/paste and q,q!,wq. ^^

I haven't found it to be very efficient yet.
KIAaze is offline   Reply With Quote
Old May 9th, 2008   #4
KIAaze
Fresh Brewed Ubuntu
 
KIAaze's Avatar
 
Join Date: Feb 2007
Location: lost
Beans: 1,259
Kubuntu
Re: Code refactoring

I think I found something:
http://www.vim.org/scripts/script.php?script_id=2087

However, I don't know how to use it.
Since you use vim, I suppose you could help me here.

I copied the .vim script into a ~/.vim/plugins directory I created, but I don't understand how to use it.
What's <A-r>r ?
KIAaze is offline   Reply With Quote
Old May 9th, 2008   #5
dwhitney67
Ubuntu addict and loving it
 
dwhitney67's Avatar
 
Join Date: Jun 2007
Location: Maryland, US
Beans: 3,962
Ubuntu 9.10 Karmic Koala
Re: Code refactoring

How many files do you need to "refactor"? If it is only a few, or if they are all of a certain type, then perhaps you can use 'sed' and 'find'?

For example, to change the class name 'Foo' to 'MyClass' in this file:
PHP Code:
class Foo
{
  public:
    
Foo() {}
    ~
Foo() {}
};

int main()
{
  
Foo foo;
  return 
0;

Just run this command for an individual file:
Code:
$ sed -i s/Foo/MyClass/ Foo.cpp
Or this command for all .cpp files at and below the current directory:
Code:
$ find . -name '*.cpp' -exec sed -i s/Foo/MyClass/ {} \;
If you have header files, replace the 'cpp' with an 'h' or 'hpp' as appropriate.
dwhitney67 is online now   Reply With Quote
Old May 10th, 2008   #6
KIAaze
Fresh Brewed Ubuntu
 
KIAaze's Avatar
 
Join Date: Feb 2007
Location: lost
Beans: 1,259
Kubuntu
Re: Code refactoring

Yes, but that's equivalent to the Find-Select-Replace of Kdevelop. (it also accepts regular expressions)

Sometimes, things get complex like in this case if you have:
FooSomething
AFoo

Separate word regex fails here:
Foo.GetX()
Foo->func()
Foo(...)
Somefunc(...,Foo,...)

I guess if I improve my knowledge of regular expressions, I could do without a refactoring tool.
For the moment, I find it easiest to change the function/class definition+prototype/methods and then compile to locate where else it's used.

Most of the time, simple search&replace works well though. But not always.
I thought that if Eclipse has such a function, there must be other programs offering the same. Eclipse is just too bloated and complex for me.

Last edited by KIAaze; May 10th, 2008 at 12:17 AM..
KIAaze is offline   Reply With Quote
Old May 10th, 2008   #7
imdano
Skinny Soy Caramel Ubuntu
 
Join Date: Jun 2007
Beans: 677
Ubuntu 9.04 Jaunty Jackalope
Re: Code refactoring

edit: never mind, misread the last post.
imdano is offline   Reply With Quote
Old May 10th, 2008   #8
achelis
A Carafe of Ubuntu
 
achelis's Avatar
 
Join Date: Mar 2008
Location: Denmark
Beans: 115
Ubuntu 8.10 Intrepid Ibex
Re: Code refactoring

Quote:
Originally Posted by dwhitney67 View Post
Renaming functions or variables has nothing to do with refactoring.
Well "Rename Method" is a refactoring and therefore does has something to do with it

As for tools I'm afraid I only use Eclipse, but found this site, maybe some of the links can help you?

http://www.refactoring.com/tools.html
__________________
Ubuntu Intrepid Ibex (32-bit)
achelis is offline   Reply With Quote
Old July 25th, 2008   #9
wolfie2x
Just Give Me the Beans!
 
Join Date: May 2008
Beans: 59
Ubuntu 9.04 Jaunty Jackalope
Re: Code refactoring

Quote:
Originally Posted by dwhitney67 View Post
As a s/w engineer, when I hear or read the term "refactor", the first thing that comes to mind is that a s/w application needs to be modified/simplified into a similar but better, perhaps more nimble, design. Renaming functions or variables has nothing to do with refactoring.

I personally do not use any IDEs, and merely rely on 'vim'. I've been successfully using vim (and its predecessor 'vi') for over 18 years. In the next 10 years it will be interesting to see if Eclipse, KDevelop etc are still around; I'm certain that 'vim' will be.
typical linux guru answer! We badly need to get something done, and they simply say we don't need it!

Refactoring DOES include renaming functions and cleaning code formatting so that code is readable, meaningful, and manageable.

This is such a standard functionality and I'm really surprised KDevelop doesn't have it.. If anybody knows of any such plugin pls post.
wolfie2x is offline   Reply With Quote
Old July 25th, 2008   #10
dribeas
Dipped in Ubuntu
 
Join Date: Jul 2008
Location: Dublin, Ireland
Beans: 591
Ubuntu 9.10 Karmic Koala
Re: Code refactoring

Quote:
Originally Posted by wolfie2x View Post
This is such a standard functionality and I'm really surprised KDevelop doesn't have it.. If anybody knows of any such plugin pls post.
C++ is a context dependent language. I don't know of any tool that does it (I don't even know of an IDE that can track template method calls properly). The easiest thing that comes to mind is renaming in the .h and .cpp (I won't suggest vi, even if it is my choice ). After that compiler will flag automatically all places where that method was used.

Not really automated, and probably quite boring, but nonetheless...

BTW, I have used and liked Eclipse Java refactoring and it was both useful and easy, but when I tried Eclipse (first version of CDT 4) it was unable to locate calls to templated methods, which is as much as saying that at least for those it will be impossible to refactor.
dribeas is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:42 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry