Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing existing functions

When adding a new feature to an existing system if you come across an existing function that almost does what you need is it best practice to:

  • Copy the existing function and make your changes on the new copy (knowing that copying code makes your fellow devs cry).

-or-

  • Edit the existing function to handle both the existing case and your new case risking that you may introduce new bugs into existing parts of the system (which makes the QA team cry)
    • If you edit the existing function where do you draw the line before you should just create a new independent function (based on a copy)...10% of the function, 50% of the function?
like image 629
TownCube Avatar asked Apr 16 '26 13:04

TownCube


1 Answers

You can't always do this, but one solution here would be to split your existing function in other tiny parts, allowing you to use the parts you need without editing all the code, and making it easier to edit small pieces of code.

That being said, if you think you can introduce new bugs into existing parts of the system without noticing it, you should probably think about using units tests.

like image 85
Leo Avatar answered Apr 21 '26 14:04

Leo