0% found this document useful (0 votes)
20 views12 pages

C++ Refactoring - Now For Real: Sergey Prigogin Google

Refactoring

Uploaded by

thumperward
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

C++ Refactoring - Now For Real: Sergey Prigogin Google

Refactoring

Uploaded by

thumperward
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ Refactoring Now for Real

Sergey Prigogin
Google
CDT committer, refactoring component lead

Copyright (c) 2012 Google. Made available under the Eclipse Public License v1.0.

History of refactoring in CDT


Getters and Setters
Hide Method
Implement Method
Extract Constant
Rename

2003

2004

2005

Extract Function
2006

2007

2008

2009

2010

2011

2012

Toggle Function
Extract Local Variable

Whats wrong with refactoring in CDT?


Corrupts surrounding code
Generated code is not formatted properly
Often produces semantically invalid code
Does it very slowly
Requires you to save all files

Why was it slow?


Refactoring requires AST
AST requires index read lock
Refactoring steps:
ocheckInitialConditions
ocheckFinalConditions
ocreateChange

How we made it fast


Disposable refactoring context object
Caching of ASTs
Using editor AST when available
oIndex doesnt help with dirty editors

Preventing collateral damage


AST rewrite infrastructure
Creation of textual changes based on AST
delta

Passing parameters by value or reference


class A {!
public:!
void setIntField(int intField) {!
this->intField = intField;!
}!
!
void setStrField(const std::string& strField) {!
this->strField = strField;!
}!
!
private:!
int intField;!
std::string strField;!
};!

Which variables should be returned


int factorial(int k) {!
int i = 1;!
int f = 1;!
while (i <= k)!
f *= i++;!
return f;!
}!

int multiply(int f, int* i) {!


f *= *i++;!
return f;!
}!
!
int factorial(int k) {!
int i = 1;!
int f = 1;!
while (i <= k)!
f = multiply(f, &i);!
return f;!
}!

How is was in CDT 8.0


int factorial(int k) {!
int i = 1;!
int f = 1;!
while (i <= k)!
f *= i++;!
return f;!
}!

int multiply(int f, int i)!


{!
f *= i++;!
}!
!
int factorial(int k) {!
int i = 1;!
int f = 1;!
while (i <= k)!
f = multiply(f, i);!
return f;!
}!

New code style preferences

How can I try it myself?


Download https://hudson.eclipse.org/hudson/
job/cdt-nightly/lastSuccessfulBuild/artifact/
releng/org.eclipse.cdt.repo/target/
org.eclipse.cdt.repo.zip

Whats next?
Focus on quality
Change Function Signature and Inline
refactorings
Rename file when renaming a class
Organize Includes

You might also like