21
Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ.

Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

Embed Size (px)

Citation preview

Page 1: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

RefactoringErlang Programs

Huiqing LiSimon Thompson University of

Kent

Zoltán Horváth

Eötvös Loránd Univ.

Page 2: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Overview

Example refactoringsGeneral observationsChallenges of ErlangOur two implementationsNext steps … and conclusion

Page 3: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Soft-ware

There’s no single correct design …

… different options for different situations.

Maintain flexibility as the system evolves.

Page 4: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Generalisation

-module (test).-export([f/1]). add_one ([H|T]) -> [H+1 | add_one(T)];

add_one ([]) -> [].

f(X) -> add_one(X).

-module (test).-export([f/1]). add_one (N, [H|T]) -> [H+N | add_one(N,T)];

add_one (N,[]) -> [].

f(X) -> add_one(1, X).

-module (test).-export([f/1]). add_int (N, [H|T]) -> [H+N | add_int(N,T)];

add_int (N,[]) -> [].

f(X) -> add_int(1, X).

Generalisation and renaming

Page 5: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Generalisation

-export([printList/1]).

printList([H|T]) -> io:format("~p\n",[H]), printList(T);printList([]) -> true.

printList([1,2,3])

-export([printList/2]).

printList(F,[H|T]) -> F(H), printList(F, T);printList(F,[]) -> true.

printList( fun(H) -> io:format("~p\n", [H]) end, [1,2,3]).

Page 6: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Generalisation

-export([printList/1]).

printList([H|T]) -> io:format("~p\n",[H]), printList(T);printList([]) -> true.

-export([printList/1]).

printList(F,[H|T]) -> F(H), printList(F, T);printList(F,[]) -> true.

printList(L) -> printList( fun(H) -> io:format("~p\n", [H]) end, L).

Page 7: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Asynchronous to synchronous

pid! {self(),msg}

{Parent,msg} -> body

pid! {self(),msg}, receive {pid, ok}-> ok

{Parent,msg} -> Parent! {self(),ok}, body

Page 8: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Refactoring = Transformation + Condition

Transformation

Ensure change at all those points needed.

Ensure change at only those points needed.

Condition

Is the refactoring applicable?

Will it preserve the semantics of the module? the program?

Page 9: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Transformations

full stop one

Page 10: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Condition > TransformationRenaming an identifier"The existing binding structure should not be affected. No binding for the new name may intervene between the binding of the old name and any of its uses, since the renamed identifier would be captured by the renaming. Conversely, the binding to be renamed must not intervene between bindings and uses of the new name."

Page 11: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Tool support

Bureaucratic and diffuse.

Tedious and error prone.

Semantics: scopes, types, modules, …

Undo/redo

Enhanced creativity

Page 12: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Static vs dynamic

Aim to check conditions statically.

Static analysis tools possible … but some aspects intractable: e.g. dynamically manufactured atoms.

Conservative vs liberal.

Compensation?

Page 13: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Compensate or fail?

-export([oldFun/1, newFun/1]).

oldFun(L) -> newFun(L).

newFun(L) -> … … .

-export([newFun/1]).

newFun(L) -> … … .

or ?

Page 14: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Erlang refactoring: challenges Multiple binding occurrences of variables.Indirect function call or function spawn: apply (lists, rev, [[a,b,c]]) Multiple arities … multiple functions: rev/1

ConcurrencyRefactoring within a design library: OTP.Side-effects.

Page 15: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Haskell vs Erlang refactorings

Haskell Refactorings Erlang Refactorings

Type/type class-related refactorings.Monad-related refactorings. . . . . .

Introduce/remove concurrency,Asynchronous/synchronous

communication,Refactoring to design patterns, . . . . . .

Renaming,Removing unused definitions/parameters,Swapping arguments,Introduce new definitions, . . . . . .

Page 16: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Architectures

Page 17: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Wrangler in Emacs

Page 18: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Wrangler in Emacs

Page 19: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

AST vs database

Lightweight.

Better integration with interactive tools (e.g. emacs).

Undo/redo external?

Ease of implementing conditions?

Higher entry cost.

Better for a series of refactorings on a large project.

Transaction support.

Ease of implementing transformations?

Page 20: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Next steps

Refactor concurrency

• styles• introduction

Refactor Erlang/OTP

• within OTP• towards OTP

Release systems by the end of January.

Performance comparisons on real systems.

Page 21: Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ

EUC 2006

Ackonwledgements

EPSRC

ELTE IKKK, CNL

Ericsson Hungary

Bolyai Res Fellowship

syntax-tools

distel