Refactoring Erlang Programs

Zoltán Horváth, László Lövei, Tamás Kozsik, Anikó Víg, Tamás Nagy
Department of Programming Languages and Compilers Faculty of Informatics ELTE, Budapest, Hungary
Supported by Ericsson Hungary, ELTE CNL and ELTE IKKK*
* GVOP-3.2.2-2004-07-0005/3.0

Contents
● ● ● ● ●

Introduction to Refactoring Introduction to Erlang Refactoring Examples Refactoring Implementation Prototype Tool

2

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Introduction to Refactoring

Restructuring program code without altering its external behaviour Simple refactorings are used every day

Rename functions, variables, types, packages Extract interface or subclass, pull up a method Available for Java, not for functional languages

Widely used in OO programming

Tool support is needed

3

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Erlang Language

Functional programming language and runtime environment developed by Ericsson Designed to build distributed, reliable, soft realtime concurrent systems (telecommunication) No static type system Lightweight processes and message passing Highly dynamic nature

● ● ●

4

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Refactoring Example

Rename a variable

Condition: new name is unused

new(A) -> Id = newId(), add(Id, A). add(Id, A) -> db:store(Id, A). new(Host) -> Id = newId(), add(Id, Host). add(Id, A) -> db:store(Id, A).

Semantical analysis is required to
– –

Determine used variable names Find every occurrence but leave others with the same name intact

5

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Generalization

Conditions:

The expression does not use external variables New variables are not used outside the expression Every call of the function must provide the new parameter

notify(P) -> P ! {notif, 1}. add(Id, A) -> db:store(Id, A), notify(A). notify(P, X) -> P ! {notif, X}. add(Id, A) -> db:store(Id, A), notify(A, 1).

Compensation:

6

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Issues with generalization

An Erlang expression can have a side effect

The new parameter is used as a function which can incorporate the side effect Function names can be passed as a parameter – flow analysis is needed to track that Function names can even be constructed at run time – that is outside the scope of a refactoring tool This influences “easy” refactorings like function renaming
The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Function references can be hard to track
– – –

7

Implementation of Refactoring

Transformations are syntax tree-based Refactoring needs extra information
Module Function: new Parameter: A Binding Call: add Param: Id Param: A Variable: Id

new(A) -> Id = newId(), add(Id, A). add(Id, A) -> db:store(Id, A).
Function: add Parameter: Id Parameter: A Call: db:store Param: Id Param: A

8

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Representation in a Database 

Extra information (like variable scoping) can be represented by links in the syntax tree The edges and links of the tree are relations between the nodes of the tree A relational database can easily represent them
– – –

Every link is bidirectional Tree traversal steps are simulated by joining tables SQL statements can express complex graph operations easily

9

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Database Example

Every node type has its own table Every node gets a globally unique ID
Variable 11 A 12 Id 13 Id 14 A 15 Id 16 A 17 Id 18 A Call 0 0 1 2 0 1 2

new(A) -> Id = newId(), add(Id, A). add(Id, A) -> db:store(Id, A).
32 33 13 14 35 17 18 Function 31 new 32 newId 33 add 34 add 35 db:store Body 31 1 41 31 2 22 34 1 23 Binding 41 1 12 41 2 21

Occurence 11 11 12 12 13 12 14 11 15 15 16 16 17 15 18 16
10

21 22 22 22 23 23 23

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Prototype tool
● ●

Refactorings are implemented in Erlang Emacs is used as a user interface

Connection using Distel Accessed using ODBC Slight modifications: comments, position information

Database backend: MySQL

Parser: standard library

Only changed modules are reparsed between consecutive refactorings
The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

11

Benefits and drawbacks
✔ Tree traversal is needed only for analysis ✔ Subgraphs with fixed depth can be manipulated

by singe joint statements – easy and efficient
✔ Large systems do not cause problems ✗ Long, variable length paths are hard to handle

They can be pre-computed like extra links

✗ In case of smaller programs, database

overhead ruins efficiency
12 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

Further Work
● ● ●

Design and implement more refactorings Speed optimization Non-refactoring transformations and syntaxdriven editing capabilities Semantic model for correctness proof Transformation language: describe refactorings formally

● ●

13

The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006

The End

Thank you for your attention!