2 methods that are 75% the same? [closed]

I have code that violates the DRY principle and I would like to consolidate 2 methods as much as I can, but the problem I am facing is that they have some differences, I would say they are about 75% the same, but the other 25% is different in certain areas. What kinds of design patterns or design approaches can I do to turn 2 methods into 1 or at least limit the code needed in each, so that they become more scalable and maintainable?

4

This is difficult to answer without looking at the code. However, it is clear that these methods are trying to do too much. They are almost certainly violating the SRP.

I would suggest that you begin by looking at where the methods are different and why. Perhaps you could use method injection to inject different code into the method, based on who or what is calling it.

Fundamentally, when it comes to refactoring code like this you should be trying to figure out why the code is the same and why it is different. Once you understand these two things, you can then factor these methods so that they are no longer trying to do too much.

6

Too difficult to assess without having a code segment to analyse.

Method consolidation should definitely be applied here, you’re just going to need to keep a few principles in mind. Have a read through this article on Jeff Atwood’s blog:

Curly’s Law: Do One Thing

Pay attention to the three core principles of modern software development, namely:

  • Don’t Repeat Yourself
  • Once and Only Once, and
  • Single Point of Truth

Although I personally wouldn’t recommend the following, I’m going to show you an example for simplicity sake if you simply wish to wrap things up:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function my_method(variable){
/*
* 75% of method here
*/
if (variable == 0){
/* variation one here */
}
else{
/* variation two here */
}
return value;
}
</code>
<code>function my_method(variable){ /* * 75% of method here */ if (variable == 0){ /* variation one here */ } else{ /* variation two here */ } return value; } </code>
function my_method(variable){
/*
 * 75% of method here
 */
if (variable == 0){
    /* variation one here */
}
else{
    /* variation two here */
}
return value;
}

1

One common way to refactor code like this is through inheritance, using abstract or virtual methods for the behavior that may vary.

Let’s say you have a method to pay employees, and another one to pay the CEO:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void PayNormalEmployee()
{
// do stuff
// do more stuff
// give meager bonus
// do final stuff
}
void PayCeo()
{
// do stuff
// do more stuff
// give huge bonus
// do final stuff
}
</code>
<code>void PayNormalEmployee() { // do stuff // do more stuff // give meager bonus // do final stuff } void PayCeo() { // do stuff // do more stuff // give huge bonus // do final stuff } </code>
void PayNormalEmployee()
{
    // do stuff
    // do more stuff
    // give meager bonus
    // do final stuff
}

void PayCeo()
{
    // do stuff
    // do more stuff
    // give huge bonus
    // do final stuff
}

Let’s assume that you have to do things in the order above; you can’t just reorder to set the bonus part as the last step.

The way to refactor this is through inheritance. You put the common behavior in a base class, and put the distinct behavior in distinct subclasses. Your common line (or set of lines) should call out to a separate method. For instance:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>abstract class EmployeeBase
{
public void Pay()
{
// do stuff
// do more stuff
PayBonus();
// do final stuff
}
protected abstract void PayBonus();
}
class NormalEmployee : EmployeeBase
{
override void PayBonus()
{
// pay meager bonus
}
}
class Ceo : EmployeeBase
{
override void PayBonus()
{
// pay huge bonus
}
}
</code>
<code>abstract class EmployeeBase { public void Pay() { // do stuff // do more stuff PayBonus(); // do final stuff } protected abstract void PayBonus(); } class NormalEmployee : EmployeeBase { override void PayBonus() { // pay meager bonus } } class Ceo : EmployeeBase { override void PayBonus() { // pay huge bonus } } </code>
abstract class EmployeeBase
{
    public void Pay()
    {
        // do stuff
        // do more stuff
        PayBonus();
        // do final stuff
    }

    protected abstract void PayBonus();
}

class NormalEmployee : EmployeeBase
{
    override void PayBonus()
    {
        // pay meager bonus
    }
}

class Ceo : EmployeeBase
{
    override void PayBonus()
    {
        // pay huge bonus
    }
}

Whether your employee is a NormalEmployee or a CEO, he/she has access to the Pay method, but the implementation in the derived class will be used.

If there’s a “default” implementation, you can make this simpler:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Employee
{
public void Pay()
{
// do stuff
// do more stuff
PayBonus();
// do final stuff
}
protected virtual void PayBonus()
{
// pay meager bonus
}
}
class Ceo : Employee
{
override void PayBonus()
{
// pay huge bonus
}
}
</code>
<code>class Employee { public void Pay() { // do stuff // do more stuff PayBonus(); // do final stuff } protected virtual void PayBonus() { // pay meager bonus } } class Ceo : Employee { override void PayBonus() { // pay huge bonus } } </code>
class Employee
{
    public void Pay()
    {
        // do stuff
        // do more stuff
        PayBonus();
        // do final stuff
    }

    protected virtual void PayBonus()
    {
        // pay meager bonus
    }
}

class Ceo : Employee
{
    override void PayBonus()
    {
        // pay huge bonus
    }
}

Again, the CEO has access to the Pay method, but will use his/her special PayBonus implementation; but everything will still happen in the proper order.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật