Source Code Rejuvenation is not Refactoring Peter Pirkelbauer - - PowerPoint PPT Presentation

source code rejuvenation is not refactoring
SMART_READER_LITE
LIVE PREVIEW

Source Code Rejuvenation is not Refactoring Peter Pirkelbauer - - PowerPoint PPT Presentation

Source Code Rejuvenation is not Refactoring Peter Pirkelbauer Damian Dechev Bjarne Stroustrup Texas A&M University SOFSEM 2010 Jan 25 th , 2010 Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring 1 / 18 Programming


slide-1
SLIDE 1

Source Code Rejuvenation is not Refactoring

Peter Pirkelbauer Damian Dechev Bjarne Stroustrup

Texas A&M University

SOFSEM 2010

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 1 / 18

slide-2
SLIDE 2

Programming Language Evolution

Evolving a language in and for the real world: C++ 1991-2006. [Str07]

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 2 / 18

slide-3
SLIDE 3

Overview

1

What is Source Code Rejuvenation?

2

Tool support for Rejuvenation

3

What is Refactoring?

4

Conclusion

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 3 / 18

slide-4
SLIDE 4

What is Source Code Rejuvenation?

Definition

Detection of outdated coding styles and idioms Automatic replacement of old code with modern language features or libraries.

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 4 / 18

slide-5
SLIDE 5

Source Code Rejuvenation

Goals

Preserve or improve a program’s behavior Raise the level of abstraction in source code

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 5 / 18

slide-6
SLIDE 6

Container Initialization in C++0x

C++0x Initializer List

// initializes a vector with 3 elements: 1, 2, 3 vector<int> vec = {1, 2, 3};

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 6 / 18

slide-7
SLIDE 7

Container Initialization in C++

Consecutive push_backs

vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3);

Copying from an array

static const int a[] = {1, 2, 3}; vector<int> vec(a, a+sizeof(a)/sizeof(int));

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 7 / 18

slide-8
SLIDE 8

Container Initialization in C++

Consecutive push_backs

vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3);

Copying from an array

static const int a[] = {1, 2, 3}; vector<int> vec(a, a+sizeof(a)/sizeof(int));

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 7 / 18

slide-9
SLIDE 9

Container Initialization in C++

Consecutive push_backs

vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3);

Copying from an array

static const int a[] = {1, 2, 3}; vector<int> vec(a, a+sizeof(a)/sizeof(int));

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 7 / 18

slide-10
SLIDE 10

Container Initialization in C++

Consecutive push_backs

vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3);

Copying from an array

static const int a[] = {1, 2, 3}; vector<int> vec(a, a+sizeof(a)/sizeof(int));

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 7 / 18

slide-11
SLIDE 11

Source Code Rejuvenation

Goals

Preserve or improve a program’s behavior Raise the level of abstraction in source code

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 8 / 18

slide-12
SLIDE 12

C++ Concepts - A Quick Introduction

Unconstrained code

template <class Iterator> Iterator random_elem(Iterator first, Iterator last) { typename Iterator::difference len = distance(first, last); advance(first, rand()%len); return first; }

C++ concepts specify syntactic and semantic properties

