0% found this document useful (0 votes)
45 views

Re Factoring

Refactoring is a systematic process of improving code without creating new functionality that can transform a mess into clean code and simple design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Re Factoring

Refactoring is a systematic process of improving code without creating new functionality that can transform a mess into clean code and simple design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UML Models to Code

UML to Code

• Model to code
• Model space
• Source code space

• Four different types of transformations


• Model transformation
• Forward engineering
• Reverse engineering
• Refactoring.
4 Different Types of Transformations Program
(in Java)
Yet Another
System Model
Another
System Model Forward
engineering

Refactoring
Model
transformation

Reverse Another
engineering Program
System Model
(in UML)
Model space Source code space
Model Transformation Example
Class model before transformation:

LeagueOwner Advertiser Player


+email:Address +email:Address +email:Address

Class model after


transformation:
User
+email:Address

LeagueOwner Advertiser Player


Refactoring Example: Pull Up Field
public class User {
private String email;
}

public class Player { public class Player extends User {


private String email; //...
//... }
}
public class LeagueOwner { public class LeagueOwner extends
User {
private String eMail;
//...
//...
}
}
public class Advertiser { public class Advertiser extends
User {
private String
email_address; //...
//... }
}
Forward Engineering Example
Object design model before transformation:

User LeagueOwner
-email:String -maxNumLeagues:int
+getEmail():String +getMaxNumLeagues():int
+setEmail(e:String) +setNaxNumLeagues(n:int)
+notify(msg:String)

Source code after transformation:


public class User { public class LeagueOwner extends User {
private String email; private int maxNumLeagues;
public String getEmail() { public int getMaxNumLeagues() {
return email; return maxNumLeagues;
} }
public void setEmail(String value){ public void setMaxNumLeagues
email = value;
(int value) {
}
maxNumLeagues = value;
public void notify(String msg) {
}
// ....
} }
}
Code Refactoring
Refactoring

• Refactoring is:
• restructuring (rearranging) code...
• ...in a series of small, semantics-preserving
transformations (i.e. the code keeps working)...
• ...in order to make the code easier to maintain and
modify
• Refactoring is not just any old restructuring
• You need to keep the code working
• You need small steps that preserve semantics
• You need to have unit tests to prove the code works
• There are numerous well-known refactoring
techniques
• You should be at least somewhat familiar with these
before inventing your own
8
When to refactor

• You should refactor:


• Any time that you see a better way to do things
• “Better” means making the code easier to understand
and to modify in the future
• You can do so without breaking the code
• Unit tests are essential for this
• You should not refactor:
• Stable code (code that won’t ever need to change)
• Someone else’s code
• Unless you’ve inherited it (and now it’s yours)

9
Example 1: switch statements

• switch statements are very rare in properly


designed object-oriented code
• Therefore, a switch statement is a simple and easily
detected “bad smell”
• Of course, not all uses of switch are bad
• A switch statement should not be used to distinguish
between various kinds of object
• There are several well-defined refactorings for this
case
• The simplest is the creation of subclasses

10
Example 1, continued

• class Animal {
final int MAMMAL = 0, BIRD = 1, REPTILE = 2;
int myKind; // set in constructor
...
String getSkin() {
switch (myKind) {
case MAMMAL: return "hair";
case BIRD: return "feathers";
case REPTILE: return "scales";
default: return “skin";
}
}
}

11
Example 1, improved

• class Animal {
String getSkin() { return “skin"; }
}
class Mammal extends Animal {
String getSkin() { return "hair"; }
}
class Bird extends Animal {
String getSkin() { return "feathers"; }
}
class Reptile extends Animal {
String getSkin() { return "scales"; }
}

12
How is this an improvement?

• Adding a new animal type, does not require


revising and recompiling existing code
• Mammals, birds, and reptiles are likely to differ
in other ways, and we’ve already separated
them out (so we won’t need more switch
statements)
• We’ve gotten rid of the flags we needed to tell
one kind of animal from another
• Basically, we’re now using Objects the way they
were meant to be used

13
Bad Smell Examples

• You should refactor any time you detect a “bad


smell” in the code
• Examples of bad smells include:
• Duplicate Code
• Long Methods
• Large Classes
• Long Parameter Lists
• Multi location code changes
• We will discuss most or all of these later

14
Extract Method Refactoring Example

void printFormatted(string text) {


System.out.println(“Author name: mohammad Ahmad”);
System.out.println(“Author title: senior developer“);
printRest(text);
}
void printFormatted(string text) {
printAuthorDetails();
printRest(text);
}
Void printAuthorDetails(){
System.out.println(“Author name: mohammad
Ahmad”);
System.out.println(“Author title: senior developer“);
} 15
Rename Method Example

void retStudInf() { ... }

Void readStudentDetails() { … }

16
Encapsulate Field Example

class Student {
Public Vector marks,
}

class Student {
protected Vector marks;

Vector getMarks(){
return marks;
}
}

17
Parameterize Method Example

class Handling{
public void handlePut() { }
public void handleGet() { }
}

class Handling{
public void handle(ServiceType st) {
...
}
}

18
Replace a Constant Number with Symbolic
Constant
double potentialEnergy(double mass, double
height) {
return mass * height * 9.81;
}

static final double GRAVITATIONAL_CONSTANT =


9.81;

double potentialEnergy(double mass, double


height) {
return mass * height *
GRAVITATIONAL_CONSTANT;
}
19
Split Temporary Variable Example

double temp = 2 * (height + width);


System.out.println(temp);
temp = height * width;
System.out.println(temp);

final double perimeter = 2 * (height + width);


System.out.println(perimeter);
final double area = height * width;
System.out.println(area);

20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy