Ask HN: Inherited the worst code and tech team I have ever seen. How to fix it?
My $work had been in that situation a couple years before I&#x27;d started.  I&#x27;ve been working on revitalizing&#x2F;removing some of the C we had for several years, while working alongside people similarly managing the php.<p>I don&#x27;t have silver bullets for you, but hopefully you can benefit from my experiences.<p>&gt; - this code generates more than 20 million dollars a year of revenue<p>Priority 0: don&#x27;t fuck this up.  Proceed cautiously, with intention.  Focus on observability before you make changes.  Get some sort of datadog type product, or run something in house.<p>Start building the culture of understanding risk, mitigating risk by having monitors.  Get the other developers on a pager duty rotation, work to get them personally invested in operational excellence.<p>Get management on board with investing time in it:  it&#x27;s risk mitigation for their business.  Get any incidents in front of them.  Explain how and why it happened, what lead up to it, and things you&#x27;re considering doing to remediate.  Track how much time winds up getting spent there, and use that as an argument to proactively fix things.  Most management will understand that if you&#x27;re getting randomized, you&#x27;re not being able to make progress on any single issue.<p>Work on getting a docker compose setup going so you can easily create a dev environment that looks exactly like production.<p>Use that to start creating black box tests.  Consider things like selenium or postman.  Your goal is to test as though you were your user and have no clue about the internals of the program.  You do this so that when you make changes, you&#x27;re not having to update tests as well.  Write the tests first.  Think in terms of TDD given&#x2F;when&#x2F;then.  As you add new code, write unit (and integration) tests.  Don&#x27;t try to unit test existing code unless it&#x27;s very simple.<p>&gt; it runs on PHP<p>I feel the pain.  Part of the engineering challenge here is accepting unfortunate initial conditions.  Your goal is to raise the bar to sustainable.<p>&gt; it has been developed for 12 years directly on production with no source control ( hello index-new_2021-test-john_v2.php )<p>Priority 1: get this in source control.  If you can&#x27;t get the other devs immediately onboard, copy what&#x27;s in production to your local machine and start a git repository.  If you need to copy down files after they&#x27;ve changed them in production, that&#x27;s ok,  just start building the repository and tracking some history.<p>After rsyncing changes down, you&#x27;ll be able to diff with your latest checkout to see what changes have been made.<p>This is another mentoring opportunity.  Show the other devs how using git is making your life easier.  Show them how it&#x27;s helping you manage the risk that they&#x27;re afraid of.<p>Ideally, get a gitlab or github account for it, and start getting a CI pipeline going.  Proceed slowly here and make sure you build consent from everyone.  Maybe start with a private gitlab account and again, show the other devs how it&#x27;s saving you time.<p>The first iterations may just be starting a Dockerfile to recreate the production environment.<p>&gt; - it doesn&#x27;t use composer or any dependency management. It&#x27;s all require_once.<p>&gt; - it doesn&#x27;t use any framework<p>The silver lining here is that it means you don&#x27;t have any external dependencies :) My biggest concern here would be:
- is it using pdo&#x2F;mysqli, or is it on the legacy mysql extension?
- is it using parameterized queries, or are you going to need to audit for sql injections.<p>Chip away at this over time. It&#x27;s not urgent. I&#x27;m sure many HN heads may explode at that thought -- but until you&#x27;ve triaged everything, everything seems urgent.  You&#x27;ve got unmet basic prerequisites here.  Say you start fixing sql injections before observability -- how do you know you haven&#x27;t accidentally broken some page?<p>&gt; - no caching ( but there is memcached but only used for sessions ...)<p>Nothing to fix! wonderful!  You can figure out a good caching strategy after everything else is under control<p>&gt; - the routing is managed exclusively as rewrites in NGInX ( the NGInX config is around 10,000 lines )<p>Having it centralized is actually a bit of a blessing.  It means you&#x27;re not having to scour the application for where it&#x27;s being routed.<p>Start collecting nginx access logs, and getting metrics on what the top K endpoints are.  Focus on those.  Configure it to have a slow request log as well as an error log.<p>Do yourself a favor and setup the access log to use tabs to delimit fields.  It&#x27;ll make awking it, or pulling it into a database for querying much easier.<p>&gt; - no code has ever been deleted. Things are just added . I gather the reason for that is because it was developed on production directly and deleting things is too risky.<p>The silver lining is that the unused code is inert.  This is another &quot;chip away with time&quot; type task.  start finding paths that haven&#x27;t had requests in N months. Use analysis tools to show that something isn&#x27;t ever called.  When someone starts with the &quot;well, what if...&quot;, remind them that it&#x27;s in the repository, and isn&#x27;t gone forever.  It&#x27;s just a revert away.<p>A bit theme here is fear.  You need to start instilling confidence and resiliency in the team.<p>&gt; - the database structure is the same mess, no migrations, etc... When adding a column, because of the volume of data, they add a new table with a join.<p>This isn&#x27;t the worst thing in the world.  It&#x27;s also not urgent.  Start putting together ERD diagrams, get the schema in source control, get a docker image going so that you can easily stand up a test database in a known state, nuke it, and start over.<p>&gt; - JS and CSS is the same. Multiple versions of jQuery fighting each other depending on which page you are or even on the same page.<p>Slowly work on normalizing the jquery version.  Identify all the different versions used, where they are, and make a list.  Chip away at the list.<p>&gt; no MVC pattern of course, or whatever pattern. No templating library. It&#x27;s PHP 2003 style.<p>Not the end of the world -- this is pretty low on the priority list.  Both are luxuries, and you&#x27;re in Sparta. Start identifying the domain models, define POD classes for them, start moving the CRUD functions near by.  The crud functions can just take the POD classes and a database connection.<p>&gt; In many places I see controllers like files making curl requests to its own rest API (via domain name, not localhost) doing oauth authorizations, etc... Just to get the menu items or list of products...<p>Same as with the domain model and jquery, make a list, chip away over time.  Be sure the curl calls have timeouts.  Slowly replace the self-http-requests with library calls.  Explain how if you only have N request workers, if all of those N requests are then making subrequests that there wont be any workers available to serve them, and they&#x27;ll fail.<p>&gt; - team is 3 people, quite junior. One backend, one front, one iOS&#x2F;android. Resistance to change is huge.<p>This is a bit of a social problem.  4 people can be very effective though, if you&#x27;re all working together well.  Get to the root of why they&#x27;re resistant and fix that.  Are they just set in their ways?  Afraid?<p>Work with them to rank their top 3 challenges, and work through what solutions may be.<p>&gt; - productivity is abysmal which is understandable. The mess is just too huge to be able to build anything.<p>&gt; This business unit has a pretty aggressive roadmap as management and HQ has no real understanding of these blockers. And post COVID, budget is really tight.<p>Measure, so management gets some visibility. Push back on work if you don&#x27;t understand it.  Be clear about what a &quot;definition of ready&quot; and &quot;definition of done&quot; is.<p>Don&#x27;t stop the world to fix things.  Consider having one person working on a fixup project while everyone else gives them cover by taking on the management ask.<p>&gt; I know a full rewrite is necessary, but how to balance it?<p>I can&#x27;t emphasize it enough, <i>do not rewrite</i> -- resist the urge, if you can&#x27;t, find a different job.  One of the challenges here is integrating with respect to time.<p>If you have no observability, and no tests, how are you supposed to even show that your rewrite behaves correctly?  And if your team is so afraid of breaking something to the point of never deleting code, how do you expect them to handle deleting <i>all</i> the code?<p>That&#x27;s about all I&#x27;ve got in me.  I hope you&#x27;re able to implement some meaningful change.  Take it one day at a time, and just try to make it better than it was the day before.  Good luck :)