SlideShare a Scribd company logo
1 of 40
Download to read offline
Refactoring and Reļ¬‚ection




  Marcus Denker
  denker@iam.unibe.ch

  UniversitƤt Bern




Ā© Marcus Denker
Roadmap




>    First lecture: Refactoring
     ā€” Squeak as an example



>    Second Lecture: Reļ¬‚ection
     ā€” About current research




Ā© Marcus Denker
Part I: Refactoring




  Marcus Denker
  denker@iam.unibe.ch

  UniversitƤt Bern




Ā© Marcus Denker
Overview




> Refactoring: Basics
> Refactoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?




Ā© Marcus Denker
Roadmap




> Refactoring: Basics
> Refactoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?




Ā© Marcus Denker
What is Refactoring?




>    The process of changing a software system in such a
     way that it does not alter the external behaviour of the
     code, yet improves its internal structure.

     ā€” Fowler, et al., Refactoring, 1999.




Ā© Marcus Denker
Typical Refactorings

Class               Method                    Attribute
add (sub)class to   add method to class       add variable to class
hierarchy
rename class        rename method             rename variable
remove class        remove method             remove variable
                    push method down          push variable down
                    push method up            pull variable up
                    add parameter to method   create accessors
                    move method to            abstract variable
                    component
                    extract code in new
                    method

Ā© Marcus Denker
Why Refactor?

ā€œGrow, donā€™t build softwareā€
     ā€” Fred Brooks
>    The reality:
     ā€”    Extremely difļ¬cult to get the design ā€œrightā€ the ļ¬rst time
     ā€”    Hard to fully understand the problem domain
     ā€”    Hard to understand user requirements, even if the user does!
     ā€”    Hard to know how the system will evolve in ļ¬ve years
     ā€”    Original design is often inadequate
     ā€”    System becomes brittle over time, and more difļ¬cult to change


>    Refactoring helps you to
     ā€” Manipulate code in a safe environment (behavior preserving)
     ā€” Recreate a situation where evolution is possible
     ā€” Understand existing code
Ā© Marcus Denker
Rename Method ā€”Ā manual steps


>    Do it yourself approach:
     ā€” Check that no method with the new name already exists in any
       subclass or superclass.
     ā€” Browse all the implementers (method deļ¬nitions)
     ā€” Browse all the senders (method invocations)
     ā€” Edit and rename all implementers
     ā€” Edit and rename all senders
     ā€” Remove all implementers
     ā€” Test

>    Automated refactoring is better !

Ā© Marcus Denker
Rename Method


> Rename Method (method, new name)
> Preconditions
     ā€” No method with the new name already exists in any subclass or
       superclass.
     ā€” No methods with same signature as method outside the
       inheritance hierarchy of method
>    PostConditions
     ā€” method has new name
     ā€” relevant methods in the inheritance hierarchy have new name
     ā€” invocations of changed method are updated to new name
>    Other Considerations
     ā€” Typed/Dynamically Typed Languages => Scope of the renaming


Ā© Marcus Denker
Refactoring and Metaprograming


>    Automated Refactoring is metaprograming
     ā€” We use a program to edit programs



>    Does not need to use Reļ¬‚ection
     ā€” Pure source-to-source transformation (e.g. Java)



>    Uses reļ¬‚ective facilities in Smalltalk
     ā€” Butā€¦ letŹ¼s discuss that at the end



Ā© Marcus Denker
Roadmap




> Refactoring: Basics
> Refactoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?




Ā© Marcus Denker
Refactoring in Squeak


>    No support in standard IDE
     ā€” Example: Try to rename a method

>    Refactoring Browser
     ā€” First Refactoring Browser (for any language)
     ā€” Now over 10 years old

>    Installation
     ā€” Get Squeak 3.9 (older version for 3.8, too)
     ā€” Install Package AST
     ā€” Install Package Refactoring Engine


Ā© Marcus Denker
Refactoring Browser




>    Browser with menus for e.g
     ā€”    rename
     ā€”    Push up/down
     ā€”    Inlining
     ā€”    Add parameter
     ā€”    Extraction




Ā© Marcus Denker
SmallLint

>   Checks for common mistakes




Ā© Marcus Denker
SmallLint Checks

>    Possible Bugs
     ā€” Variable read before written
     ā€” Deļ¬nes #= but not #hash
     ā€” Modiļ¬es Collection while iterating over it

