Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++- und Ruby- und... Refactoring für Eclipse iX-Konferenz Bessere Software! 2006 Prof. Peter Sommerlad HSR - Hochschule für Technik Rapperswil Institut.

Similar presentations


Presentation on theme: "C++- und Ruby- und... Refactoring für Eclipse iX-Konferenz Bessere Software! 2006 Prof. Peter Sommerlad HSR - Hochschule für Technik Rapperswil Institut."— Presentation transcript:

1 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 peter.sommerlad@hsr.ch http://ifs.hsr.ch http://wiki.hsr.ch/PeterSommerlad Bessere Software – Einfach, Schneller

2 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 oshort explanation is optional I assume that you are familiar with object technology and either C++, Ruby or Java

3 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 3 Peter Sommerlad peter.sommerlad@hsr.ch Work Areas oRefactoring Tools (C++,Ruby,...) oDecremental Development (make SW 10% its size!) oModern Software Engineering oPatterns  Pattern-oriented Software Architecture (POSA)  Security Patterns Background oDiplom-Informatiker (Univ. Frankfurt/M) oSiemens Corporate Research - Munich oitopia corporate information technology, Zurich (Partner) oProfessor for Software HSR Rapperswil, Head Institute for Software Credo: People create Software ocommunication ofeedback ocourage Experience through Practice oprogramming is a trade oPatterns encapsulate practical experience Pragmatic Programming otest-driven development oautomated development Simplicity Simplicity ofight complexity

4 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 4 Overview Introduction to Refactoring oElementary Refactorings (optional) Refactoring Plugins otypical approach C++ Refactoring Plugins oWhy is it so hard? oCerp Demo Ruby Refactoring Plugin oWhat is easier? o"Demo" Outlook ofuture Refactoring features and languages oRadRails, C++, Python, PHP, groovy, PL/1, Ada, Cobol...? oFunding?

5 Introduction

6 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 : osmell "bad code" othen refactor it orun all (unit-) tests

7 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 7 Improving Design improving means to eliminate violation of principles of good design, i.e., obad names – uncommuncative code oduplication – DRY principle, OAOO, SPOT olack of cohesion – too large obad dependencies – wrong direction or circular goals ounderstandable program code oachieve conceptual integrity osimplicity – not simplistic otestability (see iX conference 2005) q3 foo ClassA ClassB

8 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 8 Refactoring tools and Eclipse Java JDK provides a good support for refactoring automation oeases development orefactoring possible without too many unit tests Eclipse oprovides language independent framework for refactoring tools oDevelopment Toolkit plugins for many other languages available Most non-Java Toolkit plugins lack adequate Refactoring support

9 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 steps otedious work but omakes programmers think on what to achieve automated Refactoring oless tedious, easier to undo, no/less automatic re-testing required but olulls programmers, can refactor in circles without code improvement, refactor without sense of good design, tools not perfect

10 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 10 Some Important Refactorings (0) Rename ogood names are important owhile we program, we learn better names for fields, variables, classes, methods or functions orenaming 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

11 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 11 Some Important Refactorings (1) Change Bidirectional Association with Unidirectional olower coupling, remove circular dependency ono automation, requires thinking and planning oother simpler refactorings support this (ie. Move) C1 C2 C1 C2

12 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 12 Some Important Refactorings (2) Extract Class oimprove cohesion osmaller classes C1 field1 field2 m1() m2() C1 field1 m1() C2 field2 m2()

13 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 13 Some Important Refactorings (3) Extract Method oreuse code oless duplication C1 +m() m() { before(); a(); b(); after();} C1 +m() -extracted() m() { before(); extracted(); after();} extracted() { a(); b();}

14 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 14 Some Important Refactorings (4) Move Field obetter cohesion, C1 C2 field C1 field C2

15 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 15 Some Important Refactorings (5) Move Method omove code where it belongs C1 C2 m() C1 m() C2

16 Principles of Refactoring tools

17 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 17 Principles workings of Refactoring tools based on abstract syntax trees AST ointernal representation of program code generated by a parser owith name bindings (know what a symbol means) aka symbol table automated Refactoring mechanics (in Eclipse) ocheck preconditions (correct selection) oask for user input (e.g., new name) omodify AST ore-generate source code ocheck postconditions ovisualize changes operform changes

18 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 18 Hard parts of automated Refactoring Comments otypical parsers ignore comments ocomments are often not part of an AST Language redundancy odifferent syntax for same semantics ooften identical AST representation Preserve semantics and compilability omoving elements can change meaning omoving code can break compiles  sequential nature of source code

19 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 19 AST parsing Example simplified a = check ? "ok" : "failed" # some check acheck"ok""failed" lvarstr_literal lvar if_expr conditionthen_partelse_part assignment some check comment ? classic abstract syntax tree comment-extended abstract syntax tree

