Refactoring Erlang Programs: Zoltán Horváth, László Lövei, Tamás Kozsik, Anikó Víg, Tamás Nagy
Refactoring Erlang Programs: Zoltán Horváth, László Lövei, Tamás Kozsik, Anikó Víg, Tamás Nagy
* 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
● Widely used in OO programming
– Extract interface or subclass, pull up a method
● Tool support is needed
– Available for Java, not for functional languages
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 real-
time 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 new(A) ->
– Condition: new name Id = newId(),
is unused add(Id, A).
● Semantical analysis add(Id, A) ->
is required to db:store(Id, A).
– Determine used
variable names new(Host) ->
Id = newId(),
– Find every occurrence
add(Id, Host).
but leave others with
the same name intact add(Id, A) ->
db:store(Id, A).
5 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Generalization
● Conditions: notify(P) ->
– The expression does P ! {notif, 1}.
not use external add(Id, A) ->
variables db:store(Id, A),
– New variables are not notify(A).
used outside the
expression
notify(P, X) ->
● Compensation: P ! {notif, X}.
– Every call of the add(Id, A) ->
function must provide db:store(Id, A),
the new parameter notify(A, 1).
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 references can be hard to track
– 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
7 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Implementation of Refactoring
● Transformations are new(A) ->
syntax tree-based Id = newId(),
add(Id, A).
● Refactoring needs
extra information add(Id, A) ->
db:store(Id, A).
Module
Function: new Function: add
Parameter: A Parameter: Id
Parameter: A
Binding Variable: Id
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 new(A) ->
its own table Id = newId(),
add(Id, A).
● Every node gets a
globally unique ID add(Id, A) ->
db:store(Id, A).
10 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
● Database backend: MySQL
– Accessed using ODBC
● Parser: standard library
– Slight modifications: comments, position information
● Only changed modules are reparsed between
consecutive refactorings
11 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
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
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 syntax-
driven 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