> Bugs
  ā€” Uses True/False instead of true/false
  ā€” Variable used but not deļ¬ned
> Squeak Bugs
> Unnecessary Code
> Intention Revealing

Ā© Marcus Denker
SmallLint



> Very useful!
> Especially valuable for beginners


>    Has been integrated with SUnit
     ā€” Call SmallLint automatically as a test

>    Tag methods where SmallLint is wrong
     ā€” Uses Squeak 3.9 Method Pragmas




Ā© Marcus Denker
RewriteTool

>    Pattern driven automatic editor




Ā© Marcus Denker
RewriteTool

>    Access to full power of Refactoring Engine

>    Custom refactorings:
     ā€” generic rewrites that the RB does not currently provide
     ā€” bulk transformations: your project needs to change a project-
       speciļ¬c pattern to a new form
     ā€” changing layers: e.g. build a new DB layer, ļ¬nd and change
       17,000 references to old layer
     ā€” migrations: e.g. between Smalltalk dialects


> Powerful but not trivial to use
> Examples: Later

Ā© Marcus Denker
Roadmap




> Refactoring: Basics
> Refactoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?


                  [Main Source] Don Roberts, John Brant, and Ralph Johnson. A
                  Refactoring Tool for Smalltalk. Theory and Practice of Object
                  Systems, vol. 3, issue 4


Ā© Marcus Denker
Implementation Overview



 > Goal: Transformation on the Source
 > Idea: Transform into a higher level tree
   representation




Ā© Marcus Denker
The RB Abstract Syntax Tree



                                                    RBProgramNode
>   AST: Abstract Syntax Tree                         RBDoItNode
     ā€” Encodes the Syntax as a Tree                   RBMethodNode
                                                      RBReturnNode
     ā€” Features:                                      RBSequenceNode
          ā€“       Visitors                            RBValueNode
          ā€“       Backward pointers in ParseNodes   
      RBArrayNode
                                                    
      RBAssignmentNode
          ā€“       Encodes formatting                
      RBBlockNode
          ā€“       Transformation (replace/add/      
      RBCascadeNode
                  delete)                           
      RBLiteralNode
          ā€“       Pattern-directed TreeRewriter     
      RBMessageNode
                                                    
      RBOptimizedNode
          ā€“       PrettyPrinter                     
      RBVariableNode




Ā© Marcus Denker
A Simple AST


RBParser parseExpression: '3+4'   explore it




Ā© Marcus Denker
A Simple Visitor


      RBProgramNodeVisitor new visitNode: tree




                               Does nothing except
                               walk through the tree




Ā© Marcus Denker
More Complete Visitor

RBProgramNodeVisitor subclass: #TestVisitor

 instanceVariableNames: 'literals'

 classVariableNames: ''

 poolDictionaries: ''

 category: 'Compiler-AST-Visitors'

TestVisitor>>acceptLiteralNode: aLiteralNode

 literals add: aLiteralNode value.

TestVisitor>>initialize

 literals := Set new.

TestVisitor>>literals

 ^literals

       tree := RBParser parseExpression: '3 + 4'.
       (TestVisitor new visitNode: tree) literals

                                               a Set(3 4)
 Ā© Marcus Denker
Tree Matcher


>    Implementing all Refactorings with visitors
     ā€” Too much work
     ā€” Too low level

>    Needed: High level speciļ¬cation of transformations

>    Rewrite Engine: Core of Refactoring Engine

>    No only useful for Refactoring!


Ā© Marcus Denker
Tree Matcher




>    Describe transformation by using patterns

>    Syntax: Smalltalk + Meta Variables

>    Example:       | `@Temps |
                    ``@.Statements.
                    ``@Boolean ifTrue: [^false].
                    ^true


Ā© Marcus Denker
Meta Variables

     All Meta Variables begin with `
       Character        Type           Example

       `                recurse into   ``@object
                                       foo
       @                list           | `@Temps |
                                       `@.statements

       .                statement      `.Statement

       #                literal        `#literal



Ā© Marcus Denker
Example 1



 >     Search for:       ``@object not ifTrue: ``@block


     ā€¢ Replace with:      ``@object ifFalse: ``@block




     ā€¢ Explanation:
         ā€“ Eliminate an unnecesary not message



