Refactoring hacked Sebastian Malaca.

Slides:



Advertisements
Similar presentations
Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman.
Advertisements

SOLID Object Oriented Design Craig Berntson
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
Inheritance issues SE-2811 Dr. Mark L. Hornick 1.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 4 Big Refactorings Steve Chenoweth Office Phone: (812) Cell: (937)
Web Security A how to guide on Keeping your Website Safe. By: Robert Black.
EXtreme.NET Dr. Neil Roodyn. eXtreme.NET Who is Dr. Neil? MISSION: To increase the value of your Software Business Working with software for way too long.
Individuals and interactions
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
A Social Story for Students
Unit Testing Tips and Tricks: Database Interaction Louis Thomas.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
The Use of Foreign Language Teaching Techniques in the Computer Science Laboratory to Support Oral Presentation and Group Work.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Best Practices. Contents Bad Practices Good Practices.
Intermediate 2 Software Development Process. Software You should already know that any computer system is made up of hardware and software. The term hardware.
Documentation. Your documentation must fit the needs of your audience. It’s always better to say one thing that is useful, as opposed to many things that.
Course Instructor: K ashif I hsan 1. Chapter # 3 Kashif Ihsan, Lecturer CS, MIHE2.
11 Version Control Systems Mauro Jaskelioff (originally by Gail Hopkins)
Refactoring & Testability. Testing in OOP programming No life in flexible methodologies and for refactoring- infected developers without SOME kind of.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 13 Law of Demeter. Cohesion Cohesion: the “glue” that holds a module together. Don’t do things that do not support a common goal Cohesion: the.
User Interface Principles in API Design Elliotte Rusty Harold
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
OOD teaches a complicated method, best for large systems. Here we teach the ten cent version. OO Design for the Rest of Us 1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Strategy in 15 minutes... derived from the principles.
2014 You are not your Imposter Syndrome Neha 10/8/2014 #GHC
Java Best Practices Java Fundamentals & Object-Oriented Programming MELJUN CORTES, MBA,MPA,BSCS.
Code Simplicity: Software Design In Open Source Projects Max Kanat-Alexander
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
Andrew(amwallis) Classes!
What you always wanted to know about life as a professional software developer but were too afraid to ask.
Making Good Choices at School
Steve Chenoweth Office Phone: (812) Cell: (937)
Test-Driven Development
Concepts of Object Oriented Programming
Preparation for coding
How to be a Good Developer
COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala
Polymorphism, Interfaces, Abstract Classes
What have we already learned about staying safe on the internet?
Object Oriented Analysis and Design
Tattling and Correcting Others
تعارف. تعارف قواعد العمل ا الموبيل المشروبات الاحاديث الجانبية الاسئلة نفكر.
null, true, and false are also reserved.
Design and Programming
Software Testing and Maintenance Modifying Code
Code Smells 1.
Making Good Choices at School
Java for Beginners University Greenwich Computing At School DASCO
How to get rid of Obsession?
WEEK 8 COURSE PROJECT PRESENTATION NAME: ALEXANDER WEISS CLASS: CIS115.
Refactoring Low Level/High Level.
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Test Driven Lasse Koskela Chapter 3: Refactoring in Small Steps
Preparation for coding
How to Develop a Killer Sales Strategy
Dependency Injection Mechanism
Presented by Joe Ellers
Week 1 - Friday COMP 1600.
Presentation transcript:

Refactoring hacked Sebastian Malaca

Who am I? Fanatic of the OOP and the Code’s Quality Blogger Speaker Trainer and consultant Software Developer at Luxoft @SebastianMalaca letstalkaboutjava.blogspot.com www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

What's in the Menu? What is Refactoring? Is it worth the effort? What you should know about Refactoring? How? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

What is Refactoring? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

SMALL CHANGES What is Refactoring? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

NO CHANGES!!! SMALL CHANGES What is Refactoring? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

READABILITY SMALL CHANGES NO CHANGES!!! What is Refactoring? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Is it worth the effort? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Is it worth the effort? It's expensive -> Small changes -> Habit www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Is it worth the effort? It's NOT expensive Only for big projects -> Success -> Long life www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Is it worth the effort? It's NOT expensive NOT only for big projects Must have in big projects -> Come and go -> no author -> readability www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Refactoring vs. Redesign

Refactoring vs. Redesign Readability and Understanding Atomic changes Class as a starting point Ad hoc Improvements and Extensions Complex changes Dependencies Planning www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Stay safe!

