john_zakaria_s Posted February 22, 2023 Share Posted February 22, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/315936-functions-with-same-name-without-code-duplication/ Share on other sites More sharing options...
Strider64 Posted February 22, 2023 Share Posted February 22, 2023 (edited) 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 February 22, 2023 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/315936-functions-with-same-name-without-code-duplication/#findComment-1605887 Share on other sites More sharing options...
ginerjm Posted February 22, 2023 Share Posted February 22, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315936-functions-with-same-name-without-code-duplication/#findComment-1605888 Share on other sites More sharing options...
cyberRobot Posted February 22, 2023 Share Posted February 22, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315936-functions-with-same-name-without-code-duplication/#findComment-1605892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.