0% found this document useful (0 votes)
19 views7 pages

SR Ass Part2

SR ass Part2

Uploaded by

Noor-Ul Ain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

SR Ass Part2

SR ass Part2

Uploaded by

Noor-Ul Ain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Simplifying Conditional Expressions

1. Consolidate Conditional Expression


o Problem: You have multiple conditions that result in the same outcome.

java
Copy code
if (age > 65) {
giveSeniorDiscount();
}
if (hasDisability) {
giveSeniorDiscount();
}

o Solution: Combine the conditions into a single conditional expression.

java
Copy code
if (age > 65 || hasDisability) {
giveSeniorDiscount();
}

2. Consolidate Duplicate Conditional Fragments


o Problem: Code inside conditional branches is repeated.

java
Copy code
if (isMember) {
sendNotification();
applyDiscount();
} else {
sendNotification();
applyExtraCharge();
}

o Solution: Move the duplicated code outside the condition.

java
Copy code
sendNotification();
if (isMember) {
applyDiscount();
} else {
applyExtraCharge();
}

3. Decompose Conditional
o Problem: Complex conditionals make the code hard to understand.

java
Copy code
if (age > 65 && income < 50000 && isRetired) {
applyTaxRelief();
}
o Solution: Break down the conditional into smaller, meaningful methods.

java
Copy code
if (isEligibleForTaxRelief()) {
applyTaxRelief();
}

private boolean isEligibleForTaxRelief() {


return age > 65 && income < 50000 && isRetired;
}

4. Replace Conditional with Polymorphism


o Problem: You have a complex conditional that checks object types.

java
Copy code
void calculateDiscount(Customer customer) {
if (customer instanceof SeniorCustomer) {
applySeniorDiscount();
} else if (customer instanceof RegularCustomer) {
applyRegularDiscount();
}
}

o Solution: Use polymorphism to replace the conditional.

java
Copy code
abstract class Customer {
abstract void applyDiscount();
}

class SeniorCustomer extends Customer {


void applyDiscount() {
applySeniorDiscount();
}
}

class RegularCustomer extends Customer {


void applyDiscount() {
applyRegularDiscount();
}
}

Simplifying Method Calls

1. Add Parameter

 Problem: A method is hardcoded to handle only one specific case, reducing its
flexibility.

java
Copy code
class Order {
void applyDiscount() {
totalPrice -= 10; // Hardcoded discount
}
}

 Solution: Add a parameter to allow different discount values.

java
Copy code
class Order {
void applyDiscount(double discountAmount) {
totalPrice -= discountAmount;
}
}

2. Remove Parameter

 Problem: A parameter is unnecessary because the value can be derived from existing
fields or state.

java
Copy code
class Employee {
void setSalary(double salary, boolean isFullTime) {
this.salary = isFullTime ? salary : salary * 0.5;
}
}

 Solution: Remove the redundant parameter and use the class's existing field to derive the
value.

java
Copy code
class Employee {
boolean isFullTime;

void setSalary(double salary) {


this.salary = isFullTime ? salary : salary * 0.5;
}
}

3. Rename Method

 Problem: The method name does not clearly communicate its functionality.

java
Copy code
class Customer {
void calc() {
// Calculate total order price
}
}

 Solution: Rename the method to reflect its purpose.

java
Copy code
class Customer {
void calculateTotalPrice() {
// Calculate total order price
}
}

4. Separate Query from Modifier

 Problem: A method is both querying data and modifying state, making it harder to
reason about.

java
Copy code
class BankAccount {
double withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
return balance;
}
}

 Solution: Separate the query (returning the balance) from the modification (withdrawing
money).

java
Copy code
class BankAccount {
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}

double getBalance() {
return balance;
}
}

Dealing with Generalization

1. Pull Up Field
o Problem: Two or more subclasses have the same field, leading to code
duplication.
java
Copy code
class Teacher {
String name;
}

class Student {
String name;
}

o Solution: Move the shared field to the superclass to reduce duplication.

java
Copy code
class Person {
String name;
}

class Teacher extends Person { }


class Student extends Person { }

2. Pull Up Method
o Problem: Two or more subclasses have the same method.

java
Copy code
class GasCar {
void startEngine() {
System.out.println("Gas engine starting...");
}
}

class ElectricCar {
void startEngine() {
System.out.println("Electric engine starting...");
}
}

o Solution: Move the method to the superclass to avoid duplication.

java
Copy code
class Car {
void startEngine() {
System.out.println("Engine starting...");
}
}

class GasCar extends Car { }


class ElectricCar extends Car { }

3. Pull Up Constructor Body


o Problem: Multiple subclasses have constructors with similar logic.

java
Copy code
class Desktop {
String model;

Desktop(String model) {
this.model = model;
}
}

class Laptop {
String model;

Laptop(String model) {
this.model = model;
}
}

o Solution: Move the shared constructor body to the superclass.

java
Copy code
class Computer {
String model;

Computer(String model) {
this.model = model;
}
}

class Desktop extends Computer {


Desktop(String model) {
super(model);
}
}

class Laptop extends Computer {


Laptop(String model) {
super(model);
}
}

4. Push Down Field


o Problem: A field in the superclass is only relevant to certain subclasses.

java
Copy code
class Employee {
String programmingLanguage; // Only used by developers
String department;
}

class Developer extends Employee {


// Uses programmingLanguage
}

class Manager extends Employee {


// Does not use programmingLanguage
}
o Solution: Move the field to the relevant subclass to avoid cluttering the
superclass.

java
Copy code
class Employee {
String department;
}

class Developer extends Employee {


String programmingLanguage;
}

class Manager extends Employee { }

5. Push Down Method


o Problem: A method in the superclass is only applicable to some subclasses.

java
Copy code
class Employee {
void writeCode() {
System.out.println("Writing code...");
}
}

class Developer extends Employee { }


class Manager extends Employee { }

o Solution: Move the method to the relevant subclass to make the superclass more
general.

java
Copy code
class Employee {
// No writeCode method here
}

class Developer extends Employee {


void writeCode() {
System.out.println("Writing code...");
}
}

class Manager extends Employee { }

You might also like