Stay safe! No tests, no fun! www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Stay safe! No tests, no fun! Experimentation or Professionalism? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Stay safe! No tests, no fun! Experimentation or Professionalism? Doubts vs. Certainty www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

You have to know when to stop!

You have to know when to stop Refactoring is fun www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

You have to know when to stop Refactoring is fun Refactoring – neverending story www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

You have to know when to stop Refactoring is fun Refactoring – neverending story Boundaries and Values www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

You have to know when to stop Refactoring is fun Refactoring – neverending story Boundaries and Values Perfection! www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

You have to know when to stop Refactoring is fun Refactoring – neverending story Boundaries and Values Perfection! Software with the users vs. Ideal software www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

You have to know when to stop Refactoring is fun Refactoring – neverending story Boundaries and Values Perfection! Software with the users vs. Ideal software www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

It’s worth to know patterns

It’s worth to know patterns www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Gimme the name! www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Let's take a look at the code!

There’s only a small condition…

Less dependencies private TestsRepository testsRepository; private CodeBaseRepository codeBaseRepository; public void apply(ClassCode code, Developer developer) { if (codeBaseRepository.contains(code) && testsRepository.testsExistFor(code)) { startRefactoringOf(code, developer); } } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Less dependencies private TestsRepository testsRepository; private CodeBaseRepository codeBaseRepository; public void apply(ClassCode code, Developer developer) { if (codeBaseRepository.contains(code) && testsRepository.testsExistFor(code)) { startRefactoringOf(code, developer); } } private RefactoringPossiblePredicate refactoringPossiblePredicate; public void apply(ClassCode code) { if (refactoringPossiblePredicate.apply(code)) { startRefactoringOf(code, developer); } } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

The size matters! private TestsRepository testsRepository; public void apply(ClassCode code, Developer developer) { if (testsRepository.testsExistFor(code) && developer.canModify(code) && (code.isComplex() || code.isUnreadable())) { startRefactoringOf(code); } } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

The size matters! When object knows… private TestsRepository testsRepository; public void apply(ClassCode code, Developer developer) { if (testsRepository.testsExistFor(code) && developer.canModify(code) && (code.isComplex() || code.isUnreadable())) { startRefactoringOf(code); } } When object knows… private TestsRepository testsRepository; public void apply(ClassCode code, Developer developer) { if (testsRepository.testsExistFor(code) && developer.canModify(code) && code.canBeImproved()) { startRefactoringOf(code); } } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

The size matters! Single Responsibility Principle private TestsRepository testsRepository; public void apply(ClassCode code, Developer developer) { if (testsRepository.testsExistFor(code) && developer.canModify(code) && (code.isComplex() || code.isUnreadable())) { startRefactoringOf(code); } } private TestsRepository testsRepository; public void apply(ClassCode code, Developer developer) { if (testsRepository.testsExistFor(code) && developer.canModify(code) && code.canBeImproved()) { startRefactoringOf(code); } } private CanStartRefactoringPredicate canStartRefactoringPredicate; public void apply(ClassCode code, Developer developer) { if (canStartRefactoringPredicate.apply(code, developer)) { startRefactoringOf(code); } } Single Responsibility Principle www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Or And?

Or if (code.isComplexOrUnreadable()) { doRefactoring(code); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Or if (code.isComplexOrUnreadable()) { doRefactoring(code); } if (code.canBeImproved()) { doRefactoring(code); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

And if (isRefactoringPossibleAndJustifiedPredicate.apply(code, developer)) { doRefactoring(code); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

And if (isRefactoringPossibleAndJustifiedPredicate.apply(code, developer)) { doRefactoring(code); } if (canStartRefactoringPredicate.apply(code, developer)) { doRefactoring(code); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Method’s name makes a difference

Method’s name makes a difference refactoring.setState(IN_PROGRESS); refactoring.setState(VERIFIED); refactoring.setState(DONE); www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Method’s name makes a difference refactoring.setState(IN_PROGRESS); refactoring.setState(VERIFIED); refactoring.setState(DONE); refactoring.start(); refactoring.accepted(); refactoring.applied(); www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Don’t show too much

Law of Demeter public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getMethodByName(refactoring.getScope()).getCondition(); RefactoringApplier.applyRefactoringFor(condition, refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Law of Demeter public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getMethodByName(refactoring.getScope()).getCondition(); RefactoringApplier.applyRefactoringFor(condition, refactoring); } public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getConditionOf(refactoring.getScope()); RefactoringApplier.applyRefactoringFor(condition, refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Tell, don’t Ask public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getMethodByName(refactoring.getScope()).getCondition(); RefactoringApplier.applyRefactoringFor(condition, refactoring); } public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getConditionOf(refactoring.getScope()); RefactoringApplier.applyRefactoringFor(condition, refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Tell, don’t Ask public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getMethodByName(refactoring.getScope()).getCondition(); RefactoringApplier.applyRefactoringFor(condition, refactoring); } public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getConditionOf(refactoring.getScope()); RefactoringApplier.applyRefactoringFor(condition, refactoring); } public void apply(ClassCode code, Refactoring refactoring) { code.apply(refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Asking many objects… Tell, don’t Ask public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getMethodByName(refactoring.getScope()).getCondition(); RefactoringApplier.applyRefactoringFor(condition, refactoring); } Asking many objects… public void apply(ClassCode code, Refactoring refactoring) { Condition condition = code.getConditionOf(refactoring.getScope()); RefactoringApplier.applyRefactoringFor(condition, refactoring); } public void apply(ClassCode code, Refactoring refactoring) { code.apply(refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Too much is… too much

Too much is… too much public class History { public void store( Author author, RefactoringType type, Scope scope, RefactoringJustification justification, Date today) { // some code } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Too much is… too much public class History { public void store( Author author, RefactoringType type, Scope scope, RefactoringJustification justification, Date today) { // some code } public class History { public void store(CodeDelta delta) { // some code } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Input & Output

Input & Output public void verify(Change change) { Refactoring refactoring = change.getRefactoring(); ClassCode affectedCode = change.getAffectedCode(); CodeUnit codeUnit = codeBaseRepository.findCodeUnitFor(affectedCode); codeUnit.applyRefactoringFor(affectedCode, refactoring); testsExecutor.runFor(codeUnit); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Input & Output public void verify(Change change) { Refactoring refactoring = change.getRefactoring(); ClassCode affectedCode = change.getAffectedCode(); CodeUnit codeUnit = codeBaseRepository.findCodeUnitFor(affectedCode); codeUnit.applyRefactoringFor(affectedCode, refactoring); testsExecutor.runFor(codeUnit); } public void verify(Change change) { ClassCode affectedCode = change.getAffectedCode(); CodeUnit codeUnit = codeBaseRepository.findCodeUnitFor(affectedCode); codeUnit.apply(change); testsExecutor.runFor(codeUnit); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

(not so) easy choice

(not so) easy choice public void triggerRefactoringProcessing(Refactoring refactoring){ switch (refactoring.state()) { case DEFINED: moveToInProgressState(refactoring); break; case IN_PROGRESS: createChangeFor(refactoring); break; case TO_VERIFY: verifyChangeFor(refactoring); break; case VERIFIED: applyChangeFrom(refactoring); break; } } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

(not so) easy choice public void triggerRefactoringProcessing(Refactoring refactoring){ switch (refactoring.state()) { case DEFINED: moveToInProgressState(refactoring); break; case IN_PROGRESS: createChangeFor(refactoring); break; case TO_VERIFY: verifyChangeFor(refactoring); break; case VERIFIED: applyChangeFrom(refactoring); break; } } private Map<RefactoringState, RefactoringTriggerStrategy> strategies; public RefactoringTrigger(Map<RefactoringState, RefactoringTriggerStrategy> strategies) { this.strategies = strategies; } public void triggerRefactoringProcessing(Refactoring refactoring){ strategies.get(refactoring.state()).triggerFor(refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

State Pattern maybe? (not so) easy choice public void triggerRefactoringProcessing(Refactoring refactoring){ switch (refactoring.state()) { case DEFINED: moveToInProgressState(refactoring); break; case IN_PROGRESS: createChangeFor(refactoring); break; case TO_VERIFY: verifyChangeFor(refactoring); break; case VERIFIED: applyChangeFrom(refactoring); break; } } State Pattern maybe? private Map<RefactoringState, RefactoringTriggerStrategy> strategies; public RefactoringTrigger(Map<RefactoringState, RefactoringTriggerStrategy> strategies) { this.strategies = strategies; } public void triggerRefactoringProcessing(Refactoring refactoring){ strategies.get(refactoring.state()).triggerFor(refactoring); } www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

When you want to know too much…

When you want to know too much… public void triggerProcessingChangeOf(Code code, Change change) { if (change instanceof Refactoring) { processRefactoring(code, (Refactoring) change); } else if (change instanceof Improvement) { processImprovement(code, (Improvement) change); } else if (change instanceof Growth) { processGrowth(code, (Growth) change); } else { throw new UnsuportedChangeException(); } }

When you want to know too much… public void triggerProcessingChangeOf(Code code, Change change) { if (change instanceof Refactoring) { processRefactoring(code, (Refactoring) change); } else if (change instanceof Improvement) { processImprovement(code, (Improvement) change); } else if (change instanceof Growth) { processGrowth(code, (Growth) change); } else { throw new UnsuportedChangeException(); } }

When you want to know too much… public void triggerProcessingChangeOf(Code code, Change change) { if (change instanceof Refactoring) { processRefactoring(code, (Refactoring) change); } else if (change instanceof Improvement) { processImprovement(code, (Improvement) change); } else if (change instanceof Growth) { processGrowth(code, (Growth) change); } else { throw new UnsuportedChangeException(); } }

When you want to know too much… public void triggerProcessingChangeOf(Code code, Change change) { if (change instanceof Refactoring) { processRefactoring(code, (Refactoring) change); } else if (change instanceof Improvement) { processImprovement(code, (Improvement) change); } else if (change instanceof Growth) { processGrowth(code, (Growth) change); } else { throw new UnsuportedChangeException(); } } public void triggerProcessingChangeOf(Code code, Change change) { change.accept(new ChangeProcessor(code)); }

When you want to know too much… public void triggerProcessingChangeOf(Code code, Change change) { change.accept(new ChangeProcessor(code)); } public interface Change { void accept(Visitator visitator); } public interface Visitator { void accept(Refactoring refactoring); void accept(Improvement improvement); void accept(Growth growth); }

When you want to know too much… public void triggerProcessingChangeOf(Code code, Change change) { change.accept(new ChangeProcessor(code)); } public interface Change { void accept(Visitator visitator); } public interface Visitator { void accept(Refactoring refactoring); void accept(Improvement improvement); void accept(Growth growth); } public class ChangeProcessor implements Visitator { @Override public void accept(Refactoring refactoring) { // some code } @Override public void accept(Improvement improvement) { // some code } @Override public void accept(Growth growth) { // some code }

Far too much…

Far too much… public void process(Refactoring refactoring) { applyToCode(refactoring); createPullRequestWith(refactoring); doCodeReviewOf(refactoring); mergeToTheMaster(refactoring); }

Far too much… public void process(Refactoring refactoring) { applyToCode(refactoring); createPullRequestWith(refactoring); doCodeReviewOf(refactoring); mergeToTheMaster(refactoring); }

Far too much… public void process(Refactoring refactoring) { applyToCode(refactoring); createPullRequestWith(refactoring); doCodeReviewOf(refactoring); mergeToTheMaster(refactoring); } public interface RefactoringProcessor { void process(Refactoring refactoring); }

Far too much… public void process(Refactoring refactoring) { applyToCode(refactoring); createPullRequestWith(refactoring); doCodeReviewOf(refactoring); mergeToTheMaster(refactoring); } public interface RefactoringProcessor { void process(Refactoring refactoring); } public class CodeRefactoringProcessor implements RefactoringProcessor { @Override public void process(Refactoring refactoring) { applyToCode(refactoring); }

Far too much… public void process(Refactoring refactoring) { applyToCode(refactoring); createPullRequestWith(refactoring); doCodeReviewOf(refactoring); mergeToTheMaster(refactoring); } public interface RefactoringProcessor { void process(Refactoring refactoring); } public class CodeRefactoringProcessor implements RefactoringProcessor { @Override public void process(Refactoring refactoring) { applyToCode(refactoring); } public class PullRequestProcessor implements RefactoringProcessor { private final RefactoringProcessor processor; @Override public void process(Refactoring refactoring) { processor.process(refactoring); createPullRequestWith(refactoring); }

TL; DR

TL; DR Introduce private methods Extract parts of the code organized around dependencies Conditions Switches Single Responsibility Principle www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

What was served?

What was served? What is Refactoring? Small changes No changes!!! Readability www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

What was served? What is Refactoring? Is it worth the effort? It's not expensive It's for any kind of project www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

What was served? What is Refactoring? Is it worth the effort? What you should know about Refactoring? Refactoring vs. Redesign Stay safe! You have to know when to stop! www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

What was served? What is Refactoring? Is it worth the effort? What you should know about Refactoring? How? www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

Always leave the campground cleaner than you found it. Boy Scouts Rule www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com

www.luxoft.com, @SebastianMalaca, letstalkaboutjava.blogspot.com