concept RandomElemIter <typename Iterator> { Iterator::Iterator(const Iterator&); // copy constructor typename Iterator::difference; // associated type Iterator::distance distance(Iterator&, Iterator&); void advance(Iterator&, Iterator::distance&); . . . } template<RandomElemIter Iterator> Iterator random_elem(Iterator first, Iterator last);

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 9 / 18

slide-13
SLIDE 13

C++ Concepts - A Quick Introduction

Unconstrained code

template <class Iterator> Iterator random_elem(Iterator first, Iterator last) { typename Iterator::difference len = distance(first, last); advance(first, rand()%len); return first; }

C++ concepts specify syntactic and semantic properties

concept RandomElemIter <typename Iterator> { Iterator::Iterator(const Iterator&); // copy constructor typename Iterator::difference; // associated type Iterator::distance distance(Iterator&, Iterator&); void advance(Iterator&, Iterator::distance&); . . . } template<RandomElemIter Iterator> Iterator random_elem(Iterator first, Iterator last);

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 9 / 18

slide-14
SLIDE 14

C++ Concepts - A Quick Introduction

Unconstrained code

template <class Iterator> Iterator random_elem(Iterator first, Iterator last) { typename Iterator::difference len = distance(first, last); advance(first, rand()%len); return first; }

C++ concepts specify syntactic and semantic properties

concept RandomElemIter <typename Iterator> { Iterator::Iterator(const Iterator&); // copy constructor typename Iterator::difference; // associated type Iterator::distance distance(Iterator&, Iterator&); void advance(Iterator&, Iterator::distance&); . . . } template<RandomElemIter Iterator> Iterator random_elem(Iterator first, Iterator last);

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 9 / 18

slide-15
SLIDE 15

C++ Concepts - A Quick Introduction

Unconstrained code

template <class Iterator> Iterator random_elem(Iterator first, Iterator last) { typename Iterator::difference len = distance(first, last); advance(first, rand()%len); return first; }

C++ concepts specify syntactic and semantic properties

concept RandomElemIter <typename Iterator> { Iterator::Iterator(const Iterator&); // copy constructor typename Iterator::difference; // associated type Iterator::distance distance(Iterator&, Iterator&); void advance(Iterator&, Iterator::distance&); . . . } template<RandomElemIter Iterator> Iterator random_elem(Iterator first, Iterator last);

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 9 / 18

slide-16
SLIDE 16

C++ Concepts - A Quick Introduction

Unconstrained code

template <class Iterator> Iterator random_elem(Iterator first, Iterator last) { typename Iterator::difference len = distance(first, last); advance(first, rand()%len); return first; }

C++ concepts specify syntactic and semantic properties

concept RandomElemIter <typename Iterator> { Iterator::Iterator(const Iterator&); // copy constructor typename Iterator::difference; // associated type Iterator::distance distance(Iterator&, Iterator&); void advance(Iterator&, Iterator::distance&); . . . } template<RandomElemIter Iterator> Iterator random_elem(Iterator first, Iterator last);

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 9 / 18

slide-17
SLIDE 17

Concept Recovery

advance — Input iterator template<class Iter, class Dist> void advance(Iter& iterator, Dist dist, input_iterator_tag) { while (dist−−) ++iterator; } advance — Random access iterator template<class Iter, class Dist> void advance(Iter& iterator, Dist dist, random_access_iterator_tag) { iterator += dist; }

Propagating requirements into random_elem

Which advance exhibits minimal requirements?

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 10 / 18

slide-18
SLIDE 18

Concept Recovery - w/ workaround analysis

advance — Input iterator template<class Iter, class Dist> void advance(Iter& iterator, Dist dist, input_iterator_tag) { while (dist−−) ++iterator; } advance — Random access iterator template<class Iter, class Dist> void advance(Iter& iterator, Dist dist, random_access_iterator_tag) { iterator += dist; }

Propagating requirements into random_elem

Which advance exhibits minimal requirements?

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 11 / 18

slide-19
SLIDE 19

Source Code Rejuvenation - Summary

Key Points

Raises level of abstractions (lowers software entropy) Modern code is more concise (easier to read and maintain) Code rejuvenation can improve program behavior Code rejuvenation is directed

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 12 / 18

slide-20
SLIDE 20

Tool support for Rejuvenation to C++0x - Pivot

The Pivot

Industrial C++ Frontend Intermediate representation (IPR)

◮ frontend independent ◮ preserves high level details ◮ ready for most C++0x features

eXternal representation (XPR)

◮ human read- and writable ◮ 1:1 correspondence to IPR Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 13 / 18

slide-21
SLIDE 21

What is refactoring?

Re ⋄ factoring

Factor multiple reoccurring code into a single function

Broader Definition

An automatic and behavior preserving code transformation that improves source code that was subject to gradual structural deterioration over its life time. [OJ93]

Broader Definition

Improves the design of existing code [FBB+99]

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 14 / 18

slide-22
SLIDE 22

Refactoring Examples

Repeated (user driven) maintenance tasks

Class specification ↔ Class generalization [Opd92] Towards pattern ↔ Away from patterns [Ker04]

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 15 / 18

slide-23
SLIDE 23

Refactoring Examples (cont’d)

Elimination of bad code

Code smells Anti-pattern

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 16 / 18

slide-24
SLIDE 24

Refactoring versus Rejuvenation

Source Code Rejuvenation Refactoring Transformation Source-to-source Source-to-source Behavior preserving Behavior improving Behavior preserving Directed yes no Raises the level of abstraction Drivers Language / library evolution Feature extensions Design changes Indicators Workaround techniques / idioms Code smells Anti-patterns Applications One-time source code migration Recurring maintenance tasks

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 17 / 18

slide-25
SLIDE 25

Thank You!

Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 18 / 18

slide-26
SLIDE 26

Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts. Refactoring: improving the design of existing code. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1999. Joshua Kerievsky. Refactoring to Patterns. Pearson Higher Education, 2004. William F . Opdyke and Ralph E. Johnson. Creating abstract superclasses by refactoring. In CSC ’93: Proceedings of the 1993 ACM conference on Computer science, pages 66–73, New York, NY, USA, 1993. ACM. William F . Opdyke. Refactoring object-oriented frameworks. PhD thesis, University of Illinois at Urbana-Champaign, Champaign, IL, USA, 1992. UMI Order No. GAX93-05645. Bjarne Stroustrup. Evolving a language in and for the real world: C++ 1991-2006. In HOPL III: Proceedings of the third ACM SIGPLAN conference on History of programming languages, pages 4–1, New York, NY, USA, 2007. ACM. Parasol Lab (Texas A&M) Source Code Rejuvenation is not Refactoring Jan 25th, 2010 18 / 18