Ā© Marcus Denker
Example 2

                              | `@Temps |
                              ``@.Statements.
 >     Search for:            ``@Boolean ifTrue: [^false].
                              ^true

     ā€¢ Replace with:
                               | `@Temps |
                               ``@.Statements.
                               ^``@Boolean not

     ā€¢ Explanation:
         ā€“ Return the value of the boolean negated instead of using a
           conditional


Ā© Marcus Denker
Implementation: Model and
Environment

>    Model Code transformed but not installed
     ā€” We need to be able to see refactored code without changing the
       system.
     ā€” RBNameSpace

>    Model Classes + Methods
     ā€” Framework duplicates SmalltalkŹ»s structural Reļ¬‚ection
     ā€” RBClass, RBMethod

>    Model Scope to which Refactorings apply
     ā€” RBEnvironment


Ā© Marcus Denker
Back to Code: Pretty Printer




> Visitor: Walks the AST
> Prints out text for each node



>    Problem: How to preserve formatting?
     ā€” AST saves formatting (whitespace, parenthesis)
     ā€” Pretty Printer can use saved formatting information




Ā© Marcus Denker
Contributions needed

>    Improved UI

>    Integrated Parser with Squeak NewCompiler
     ā€” Scanner/Parser done with tool (SmaCC)
     ā€” Easier to change / experiment


>    Integrated RoelTyper
     ā€” Heuristical type inference


>    Better PrettyPrinter
     ā€” Conļ¬gurability
     ā€” Better Layout

Ā© Marcus Denker
Roadmap




> Refactoring: Basics
> Refactoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?




Ā© Marcus Denker
Reļ¬‚ection?


>    We change the system using itself
     ā€” So itŹ»s Reļ¬‚ection, on some level

>    But: LetŹ»s look again at the deļ¬nition
     ā€” Model of itself
     ā€” Causally connected


>    We Build our own abstraction layer
     ā€” AST + Environment

>    This Model is not causally connected!

Ā© Marcus Denker
State Today




Ā© Marcus Denker
Why not this?




Ā© Marcus Denker
We have seen...




> Refactoring: Basics
> Refatoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?




Ā© Marcus Denker
Questions?




> Refactoring: Basics
> Refatoring in Squeak: Browser + Tools
> Refactoring Engine: Implementation
> Discussion: Reļ¬‚ection?


                              Questions?
Ā© Marcus Denker
License

>    http://creativecommons.org/licenses/by-sa/2.5/



                                           Attribution-ShareAlike 2.5
     You are free:
     ā€¢ to copy, distribute, display, and perform the work
     ā€¢ to make derivative works
     ā€¢ to make commercial use of the work

     Under the following conditions:

              Attribution. You must attribute the work in the manner speciļ¬ed by the author or licensor.

     
              Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting
              work only under a license identical to this one.

     ā€¢ For any reuse or distribution, you must make clear to others the license terms of this work.
     ā€¢ Any of these conditions can be waived if you get permission from the copyright holder.

                     Your fair use and other rights are in no way affected by the above.
Ā© Marcus Denker

More Related Content

Viewers also liked

Some thoughts on success
Some thoughts on successSome thoughts on success
Some thoughts on successRajit Desai
Ā 
קן לציפו×Ø
קן לציפו×Øקן לציפו×Ø
קן לציפו×ØHadas Shamir
Ā 
Talk: An Instrument whose Music is Ideas
Talk: An Instrument whose Music is IdeasTalk: An Instrument whose Music is Ideas
Talk: An Instrument whose Music is IdeasMarcus Denker
Ā 
Pharo3 at Fosdem
Pharo3 at FosdemPharo3 at Fosdem
Pharo3 at FosdemMarcus Denker
Ā 
Pharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellPharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellMarcus Denker
Ā 
Lecture: Reflection
Lecture: ReflectionLecture: Reflection
Lecture: ReflectionMarcus Denker
Ā 
מצג×Ŗ לשי×Øה בציבו×Ø
מצג×Ŗ לשי×Øה בציבו×Øמצג×Ŗ לשי×Øה בציבו×Ø
מצג×Ŗ לשי×Øה בציבו×Ømoshfeu
Ā 

Viewers also liked (7)

