Jump to content

Functions with same name without code duplication


john_zakaria_s

Recommended Posts

Hi

now i have a lot of functions that exists in a file (commonfunctions.php)

and my manager want to change a specific function to do a job in a section and another job in another section by adding a condition in query 

and he is not accepting code duplication, so when i took the same function and changed it's name then adding the condition, he refused this in code review by telling me i don't want code duplication

 

can any one guide me how i can do this?

 

Link to comment
Share on other sites

2 hours ago, john_zakaria_s said:

Hi

now i have a lot of functions that exists in a file (commonfunctions.php)

and my manager want to change a specific function to do a job in a section and another job in another section by adding a condition in query 

and he is not accepting code duplication, so when i took the same function and changed it's name then adding the condition, he refused this in code review by telling me i don't want code duplication

 

can any one guide me how i can do this?

 

Not trying to sound rude, but you might think about dusting off your resume if that is one of your job duties. If it isn't (which has happen to me in the past) explained to him/her that you don't know how to accomplish the task.

Edited by Strider64
Link to comment
Share on other sites

I'm going to assume that you have to add a new parameter to this function.  And then you will add some code to check what that new value is and perform some piece of the function a little differently, depending on this new value.

I would make sure that I set the new argument to a default value (null?) and modify the code changes to do the same old thing if this new value is that default value.  Any other value your changed code will then handle.  The new argument must be the last in the function header so that if it is not sent in a call the default value will work.

Keep the same function name.

function xyz($arg, $arg2, $arg3, $newarg=null)
{
	(do something normal)
	if ($newarg == null)
	{
		(continue to do something normal)
	}
	elseif($newarg == 'x')
	{
		do something new
	}
	elseif($newarg == 'y')
	{
		do something other new thing
	}
	(finish up the function)
	return;
}

An example of how it might look.  Should give an idea of how to work this out.

I can understand your manager not wanting you to duplicate code.  It isn't necessary and it only creates more lines of code to maintain down the road which means more hours and such.  And now that you see how you could modify this function for his new purpose I hope you understand.

Link to comment
Share on other sites

The solution will of course depend on what you are trying to accomplish. If the bulk of the code in the function is the same for both jobs, you might be able to keep that code in a single function. Then build another function(s) to handle the unique parts of each job type.

If you need further assistance, we'll likely need to see the function code for each job.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.