Recfactoring duplication of codes without adding complexity?
NickName:Howard Chou Ask DateTime:2015-06-11T12:49:32

Recfactoring duplication of codes without adding complexity?

I have the following simple methods of very similar code, I like to extract out the common bits, but not if it adds complexity to the method, can someone give me an example or even links on how this can be achieved?

Here are two examples:

public function findRecipeById($id)  
{
   $query = "SELECT * FROM assets WHERE asset_type = 'recipe' AND asset_id = $id";
   ...fetch query data ...
}

public function findArticleById($id) 
{
   $query = "SELECT * FROM assets WHERE asset_type = 'article' AND asset_id = $id";
   ...fetch query data ...
}

As you can see, these two are just some very simple methods to illustrate my problem, they have almost identical query string, and the only option I can think of is to convert them into the following method:

public function findAssetById($assetType, $assetId)
{
   $query = "SELECT * FROM assets WHERE asset_type = '".$assetType."' AND asset_id = $id";
   ...fetch query data ...
}

So the problem I see is that I now have reduced duplication of code and reduced 2 methods into 1, but at the same time I have also created a method with higher complexity than needed by introducing more parameter.

a few key things that are not illustrated here:

1) the query could be much bigger and

2) the complexity of the refactored method could be much higher with many more params added.

My question for the experts out there is if there's a better way to reduce code duplication in this case without introducing complexity? Or is it better to just leave it as 2 simpler methods instead?

Thanks!

Copyright Notice:Content Author:「Howard Chou」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30771796/recfactoring-duplication-of-codes-without-adding-complexity

More about “Recfactoring duplication of codes without adding complexity?” related questions

Recfactoring duplication of codes without adding complexity?

I have the following simple methods of very similar code, I like to extract out the common bits, but not if it adds complexity to the method, can someone give me an example or even links on how thi...

Show Detail

Comments in codes increase time complexity of a program?

Do Comments in code increase time complexity of a program.? I have many running applications wherein there are codes which are commented within functions. These commented codes are of 500 lines w...

Show Detail

What is the Time Complexity and Space complexity of the following codes?

Here are the codes attached below. I have done these problems in one of the FAANG companies. I am open to have a discussion on time complexity and space complexity of these codes. Code1: public sta...

Show Detail

Time and space complexity of these codes

I just learned about time and space complexity and I dont think I am really grasping them. I will put up some codes would would ask kindly if some of you could tell me their time and space complexi...

Show Detail

How to post id with auto increment and without duplication

How can i post integer id in database manually without a duplication.I tried many codes but not working. Below is my code how can i check if id is empty and Post id without duplicating ...

Show Detail

finding the correct time complexity of these codes

I was trying to find time complexity of these 2 codes but i am not sure about my answers . code1 int i = 1; int count = 0; while (i < n) { for (int j = 0; j < i; j++) ...

Show Detail

Avoid race condition in accessing db data without synchronized in java?

List<String> Codes= req.getCodes(); List<String> existingCodes = helper.getExistingCodes(Codes); Codes.removeAll(existingCodes); for (String Code : Codes) { addCode(Code); } I am get...

Show Detail

Determine the time complexity based on my codes and do some alteration

I have a question regarding time complexity in my codes. I tried to refer to a number of sites and tutorials and also stackoverflow explanations but still could not understand how we calculate or

Show Detail

TypeScript code quality with Sonar : code duplication, cyclomatic complexity

We recently adopted TypeScript as our official frontend programming langage for the whole company, because of all of the language's qualities, but we are facing a potential showstopper : Sonar supp...

Show Detail

Tools for measuring empirical computational complexity of Java codes?

I have a few Java codes for which I wish to measure empirical computational complexity . There is a trend-prof tool which takes as input compiled C/C++ programs . Are there similar tools to the tr...

Show Detail