Some thoughts on success
Some thoughts on successSome thoughts on success
Some thoughts on success
Ā 
קן לציפו×Ø
קן לציפו×Øקן לציפו×Ø
קן לציפו×Ø
Ā 
Talk: An Instrument whose Music is Ideas
Talk: An Instrument whose Music is IdeasTalk: An Instrument whose Music is Ideas
Talk: An Instrument whose Music is Ideas
Ā 
Pharo3 at Fosdem
Pharo3 at FosdemPharo3 at Fosdem
Pharo3 at Fosdem
Ā 
Pharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellPharo: Syntax in a Nutshell
Pharo: Syntax in a Nutshell
Ā 
Lecture: Reflection
Lecture: ReflectionLecture: Reflection
Lecture: Reflection
Ā 
מצג×Ŗ לשי×Øה בציבו×Ø
מצג×Ŗ לשי×Øה בציבו×Øמצג×Ŗ לשי×Øה בציבו×Ø
מצג×Ŗ לשי×Øה בציבו×Ø
Ā 

Similar to Refactoring

Lecture: Reflection
Lecture: ReflectionLecture: Reflection
Lecture: ReflectionMarcus Denker
Ā 
Beyond Text - Methods as Objects
Beyond Text - Methods as ObjectsBeyond Text - Methods as Objects
Beyond Text - Methods as ObjectsMarcus Denker
Ā 
Reflectivity Demo
Reflectivity DemoReflectivity Demo
Reflectivity DemoMarcus Denker
Ā 
Reflection and Context
Reflection and ContextReflection and Context
Reflection and ContextMarcus Denker
Ā 
Unanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral ReflectionUnanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral ReflectionESUG
Ā 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: RefactoringMarcus Denker
Ā 
Unanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral ReflectionUnanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral ReflectionMarcus Denker
Ā 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5The World of Smalltalk
Ā 
Sub-Method Reflection
Sub-Method ReflectionSub-Method Reflection
Sub-Method ReflectionMarcus Denker
Ā 
Demo: Reflectivity
Demo: ReflectivityDemo: Reflectivity
Demo: ReflectivityMarcus Denker
Ā 
The Reflectivity
The ReflectivityThe Reflectivity
The ReflectivityESUG
Ā 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
Ā 
Sub-method Structural and Behavioral Reflection
Sub-method Structural and Behavioral ReflectionSub-method Structural and Behavioral Reflection
Sub-method Structural and Behavioral ReflectionMarcus Denker
Ā 
Webinar: Migrating from RDBMS to MongoDB (June 2015)
Webinar: Migrating from RDBMS to MongoDB (June 2015)Webinar: Migrating from RDBMS to MongoDB (June 2015)
Webinar: Migrating from RDBMS to MongoDB (June 2015)MongoDB
Ā 
Talk: Practical, Pluggable Types
Talk: Practical, Pluggable TypesTalk: Practical, Pluggable Types
Talk: Practical, Pluggable TypesMarcus Denker
Ā 
Presentation about my Research
Presentation about my ResearchPresentation about my Research
Presentation about my ResearchMarcus Denker
Ā 
Behavioral Reflection
Behavioral ReflectionBehavioral Reflection
Behavioral ReflectionMarcus Denker
Ā 

Similar to Refactoring (20)

Reflection
ReflectionReflection
Reflection
Ā 
Lecture: Reflection
Lecture: ReflectionLecture: Reflection
Lecture: Reflection
Ā 
Beyond Text - Methods as Objects
Beyond Text - Methods as ObjectsBeyond Text - Methods as Objects
Beyond Text - Methods as Objects
Ā 
Reflectivity Demo
Reflectivity DemoReflectivity Demo
Reflectivity Demo
Ā 
Reflection and Context
Reflection and ContextReflection and Context
Reflection and Context
Ā 
Unanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral ReflectionUnanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral Reflection
Ā 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: Refactoring
Ā 
08 refactoring
08 refactoring08 refactoring
08 refactoring
Ā 
Unanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral ReflectionUnanticipated Partial Behavioral Reflection
Unanticipated Partial Behavioral Reflection
Ā 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
Ā 
Sub-Method Reflection
Sub-Method ReflectionSub-Method Reflection
Sub-Method Reflection
Ā 
Demo: Reflectivity
Demo: ReflectivityDemo: Reflectivity
Demo: Reflectivity
Ā 
The Reflectivity
The ReflectivityThe Reflectivity
The Reflectivity
Ā 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
Ā 
Sub-method Structural and Behavioral Reflection
Sub-method Structural and Behavioral ReflectionSub-method Structural and Behavioral Reflection
Sub-method Structural and Behavioral Reflection
Ā 
Webinar: Migrating from RDBMS to MongoDB (June 2015)
Webinar: Migrating from RDBMS to MongoDB (June 2015)Webinar: Migrating from RDBMS to MongoDB (June 2015)
Webinar: Migrating from RDBMS to MongoDB (June 2015)
Ā 
Talk: Practical, Pluggable Types
Talk: Practical, Pluggable TypesTalk: Practical, Pluggable Types
Talk: Practical, Pluggable Types
Ā 
Presentation about my Research
Presentation about my ResearchPresentation about my Research
Presentation about my Research
Ā 
Unstuck
UnstuckUnstuck
Unstuck
Ā 
Behavioral Reflection
Behavioral ReflectionBehavioral Reflection
Behavioral Reflection
Ā 

