Upload
mliss
View
41
Download
2
Embed Size (px)
DESCRIPTION
Bessere Software – Einfach, Schneller. C++- und Ruby- und ... Refactoring für Eclipse. iX-Konferenz Bessere Software! 2006 Prof. Peter Sommerlad HSR - Hochschule für Technik Rapperswil Institut for Software Oberseestraße 10, CH-8640 Rapperswil [email protected] http://ifs.hsr.ch - PowerPoint PPT Presentation
Citation preview
C++- und Ruby- und ...Refactoring für Eclipse
iX-Konferenz Bessere Software! 2006
Prof. Peter SommerladHSR - Hochschule für Technik RapperswilInstitut for SoftwareOberseestraße 10, CH-8640 [email protected]://ifs.hsr.chhttp://wiki.hsr.ch/PeterSommerlad
Bessere Software – Einfach, Schneller
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 2
Assumptions/Questions
•Talk in English or deutsch?
•I assume that you ask questions when you have them. Please interrupt me.
•I assume that you are not yet very familiar with the concept of Refactoring
o short explanation is optional
•I assume that you are familiar with object technology and either C++, Ruby or Java
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 3
Peter [email protected]
• Work Areaso Refactoring Tools (C++,Ruby,...)o Decremental Development
(make SW 10% its size!)o Modern Software Engineeringo Patterns
Pattern-oriented Software Architecture (POSA)
Security Patterns
• Backgroundo Diplom-Informatiker (Univ.
Frankfurt/M)o Siemens Corporate Research -
Municho itopia corporate information
technology, Zurich (Partner)o Professor for Software
HSR Rapperswil, Head Institute for Software
Credo:
• People create Softwareo communicationo feedbacko courage
• Experience through Practiceo programming is a tradeo Patterns encapsulate practical
experience
• Pragmatic Programmingo test-driven developmento automated development
• SimplicitySimplicityo fight complexity
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 4
Overview
• Introduction to Refactoringo Elementary Refactorings (optional)
•Refactoring Pluginso typical approach
•C++ Refactoring Pluginso Why is it so hard?o Cerp Demo
•Ruby Refactoring Plugino What is easier?o "Demo"
•Outlooko future Refactoring features and languageso RadRails, C++, Python, PHP, groovy, PL/1, Ada, Cobol...?o Funding?
Introduction
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 6
Refactoring
"Improving the Design of Existing Code" -- Martin Fowler
•after we learned to solve the programming problem, solve it in a better way.
•process:o smell "bad code"o then refactor ito run all (unit-) tests
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 7
Improving Design
•improving means to eliminate violation of principles of good design, i.e.,o bad names – uncommuncative codeo duplication – DRY principle, OAOO, SPOTo lack of cohesion – too largeo bad dependencies – wrong direction or circular
•goalso understandable program codeo achieve conceptual integrityo simplicity – not simplistico testability (see iX conference 2005)
q3 foo
ClassA
ClassB
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 8
Refactoring tools and Eclipse
•Java JDK provides a good support for refactoring automationo eases developmento refactoring possible without too many unit tests
•Eclipseo provides language independent framework for
refactoring toolso Development Toolkit plugins for many other
languages available
Most non-Java Toolkit plugins lack adequate Refactoring support
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 9
Benefits and Drawbacks of automated Refactoring
•Fowler's book describes Refactoring as a manual process of (very) small stepso tedious workbut o makes programmers think on what to achieve
•automated Refactoringo less tedious, easier to undo, no/less automatic re-
testing requiredbuto lulls programmers, can refactor in circles without
code improvement, refactor without sense of good design, tools not perfect
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 10
Some Important Refactorings (0)
•Renameo good names are importanto while we program, we learn better names for
fields, variables, classes, methods or functionso renaming benefits heavily from automatic
support find all places where this named program element is
used by tracking references from a definition no need to blindly change the name string everywhere no need for manual checking if name means something
else in another place better than sed –e 's/oldname/newname/g'
q3 circleDiameter
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 11
Some Important Refactorings (1)
•Change Bidirectional Association with Unidirectionalo lower coupling, remove circular dependencyo no automation, requires thinking and planning
o other simpler refactorings support this (ie. Move)
C1
C2
C1
C2
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 12
Some Important Refactorings (2)
•Extract Classo improve cohesiono smaller classes
C1
field1field2
m1()m2()
C1
field1
m1()
C2
field2
m2()
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 13
Some Important Refactorings (3)
•Extract Methodo reuse codeo less duplication
C1
+m()
m() { before(); a(); b(); after();}
C1
+m()-extracted()
m() { before(); extracted(); after();}
extracted() { a(); b();}
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 14
Some Important Refactorings (4)
•Move Fieldo better cohesion,
C1
C2
field
C1
field
C2
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 15
Some Important Refactorings (5)
•Move Methodo move code where it belongs
C1
C2
m()
C1
m()
C2
Principles of Refactoring tools
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 17
Principles workings of Refactoring tools
•based on abstract syntax trees ASTo internal representation of program code
generated by a parsero with name bindings (know what a symbol means)
aka symbol table
•automated Refactoring mechanics (in Eclipse)o check preconditions (correct selection)o ask for user input (e.g., new name)o modify ASTo re-generate source codeo check postconditionso visualize changeso perform changes
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 18
Hard parts of automated Refactoring
•Commentso typical parsers ignore commentso comments are often not part of an AST
•Language redundancyo different syntax for same semanticso often identical AST representation
•Preserve semantics and compilabilityo moving elements can change meaningo moving code can break compiles
sequential nature of source code
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 19
AST parsing Examplesimplified
a = check ? "ok" : "failed" # some check
a check "ok" "failed"
lvar str_literal str_literallvar
if_expr
condition then_part else_part
assignment
some check
comment
?
classic abstract syntax tree
comment-extended abstract syntax tree
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 20
Creating Refactoring Plugin Infrastructure
•Tasks:o extend parser and AST to keep comments
JDT loses comments in some cases (or used to) C++ especially hard with preprocessor
o generate code from AST without losing comments pretty-printing in context, e.g., nesting levels might need to rely on original formatting
o automate tests for this have to deal with all syntax idiosynchracies
o understand name bindings for necessary semantic checks for individual refactorings can require extending AST
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 21
Re-generate Code from AST
a check "ok" "failed"
lvar str_literal str_literallvar
if_expr
condition then_part else_part
assignment
some check
comment
a = check ? "ok" : "failed" # some checka = if check "ok" else "failed" end # some check
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 22
Additional features
•(re-)generating source code from an extended AST allows additional features
•Pretty printingo configurable consistent layout of source code
•Code generationo generate typical source code pieces
i.e. generate getters/setters/accessors
o actually changing the code's semantics generate constructor using fields override method
Our Projects
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 24
Our Refactoring projects
•CERP – simple C++ Refactoring
•Ruby Refactoring
•CDT – C++ Refactoring
•Python
•Rails
•PHP
•groovy
•your language here... (PL/1, ABAP, Cobol?)
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 25
C++ Refactoring in Eclipse
•CDT provides support for C++ development in Eclipseo mainly supported by companies providing
development tools for embedded platformso Refactoring not in their focus (yet)o CDT's evolution provides some interesting and
duplicate design elements and features i.e. several parsers, several source representations
•C/C++ is especially hard to deal witho #includes, Macros, templateso hand-coded parsers – not grammar generatedo redundancy of definitions with declarations
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 26
Example of C++ specific problems
•Preprocessoro #include
classical compiler physically includes
o comments already removed by preprocessor
o macros can do strange things to syntax in original source
•Separate compilation unitso .c, .cpp fileso package and project concept by convention or
separate tools
virtual Preprocessin
g
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 27
Eclipse CDT specific challenges
•different generations of source code representation and corresponding parserso CModel - elementary information of position
of elements today deprecated
o DOM – the AST of a single translation unit "completely" parsed code representation
o PDOM – "persistent" DOM not yet complete (but almost now)
o Rename-Refactoring – own parser and "DOM" copy-paste reuse of DOM parser
•neither represents comments or all preprocessing information
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 28
Demo CERP
•Diploma thesis by Guido Zgraggen and Christian Cavegno 8 weeks of intense work fall 2005
•uses CDT's legacy CModel to find pieces of code
•generates source code without relying on complete ASTo usable, but problems with some C++ syntax
• "interesting" C++ Refactoring: synchronize Methodo adjust Definition with Declaration or vice versa
•provideso Declare Method - generates method declaration from impl.o Extract Baseclass o Implement [from] Baseclasso Hide Method – make method privateo Implement Method – generated from declarationo Separate Class – moves a class' definition into separate fileo Synchronize Method [signature]
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 30
CERP Menu
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 31
Extract Baseclass Dialog
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 32
Refactoring Preview Wizard
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 33
Ruby RefactoringEasier...
•Ruby has a simpler syntaxo with some problems when re-arranging codeo parentheses are optional in some places but
required for disambiguating
•dynamic typing eases code generation and refactoringo everything is an object
•JRuby as parser needed adjustments
•RDT needed extension
•Rails team enthusiastic supporters
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 34
Demo RDTFeatures
•Generate Accessors
•Generate Constructor using Fields
•Override Method
•Rename Local Variable
•Push Down Method
•Extract Method
•Extract Class
•Rename Class, Method, Field
• Inline Class, Method, Field
•Split Temporary Variable
•Move Method, Field
•Convert Local Variable to Field
•Merge Class' Parts from different Files
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 35
Extract Method Refactoring
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 37
Generate Accessors
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 39
Inline Temporary
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 41
Merge Class Parts
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 43
Push Down
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 45
Rename Local
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 46
Split Temporary Variablewhen multiple assignments occur
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 47
Create Field from Local
Conclusion and Outlook
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 50
Difficulties of developing Refactoring plugins
•Unit Testingo workbench expensive to start
decouple your code and tests from workbench as good as possible
run tests as often as possible and as quick as possible
o many tests required write configurable tests files with test data, not code with test cases quick to add and extend tests
• "Fixing" and extending existing Plugins - Logisticso establish collaboration with plugin projectso de-couple from plugin projects
• frequent releases of Eclipse platform and projectso especially with volatile projects like CDT, hard to keep up
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 51
Our Refactoring projects
•CERP – simple C++ Refactoring
•Ruby Refactoring
•CDT – C++ Refactoring
•Python
•Rails
•PHP
•groovy
•your language here... (PL/1, ABAP, Cobol?)
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 52
Future Refactoring and Plugin projects @ IFS
• incorporate Ruby Refactoring into official RDT and RadRails projects
o RubyOnRails-specific Refactorings
•extend C++ CDT Refactoringo still much work to do, but basic infrastructure available:
comments aware parser and AST AST rewriting virtual preprocessing keeps original source
•Python Refactoringo current semester student project
•Projects, Plans and Ideas foro Quick-Fixes for FindBugsTM (Java)o groovy, PL/1(?), ABAP (?), Cobol(?), Ada(?) - Refactoringo Business-Process Refactoring
iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 53
Have fun Refactoring!
•Remember: Refactor to simplify your code
o or order specific Refactoring features for your organization
•Do not hesitate to contact me for further information and feedback:
Online-Resources:
•http://ifs.hsr.ch
•http://ifs.hsr.ch/cerp/updatesite - CERP
•http://r2.ifsoftware.ch – Ruby Refactoring Plugin
Sponsoring welcome