20 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 20 Creating Refactoring Plugin Infrastructure Tasks: oextend parser and AST to keep comments  JDT loses comments in some cases (or used to)  C++ especially hard with preprocessor ogenerate code from AST without losing comments  pretty-printing in context, e.g., nesting levels  might need to rely on original formatting oautomate tests for this  have to deal with all syntax idiosynchracies ounderstand name bindings for necessary semantic checks for individual refactorings  can require extending AST

21 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 21 Re-generate Code from AST acheck"ok""failed" lvarstr_literal lvar if_expr conditionthen_partelse_part assignment some check comment a = check ? "ok" : "failed" # some check a = if check "ok" else "failed" end # some check

22 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 22 Additional features (re-)generating source code from an extended AST allows additional features Pretty printing oconfigurable consistent layout of source code Code generation ogenerate typical source code pieces  i.e. generate getters/setters/accessors oactually changing the code's semantics  generate constructor using fields  override method

23 Our Projects

24 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?)

25 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 25 C++ Refactoring in Eclipse CDT provides support for C++ development in Eclipse omainly supported by companies providing development tools for embedded platforms oRefactoring not in their focus (yet) oCDT'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 with o#includes, Macros, templates ohand-coded parsers – not grammar generated oredundancy of definitions with declarations

26 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 26 Example of C++ specific problems Preprocessor o#include  classical compiler physically includes ocomments  already removed by preprocessor omacros  can do strange things to syntax in original source Separate compilation units o.c,.cpp files opackage and project concept by convention or separate tools virtual Preprocessin g

27 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 27 Eclipse CDT specific challenges different generations of source code representation and corresponding parsers oCModel - elementary information of position of elements  today deprecated oDOM – the AST of a single translation unit  "completely" parsed code representation oPDOM – "persistent" DOM  not yet complete (but almost now) oRename-Refactoring – own parser and "DOM"  copy-paste reuse of DOM parser neither represents comments or all preprocessing information

28 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 28 Demo CERP Diploma thesis by Guido Zgraggen and Christian Cavegn o8 weeks of intense work fall 2005 uses CDT's legacy CModel to find pieces of code generates source code without relying on complete AST ousable, but problems with some C++ syntax "interesting" C++ Refactoring: synchronize Method oadjust Definition with Declaration or vice versa provides oDeclare Method - generates method declaration from impl. oExtract Baseclass oImplement [from] Baseclass oHide Method – make method private oImplement Method – generated from declaration oSeparate Class – moves a class' definition into separate file oSynchronize Method [signature]

29 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 29

30 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 30 CERP Menu

31 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 31 Extract Baseclass Dialog

32 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 32 Refactoring Preview Wizard

33 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 33 Ruby Refactoring Easier... Ruby has a simpler syntax owith some problems when re-arranging code oparentheses are optional in some places but required for disambiguating dynamic typing eases code generation and refactoring oeverything is an object JRuby as parser needed adjustments RDT needed extension Rails team enthusiastic supporters

34 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 34 Demo RDT Features 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

35 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 35 Extract Method Refactoring

36 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 36 Extract Method Refactoring

37 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 37 Generate Accessors

38 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 38 Generate Accessors

39 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 39 Inline Temporary

40 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 40 Inline Temporary

41 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 41 Merge Class Parts

42 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 42 Merge Class Parts

43 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 43 Push Down

44 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 44 Push Down

45 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 45 Rename Local

46 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 46 Split Temporary Variable when multiple assignments occur

47 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 47 Create Field from Local

48 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 48 Convert Local Variable to Field

49 Conclusion and Outlook

50 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 50 Difficulties of developing Refactoring plugins Unit Testing oworkbench 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 omany 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 - Logistics oestablish collaboration with plugin projects ode-couple from plugin projects frequent releases of Eclipse platform and projects oespecially with volatile projects like CDT, hard to keep up

51 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?)

52 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 oRubyOnRails-specific Refactorings extend C++ CDT Refactoring ostill much work to do, but basic infrastructure available:  comments aware parser and AST  AST rewriting  virtual preprocessing keeps original source Python Refactoring ocurrent semester student project Projects, Plans and Ideas for oQuick-Fixes for FindBugs TM (Java) ogroovy, PL/1(?), ABAP (?), Cobol(?), Ada(?) - Refactoring oBusiness-Process Refactoring

53 iX-Konferenz 2006 - Eclipse Refactoring Plugins (c) Peter Sommerlad 53 Have fun Refactoring! Remember: Refactor to simplify your code oor order specific Refactoring features for your organization Do not hesitate to contact me for further information and feedback: opeter.sommerlad@hsr.ch Online-Resources: http://ifs.hsr.ch http://ifs.hsr.ch/cerp/updatesite - CERP http://r2.ifsoftware.ch – Ruby Refactoring Plugin Sponsoring welcome


Download ppt "C++- und Ruby- und... Refactoring für Eclipse iX-Konferenz Bessere Software! 2006 Prof. Peter Sommerlad HSR - Hochschule für Technik Rapperswil Institut."

Similar presentations


Ads by Google