More from Marcus Denker

Soil And Pharo
Soil And PharoSoil And Pharo
Soil And PharoMarcus Denker
Ā 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11Marcus Denker
Ā 
Demo: Improved DoIt
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoItMarcus Denker
Ā 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST AnnotationsMarcus Denker
Ā 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportMarcus Denker
Ā 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Marcus Denker
Ā 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the DebuggerMarcus Denker
Ā 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in PharoMarcus Denker
Ā 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksMarcus Denker
Ā 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for PharoMarcus Denker
Ā 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksMarcus Denker
Ā 
Slot Composition
Slot CompositionSlot Composition
Slot CompositionMarcus Denker
Ā 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksMarcus Denker
Ā 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite GameMarcus Denker
Ā 
Lecture: MetaLinks
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinksMarcus Denker
Ā 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoMarcus Denker
Ā 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in PracticeMarcus Denker
Ā 
Pharo6 - ESUG17
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17Marcus Denker
Ā 

More from Marcus Denker (20)

Soil And Pharo
Soil And PharoSoil And Pharo
Soil And Pharo
Ā 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11
Ā 
Demo: Improved DoIt
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoIt
Ā 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
Ā 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo Support
Ā 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"
Ā 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
Ā 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
Ā 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinks
Ā 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for Pharo
Ā 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
Ā 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
Ā 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinks
Ā 
PHARO IOT
PHARO IOTPHARO IOT
PHARO IOT
Ā 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite Game
Ā 
Lecture: MetaLinks
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinks
Ā 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to Pharo
Ā 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in Practice
Ā 
Pharo6 - ESUG17
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17
Ā 
Pharo6
Pharo6Pharo6
Pharo6
Ā 

Recently uploaded

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
Ā 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
Ā 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
Ā 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
Ā 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
Ā 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
Ā 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
Ā 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
Ā 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationRadu Cotescu
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
Ā 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Ā 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Ā 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Ā 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Ā 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Ā 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
Ā 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organization
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Ā 

Refactoring

  • 1. Refactoring and Reļ¬‚ection Marcus Denker denker@iam.unibe.ch UniversitƤt Bern Ā© Marcus Denker
  • 2. Roadmap > First lecture: Refactoring ā€” Squeak as an example > Second Lecture: Reļ¬‚ection ā€” About current research Ā© Marcus Denker
  • 3. Part I: Refactoring Marcus Denker denker@iam.unibe.ch UniversitƤt Bern Ā© Marcus Denker
  • 4. Overview > Refactoring: Basics > Refactoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? Ā© Marcus Denker
  • 5. Roadmap > Refactoring: Basics > Refactoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? Ā© Marcus Denker
  • 6. What is Refactoring? > The process of changing a software system in such a way that it does not alter the external behaviour of the code, yet improves its internal structure. ā€” Fowler, et al., Refactoring, 1999. Ā© Marcus Denker
  • 7. Typical Refactorings Class Method Attribute add (sub)class to add method to class add variable to class hierarchy rename class rename method rename variable remove class remove method remove variable push method down push variable down push method up pull variable up add parameter to method create accessors move method to abstract variable component extract code in new method Ā© Marcus Denker
  • 8. Why Refactor? ā€œGrow, donā€™t build softwareā€ ā€” Fred Brooks > The reality: ā€” Extremely difļ¬cult to get the design ā€œrightā€ the ļ¬rst time ā€” Hard to fully understand the problem domain ā€” Hard to understand user requirements, even if the user does! ā€” Hard to know how the system will evolve in ļ¬ve years ā€” Original design is often inadequate ā€” System becomes brittle over time, and more difļ¬cult to change > Refactoring helps you to ā€” Manipulate code in a safe environment (behavior preserving) ā€” Recreate a situation where evolution is possible ā€” Understand existing code Ā© Marcus Denker
  • 9. Rename Method ā€”Ā manual steps > Do it yourself approach: ā€” Check that no method with the new name already exists in any subclass or superclass. ā€” Browse all the implementers (method deļ¬nitions) ā€” Browse all the senders (method invocations) ā€” Edit and rename all implementers ā€” Edit and rename all senders ā€” Remove all implementers ā€” Test > Automated refactoring is better ! Ā© Marcus Denker
  • 10. Rename Method > Rename Method (method, new name) > Preconditions ā€” No method with the new name already exists in any subclass or superclass. ā€” No methods with same signature as method outside the inheritance hierarchy of method > PostConditions ā€” method has new name ā€” relevant methods in the inheritance hierarchy have new name ā€” invocations of changed method are updated to new name > Other Considerations ā€” Typed/Dynamically Typed Languages => Scope of the renaming Ā© Marcus Denker
  • 11. Refactoring and Metaprograming > Automated Refactoring is metaprograming ā€” We use a program to edit programs > Does not need to use Reļ¬‚ection ā€” Pure source-to-source transformation (e.g. Java) > Uses reļ¬‚ective facilities in Smalltalk ā€” Butā€¦ letŹ¼s discuss that at the end Ā© Marcus Denker
  • 12. Roadmap > Refactoring: Basics > Refactoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? Ā© Marcus Denker
  • 13. Refactoring in Squeak > No support in standard IDE ā€” Example: Try to rename a method > Refactoring Browser ā€” First Refactoring Browser (for any language) ā€” Now over 10 years old > Installation ā€” Get Squeak 3.9 (older version for 3.8, too) ā€” Install Package AST ā€” Install Package Refactoring Engine Ā© Marcus Denker
  • 14. Refactoring Browser > Browser with menus for e.g ā€” rename ā€” Push up/down ā€” Inlining ā€” Add parameter ā€” Extraction Ā© Marcus Denker
  • 15. SmallLint > Checks for common mistakes Ā© Marcus Denker
  • 16. SmallLint Checks > Possible Bugs ā€” Variable read before written ā€” Deļ¬nes #= but not #hash ā€” Modiļ¬es Collection while iterating over it > Bugs ā€” Uses True/False instead of true/false ā€” Variable used but not deļ¬ned > Squeak Bugs > Unnecessary Code > Intention Revealing Ā© Marcus Denker
  • 17. SmallLint > Very useful! > Especially valuable for beginners > Has been integrated with SUnit ā€” Call SmallLint automatically as a test > Tag methods where SmallLint is wrong ā€” Uses Squeak 3.9 Method Pragmas Ā© Marcus Denker
  • 18. RewriteTool > Pattern driven automatic editor Ā© Marcus Denker
  • 19. RewriteTool > Access to full power of Refactoring Engine > Custom refactorings: ā€” generic rewrites that the RB does not currently provide ā€” bulk transformations: your project needs to change a project- speciļ¬c pattern to a new form ā€” changing layers: e.g. build a new DB layer, ļ¬nd and change 17,000 references to old layer ā€” migrations: e.g. between Smalltalk dialects > Powerful but not trivial to use > Examples: Later Ā© Marcus Denker
  • 20. Roadmap > Refactoring: Basics > Refactoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? [Main Source] Don Roberts, John Brant, and Ralph Johnson. A Refactoring Tool for Smalltalk. Theory and Practice of Object Systems, vol. 3, issue 4 Ā© Marcus Denker
  • 21. Implementation Overview > Goal: Transformation on the Source > Idea: Transform into a higher level tree representation Ā© Marcus Denker
  • 22. The RB Abstract Syntax Tree RBProgramNode > AST: Abstract Syntax Tree RBDoItNode ā€” Encodes the Syntax as a Tree RBMethodNode RBReturnNode ā€” Features: RBSequenceNode ā€“ Visitors RBValueNode ā€“ Backward pointers in ParseNodes RBArrayNode RBAssignmentNode ā€“ Encodes formatting RBBlockNode ā€“ Transformation (replace/add/ RBCascadeNode delete) RBLiteralNode ā€“ Pattern-directed TreeRewriter RBMessageNode RBOptimizedNode ā€“ PrettyPrinter RBVariableNode Ā© Marcus Denker
  • 23. A Simple AST RBParser parseExpression: '3+4' explore it Ā© Marcus Denker
  • 24. A Simple Visitor RBProgramNodeVisitor new visitNode: tree Does nothing except walk through the tree Ā© Marcus Denker
  • 25. More Complete Visitor RBProgramNodeVisitor subclass: #TestVisitor instanceVariableNames: 'literals' classVariableNames: '' poolDictionaries: '' category: 'Compiler-AST-Visitors' TestVisitor>>acceptLiteralNode: aLiteralNode literals add: aLiteralNode value. TestVisitor>>initialize literals := Set new. TestVisitor>>literals ^literals tree := RBParser parseExpression: '3 + 4'. (TestVisitor new visitNode: tree) literals a Set(3 4) Ā© Marcus Denker
  • 26. Tree Matcher > Implementing all Refactorings with visitors ā€” Too much work ā€” Too low level > Needed: High level speciļ¬cation of transformations > Rewrite Engine: Core of Refactoring Engine > No only useful for Refactoring! Ā© Marcus Denker
  • 27. Tree Matcher > Describe transformation by using patterns > Syntax: Smalltalk + Meta Variables > Example: | `@Temps | ``@.Statements. ``@Boolean ifTrue: [^false]. ^true Ā© Marcus Denker
  • 28. Meta Variables All Meta Variables begin with ` Character Type Example ` recurse into ``@object foo @ list | `@Temps | `@.statements . statement `.Statement # literal `#literal Ā© Marcus Denker
  • 29. Example 1 > Search for: ``@object not ifTrue: ``@block ā€¢ Replace with: ``@object ifFalse: ``@block ā€¢ Explanation: ā€“ Eliminate an unnecesary not message Ā© Marcus Denker
  • 30. Example 2 | `@Temps | ``@.Statements. > Search for: ``@Boolean ifTrue: [^false]. ^true ā€¢ Replace with: | `@Temps | ``@.Statements. ^``@Boolean not ā€¢ Explanation: ā€“ Return the value of the boolean negated instead of using a conditional Ā© Marcus Denker
  • 31. Implementation: Model and Environment > Model Code transformed but not installed ā€” We need to be able to see refactored code without changing the system. ā€” RBNameSpace > Model Classes + Methods ā€” Framework duplicates SmalltalkŹ»s structural Reļ¬‚ection ā€” RBClass, RBMethod > Model Scope to which Refactorings apply ā€” RBEnvironment Ā© Marcus Denker
  • 32. Back to Code: Pretty Printer > Visitor: Walks the AST > Prints out text for each node > Problem: How to preserve formatting? ā€” AST saves formatting (whitespace, parenthesis) ā€” Pretty Printer can use saved formatting information Ā© Marcus Denker
  • 33. Contributions needed > Improved UI > Integrated Parser with Squeak NewCompiler ā€” Scanner/Parser done with tool (SmaCC) ā€” Easier to change / experiment > Integrated RoelTyper ā€” Heuristical type inference > Better PrettyPrinter ā€” Conļ¬gurability ā€” Better Layout Ā© Marcus Denker
  • 34. Roadmap > Refactoring: Basics > Refactoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? Ā© Marcus Denker
  • 35. Reļ¬‚ection? > We change the system using itself ā€” So itŹ»s Reļ¬‚ection, on some level > But: LetŹ»s look again at the deļ¬nition ā€” Model of itself ā€” Causally connected > We Build our own abstraction layer ā€” AST + Environment > This Model is not causally connected! Ā© Marcus Denker
  • 37. Why not this? Ā© Marcus Denker
  • 38. We have seen... > Refactoring: Basics > Refatoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? Ā© Marcus Denker
  • 39. Questions? > Refactoring: Basics > Refatoring in Squeak: Browser + Tools > Refactoring Engine: Implementation > Discussion: Reļ¬‚ection? Questions? Ā© Marcus Denker
  • 40. License > http://creativecommons.org/licenses/by-sa/2.5/ Attribution-ShareAlike 2.5 You are free: ā€¢ to copy, distribute, display, and perform the work ā€¢ to make derivative works ā€¢ to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner speciļ¬ed by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. ā€¢ For any reuse or distribution, you must make clear to others the license terms of this work. ā€¢ Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. Ā© Marcus Denker