TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How to be productive with big existing code base

284 pointsby maheshsabout 6 years ago
I have just started working with one of the client who have existing nodeJS code which they build in last 3 years.<p>Is there any guiding principle which is beneficial while working with existing code base?

67 comments

cbanekabout 6 years ago
My #1 rule for existing codebases: Just because you wouldn&#x27;t have done it the way they did doesn&#x27;t mean they did it wrong.<p>I think it&#x27;s developer nature to look at a huge pile of code that someone else wrote and immediately think: &quot;This is a pile of crap. I can do better, so the first thing to do is rewrite all of this, my way (which just so happens to be _The Right Way_).&quot;<p>Figure out what you&#x27;re trying to do, and what is keeping you from doing it. Take an iterative approach to get things done. Realize that after 3 years, they have hopefully fixed a lot of bugs and got to a solution that is somewhat mature and better than you can do in a week.
评论 #19268448 未加载
评论 #19268402 未加载
评论 #19269139 未加载
评论 #19269039 未加载
评论 #19269532 未加载
评论 #19268957 未加载
评论 #19268923 未加载
评论 #19270968 未加载
评论 #19269681 未加载
评论 #19263851 未加载
评论 #19269087 未加载
评论 #19268343 未加载
nisaabout 6 years ago
I have a similiar problem like you, except it&#x27;s Java and more like 15 years old...<p>What helped? Using a debugger and stepping through the code was useful, it&#x27;s more a less a REST-API here (build ontop of the system, before it was SOAP, etc.pp) and I&#x27;ve just used some heavily used endpoints and stepped through all the way...<p>Another huge boost in understanding was using flamegraphs (not sure what&#x27;s hip for nodejs maybe this? <a href="https:&#x2F;&#x2F;github.com&#x2F;davidmarkclements&#x2F;0x" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;davidmarkclements&#x2F;0x</a>)<p>This was really an eye opener because that app also used an external huge Java ECM and there was lot&#x27;s of AOP magic, reading the flamegraphs and looking at the source was a big boost in understanding.<p>It&#x27;s also a really useful tool to get visibility for performance problems that are not directly visible in the code.<p>If there are tests, reading them might also be worthwile.<p>And take your time... took me a few months to get a basic understanding how it&#x27;s working (I&#x27;m more sysadmin, not really a dev there), so don&#x27;t except to grasp everything in one week.<p>Ask your colleagues - maybe were to find documentation or if you don&#x27;t understand something while reading the source.
评论 #19269615 未加载
评论 #19268723 未加载
评论 #19269904 未加载
spricketabout 6 years ago
It depends. Number one, find out if the codebase is bad or just big. This will take a few months, so I try to keep my mouth shut for a while.<p>If it&#x27;s really that bad, build a world in a teacup. Try to make one small new area of code that&#x27;s nice and slowly work existing code into it whenever you get the excuse. It&#x27;s very unlikely they&#x27;ll allow you to rewrite or even make substantial changes to existing code. If it was allowed, somebody would have done it.<p>It&#x27;s also unlikely you&#x27;ll ever have the codebase migrated completely. In my case, this meant migrating part of the app to a new web framework while keeping the ORM layer relatively the same. Focus on the worst parts. Kinda bad stuff can wait. Expect to write some glue between the worlds on your own time.<p>In your case, IMO Node is a bad plaform for large code bases. My approach would be to introduce TypeScript into a small corner of the app and grow it over time. Even in the existing code, Typescript will type checked the JS and make work&#x2F;refactoring easier.<p>Once you have typescript up and going, pull in some add-ons to make Node work with async code. The biggest downsides of Node are dynamic typing and callback hell. Typescript + async + heavy linting so this doesn&#x27;t happen again should put you on a good path unless there&#x27;s more demons lurking in their stack
评论 #19269729 未加载
评论 #19271298 未加载
mannykannotabout 6 years ago
You haven&#x27;t said much about your role here, but let&#x27;s assume that programming figures prominently. Your immediate problem is that you will be given tasks that involve making changes to a codebase that you don&#x27;t understand very well. There are many different ways to understand a codebase, and to be effective, you will need to learn some of all of them.<p>Firstly, there is the purpose of the system, which means getting to know what its users want from it and how they use it to achieve that. I put that first, because everything else follows from it, but that does not mean that you have to know all there is to know about that aspect before tackling anything else.<p>You will need to learn about your environment&#x27;s process: whatever is used for task assignment, scheduling and tracking; version control; building; testing and verification, inspections, test setup and execution; integration and deployment. Of these, you will need to know how to get the source code and test any changes you make, before you can do any programming, and a significant landmark in getting to know the system is when, given nothing but a backup of the source and configuration files, you could resurrect it.<p>The more you know about the architecture, the better - it is the first step in understanding how the system meets (or fails to meet) the users&#x27; needs. The architecture often imposes requirements and constraints on how you approach completing a given task.<p>Understanding how everything works at the code level would be a desirable goal, but not one that can be achieved quickly (if at all), so you will have to be guided by what you need to understand in order to do do your assigned tasks.<p>It is also useful to know who knows what among the people you will be working with.
otrasabout 6 years ago
I recommend the book <i>Working Effectively with Legacy Code</i> by Michael Feathers. I&#x27;ve been reading it recently, and I&#x27;ve enjoyed the lessons and advice so far.
评论 #19255464 未加载
fslothabout 6 years ago
Based on my years of experience in working with a millions-of-locs over decades codebase:<p>Be aware of the abstraction fallacy: developers are often guided by this insane notion that they can get <i>rid of</i> accidental complexity by wrapping it away behind an abstraction layer. You can&#x27;t. It&#x27;s better to suck up to the complexities of the existing system, and prefer explicit, procedural copy-and-pasting than trying to invent your own abstraction layer on top.<p>The problem with the after-the-fact abstraction layer is that if the original team members are not available, you are likely not in possession of the whole theory of the software. Hence it is not likely you can in the beginning choose the right abstractions.<p>The correct way - if possible - to simplify existing code is to refactor the code itself.<p>The specific anti-pattern you will reach by following the false abstraction strategy is the lasagna architecture: <a href="https:&#x2F;&#x2F;herbertograca.com&#x2F;2017&#x2F;08&#x2F;03&#x2F;layered-architecture&#x2F;" rel="nofollow">https:&#x2F;&#x2F;herbertograca.com&#x2F;2017&#x2F;08&#x2F;03&#x2F;layered-architecture&#x2F;</a><p>Two literary works that helped me enormously to grok working with legacy code and programming in particular:<p>* Peter Naur&#x27;s paper &quot;Programming as theory building&quot; - this was an amazing eye opener to me. It specifically highlights several problems that may arise when working with legacy code when the original developers have left the building.<p>* Michael Feathers: Working Effectively With Legacy Code - not to be read necessarily as a &quot;how to&quot; recipe book, but rather as a collection of philosphies and techniques to utilize when faced with a huge in-production codebase. It can be read as a recipe book if the examples match your situation, but that&#x27;s not the point.
nephrenkaabout 6 years ago
A large codebase under active development presents a moving target; Even if you knew how something worked last week, that code might have changed twice since then. Detailed knowledge in the solution domain gets outdated fast.<p>To address this issues, I work with something I call a behavioral code analysis. In a behavioral code analysis, you prioritize the code based on its relative importance and the likelihood that you will have to work with it and, hence, needs to understand that part. Behavioral code analysis is based on data from how the organization works with the code, and I use version-control data (e.g. Git) as the primary data source. More specifically, I look to identify <i>hotspots</i>. A hotspot is complicated code that the organization has to work with often. So it&#x27;s a combination of static properties of the code (complexity, dependencies, abstraction levels, etc) and -- more important -- a temporal dimension like change frequency (how often do you need to modify the code?) and evolutionary trends.<p>I have found that identifying and visualizing hotspots speeds up my on-boarding time significantly as I can focus my learning on the parts of the code that are likely to be central to the solution. In addition, a hotspot visualization provides a mental map that makes it easier to mentally fit the codebase into our head.<p>There are a set of public examples and showcases based on the CodeScene tool here: <a href="https:&#x2F;&#x2F;codescene.io&#x2F;showcase" rel="nofollow">https:&#x2F;&#x2F;codescene.io&#x2F;showcase</a><p>I have an article that explains hotspots and behavioral code analysis in more depth here: <a href="https:&#x2F;&#x2F;empear.com&#x2F;blog&#x2F;prioritize-technical-debt&#x2F;" rel="nofollow">https:&#x2F;&#x2F;empear.com&#x2F;blog&#x2F;prioritize-technical-debt&#x2F;</a><p>I also have a book, Software Design X-Rays: Fix Technical Debt with Behavioral Code Analysis, that goes into more details and use cases that you might find useful for working with large codebases: <a href="https:&#x2F;&#x2F;pragprog.com&#x2F;book&#x2F;atevol&#x2F;software-design-x-rays" rel="nofollow">https:&#x2F;&#x2F;pragprog.com&#x2F;book&#x2F;atevol&#x2F;software-design-x-rays</a>
评论 #19270493 未加载
评论 #19270219 未加载
tomlagierabout 6 years ago
The things I do when starting on a new codebase:<p>1) Ask if there is onboarding documentation, or someone who can give you a high-level overview of the codebase. Typically finding a person with a lot of context on the code is the fastest and most thorough way to understand the responsibilities and layouts of a codebase. Ask if they can draw an ER diagram, it&#x27;s extremely valuable documentation for any additional developers.<p>2) Read all the documentation possible, especially design documentation. This should hopefully give you some clues as to both function (what) and purpose (why). The discussion around this will also introduce you to the major players in the architecture of the codebase.<p>Note this does not necessarily mean formalized design docs, it could just be searching for any README&#x27;s or relevant wiki pages. You&#x27;re just gathering threads at this point, and documentation tends to be a lot more compact and easily digestible than foreign code.<p>3) Look at the models - there will be compact representations of data at some point. This gives good insight into the shared language of the code and can give a lot of clues about how things are done. They also tend to be a lot more human-readable than other pieces of code, so this is a plus.<p>4) Find and skim the largest files. Typically these perform the majority of the work, have the most responsibility, and introduce the most bugs. Knowing roughly where the major players are and what they do makes it a lot easier to read any individual file.<p>5) Run the application, find some small behavior (a single, simple endpoint) and debug it, stepping through the application code so you can see how a particular request flows through the system. This can show you how a lot of different concerns within the code are tied together and also ensures that you&#x27;re set up for both running and debugging the codebase.<p>At this point you should have a fairly solid understanding of at least the most critical points of the codebase, and also be set up to run and debug it. You should also have at least one or two points of contact to ask questions. This gives you a good framework for figuring out how to modify the codebase moving forward.
sandreasabout 6 years ago
Some of my rules for a big legacy code base:<p>- don&#x27;t plan or do a full rewrite - it&#x27;ll almost never work<p>- learn and use tools to automate the build system and quality assurance (jenkins, sonarqube, docker, git, etc.)<p>- take the time to improve your skills and the skills of your team (coding dojos, experiments)<p>- write automated tests (unit, integration, acceptance) for existing code where ever possible - write at least unit tests and integration tests for new code<p>- do refactoring and first focus on cross cutting concerns (APIs, translations, caching, logging, database, etc.)<p>- migrate things to well tested isolated APIs (e.g. use REST &#x2F; Graphql APIs with new endpoints in the frontend and try not to use untested code for these APIs)<p>- don&#x27;t be too backwards compatible (move fast and break things)<p>Hope it helps ;)
评论 #19271205 未加载
keithnzabout 6 years ago
A lot of people are giving advice on changing&#x2F;testing&#x2F;refactoring etc, I&#x27;m not sure if that&#x27;s what you are asking, as opposed to how to lean a new code base.<p>My technique for learning code bases, other than asking other devs questions like :-<p>&quot;What architectural patterns are you using?&quot; &quot;How do you deal with testing?&quot; &quot;How is it deployed?&quot; &quot;how do you deal with data access?&quot; &quot;How do you deal with data migration&quot; &quot;how do you deal with scaling &#x2F; concurrency &#x2F; security &#x2F; authentication&quot; etc... broad stroke things.<p>If there are no other devs...look at the code and look around at some of these broadstroke things.<p>One thing you may find is they are using an architectural pattern you aren&#x27;t familiar with, so be on the lookout for things that look odd but look very deliberate and duckduckgo to see if you can find any information around names in the code. Like if you saw FooActor BarActor, google for Actor &#x2F; software etc.<p>Another thing to keep in mind when you find odd stuff is that quite a lot of devs copy paste stuff from the internet, and implement partial ideas they have learnt about, so duckduckgo for snippets you suspect.<p>Then the main thing I like to do is take a usecase from the software and follow it right through every layer, either through static inspection, or using a debugger. I then follow up on things that initially seem confusing. I then start sketch out a bit of an architectural diagram ( throwaway )<p>For Embedded systems code I tend to start from boot and draw out a bit of a flow diagram of what&#x27;s happening.
luisehkabout 6 years ago
Do they have good test coverage? That&#x27;s key. If they don&#x27;t, start with that.
评论 #19268845 未加载
评论 #19269953 未加载
评论 #19268746 未加载
评论 #19270962 未加载
评论 #19268380 未加载
评论 #19268440 未加载
xupybdabout 6 years ago
I’d say the most important thing is to learn the domain and the business you are working with. Never assume the code is doing things the right way for the business. Get to know your client really well and try to understand what they need to software to do. Keep them in the loop as much as possible.
评论 #19271163 未加载
andreasklingerabout 6 years ago
This is super specific to each project but here things that worked for me in previous projects.<p>Two assumptions: You plan to work on this longer-term (not a 1month project stint) and there are things worth improving (eg barely used legacy app might not be worth your time)<p>#1 Get the team on board<p>if there are multiple people you need their buy-in and support for whatever approaches you want to do<p>#2 Plan for &quot;health by a thousand small improvements&quot;<p>it will be an iterative approach and you will refactor as you go.<p>#3 Don&#x27;t assume different = bad<p>people might have done differently, consider using their approaches. you might do it differently. but it&#x27;s better if you keep a consistency within the codebase. in codebase management consistency trumps cleverness<p>#4 Create space<p>Consider introducing a fix-it friday where everyone can work on little improvements<p>#5 Create non-blame culture<p>Stuff will break if people risk improving things. Avoid blame shifted to them. If bug trackers ping individual people consider pinging the whole team instead<p>#6 Consider automation<p>introduce linters, autoformating, codemods, danger.js, code complexity analysis, etc<p>#7 Introduce tests<p>This one is the most annoying. But worth doing: whenever you improve a feature a bit try adding a test - often in legacy apps there are no good tests. A lot of people recommend writing a test suite for the whole app before you do anything. If you are lucky enough to do this try it. I always found the iterative approach more realistic as you can also do feature work while refactoring.<p>When doing tests focus on integration (vertical&#x2F;functional&#x2F;etc) and not unit tests (unless the &quot;unit&quot; contains critical or complex logic). Your goal is to know &quot;that you broke something&quot; - you get by if you don&#x27;t always know &quot;what you broke&quot;<p>#8 Acknowledge tech debt<p>not everything needs refactoring. If it&#x27;s not critical and nobody needs to touch it consider acknowledging it as tech debt. Add larger notes above the problematic areas and explain why you aren&#x27;t refactoring it, explain things worth knowing to understand the code better, etc. Whenever you leave comments remember that comments should explain &quot;why&quot; not &quot;what&quot; the code does.<p>hope that helps! good luck.
评论 #19268776 未加载
评论 #19268614 未加载
评论 #19268499 未加载
sqsabout 6 years ago
First, get your tooling set up, especially a code search tool with go-to-definition and find references. A good code search tool will make you much faster and better at understanding code, finding correct usages, debugging problems, etc.<p>It also makes it easy to get a URL to any line&#x2F;region in a code file to paste into email&#x2F;Slack to ask&#x2F;answer questions. (Of course, GitHub has URLs, too, but you probably aren&#x27;t browsing code on GitHub already because it lacks code navigation&#x2F;intelligence features, so getting the GitHub URL would add an extra clunky step.)<p>Here is a study of Google&#x27;s internal code search tool with some example use cases and interesting stats: <a href="https:&#x2F;&#x2F;research.google.com&#x2F;pubs&#x2F;archive&#x2F;43835.pdf" rel="nofollow">https:&#x2F;&#x2F;research.google.com&#x2F;pubs&#x2F;archive&#x2F;43835.pdf</a>. Most(?) engineers at companies with large codebases use code search frequently <i>if</i> they&#x27;ve ever tried a good code search tool (i.e., it&#x27;s hard to give it up once you&#x27;ve used it).<p>(Disclaimer: I work on a tool that does this, but I&#x27;m omitting the name&#x2F;URL because the advice is general.)
ioddlyabout 6 years ago
Lots of good comments here, but I didn&#x27;t see a code search tool recommended while skimming.<p>I use this one: <a href="https:&#x2F;&#x2F;github.com&#x2F;ggreer&#x2F;the_silver_searcher" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ggreer&#x2F;the_silver_searcher</a><p>But the important thing is to be comfortable popping open the console and using it. Makes it so much easier to &quot;research&quot; a particular part of code quickly.
评论 #19271649 未加载
dusklightabout 6 years ago
Reading Refactoring by Martin Fowler helped me a lot. The examples are in Java but many of the concepts apply across all languages. However I would say the examples makes the most sense for statically typed languages. I wonder if anyone knows of a book that covers the concepts in Refactoring but with examples in a dynamically typed language like javascript?
评论 #19268563 未加载
评论 #19268526 未加载
评论 #19268528 未加载
pizlonatorabout 6 years ago
Read a lot of code!<p>Spend time reading code in the codebase even if it doesn’t seem to make sense, even if you don’t think you’ll need to know that part of the code. Keep reading until it starts to make sense.<p>My trick has always been to just read. I even avoid tools that automatically navigate the code because I like to just read it until I know where things are.
sidcoolabout 6 years ago
Apart from what others have mentioned, it may help you to run some stats on the Git (assuming it&#x27;s Git) repos&#x2F;code. The most changed files would be the important ones. Modules&#x2F;Classes with most test coverage would be important ones. Check out for God classes (<a href="http:&#x2F;&#x2F;wiki.c2.com&#x2F;?GodClass" rel="nofollow">http:&#x2F;&#x2F;wiki.c2.com&#x2F;?GodClass</a>)<p>There is no better substitute to talking to people though.
评论 #19269788 未加载
nslindtnerabout 6 years ago
Very simple input, but they have served me very well :-)<p>1: Make sure you have development and test environment available (including data transfer from prod -&gt; test)<p>2: Source control and Easy deployment (to all environments)<p>3: Map the code into importance. (not all code is equal).<p>4: If possible spend time with the main user &#x2F; product owner (especially in peak periods). You&#x27;re blessed to have users, who understand your system.<p>And rember code that has been live for 3 years have earned a lot of expirience<p>PS: Typescript +1
TheOtherHobbesabout 6 years ago
1. Assess how much of the code is actually understood. Is there any record of the design decisions, the edge cases, the debugging process, the paths that weren&#x27;t taken? Who knows the most about the codebase and how it got to be how it is?<p>2. What&#x27;s the current specification? Don&#x27;t look at the stack, look at input and output cases. How well does the code meet the spec? Where is it failing?<p>3. Before you change anything you need to know what the change process is. You probably do already know this, but if you don&#x27;t need to find out whether there are any demarcations of responsibility, even if they&#x27;re only informal and unstated areas of interest.<p>4. When you have all that, you can start working on the code with some knowledge of the context you - and the code - are operating in.<p>5. If code works, don&#x27;t rewrite or refactor for style without a very very good reason. And don&#x27;t do it unless you can change all the &quot;bad&quot; code at once. Otherwise you&#x27;ll end up with a mess of incompatible idioms that make future changes hard to read.<p>6. Write your own docs as you go. Best case is other people will benefit from reading them, worst case is you&#x27;ll remind yourself what you were doing six months from now - because you&#x27;ll have forgotten by then.<p>If you&#x27;re a junior you may not have access to all of the above, so the fall-back is to find out what you specifically are supposed to do, and where you&#x27;re supposed to do it.<p>If that&#x27;s vague or unspecified, I&#x27;d suggest studying the code to make your own model of it and then running possible actions past other team members and the client before you make the first few changes - to establish a working pattern.
arkhabout 6 years ago
Get a copy of &quot;Working effectively with Legacy Code&quot;.<p>The definition of Legacy Code for the author is &quot;untested code&quot;. The book is mainly a list of situations a code base can be in and how to add tests.<p>Once your application is end-to-end tested you can play with the code with better peace of mind. Just adding those tests require you to get all the specifications the app has to fulfill so it is a good way to learn them.
notacowardabout 6 years ago
I just happened to write a blog post about this a while ago:<p><a href="http:&#x2F;&#x2F;obdurodon.silvrback.com&#x2F;navigating-a-large-codebase" rel="nofollow">http:&#x2F;&#x2F;obdurodon.silvrback.com&#x2F;navigating-a-large-codebase</a><p>Short version: document everything as you learn, master your tools, look at message and data formats before code, follow some of the important code paths, change something and see how the system responds.
marioptabout 6 years ago
Been there multiple times.<p>Before making such judgement, you need to be sure you know the community guidelines for that language being used, libraries, etc. You&#x27;ll need to get familiar with the language ecosystem so that you&#x27;re not the one doing things your way. You can not live inside your head.<p>You should never aim for a full refactor but rather try to refactor a module at a time. It might happen that codebase in questions doesn&#x27;t uses modules but you can start to commit one at a time.<p>The rule I follow is: I work on a ticket and will only clean&#x2F;modify the files that are relevant to the ticket I&#x27;m solving. This way you get more familiar with the codebase has you go and you might notice that some parts might actually be well written.<p>Do not fell tempted to wipe out the old code because it contains a good chunk of specification that got lost in past conversations and is probably crucial to the business logic and&#x2F;or fixes bugs. Another funny side effect is: might be buggy but actually works according to the spec :D
Pooparadoxabout 6 years ago
Never try to refactor everything. Sometimes logic may look like it was implemented incorrectly, but from my experience it may have been written like this on purpose. Business logic can be really twisted.<p>Also remember about scout rule: &quot;Leave Things BETTER than you found them.&quot;. It will help you to slowly, yet steadly improve the codebase.
评论 #19271438 未加载
hello_jerryabout 6 years ago
Step 1: investigate. find out where the program is breaking and&#x2F;or delivering unacceptable performance. This means asking stakeholders &quot;is anything broken? What is wrong with the app? what do you want to change?&quot;, reviewing exceptions, and crash logs. Keep in mind that often times stakeholders don&#x27;t know what they want - determine the business objective and then work from that. Determine where the broken code is, and write narrative comments about what you think it does. If there is documentation, read it. There often isn&#x27;t any documentation, and often times the comments are useless on a good day. Resign yourself to playing a combination garbage man&#x2F;forensic psychologist for the next few months.<p>Step 2: triage. determine which broken parts you can get away with leaving alone for the short term, and which parts need immediate attention.<p>Step 3: Fix the most critical broken parts incrementally. If there are no tests (there are never tests...), write a test for each block of code you modify. Avoid wholesale redesigns if possible. make sure that you write tests. Try to avoid getting mad about the previous person&#x27;s style - focus on getting things working to a borderline acceptable level, writing comments to explain your decisions so you or someone else has a frame of reference. The goal of doing this is to buy yourself time to clean the entire thing up.<p>Step 4: Once the app is working at a baseline acceptable level, examine the codebase and determine which areas (if any) require redesigns, and determine the cost&#x2F;benefit of each redesign, based on what the stakeholders need, want and expect. If any redesign is necessary, negotiate with stakeholders to buy time for it - your bargaining chip should be an additional feature or two that the previous guy shat the bed on. Basic criteria for a redesign: is the current design impossible to understand? Does the current design impose unacceptable costs in terms of performance or development time? if yes to either, a redesign is probably worthwhile.
Noumenon72about 6 years ago
You&#x27;ll come back to the same pieces of code over and over, forgetting most of the details and context each time. If it takes you a while to figure out, write it down. If you had to use the debugger to find out what&#x27;s in a map, leave a comment with an example of what the keys and values look like and where they&#x27;re populated from.<p>Once you have added comments, it lets you hover over a function to remind yourself &quot;This does X to Y when the deposit is a check&quot;, so you never have to read the internals of that function again when you&#x27;re not tracing a check.<p>When you have to go 12 levels deep in the call stack to find the source of parameter Y, make a note in a side wiki so you can recover that detective work the next time.<p>Your knowledge of the code base grows like compound interest when you only have to figure out what each piece of code does <i>once</i> and can skip over it after that.
OliverJonesabout 6 years ago
If you use an IDE, learn and exploit its code-exploration features. Use them all the time.<p>Do global searches whenever you aren&#x27;t sure how things work. If they aren&#x27;t fast enough for you, get an SSD. Use them to look for all kinds of things to convince yourself your change is safe.<p>Add comments as you figure things out. For example, &quot;this algorithm seems to match the one at &#x2F;src&#x2F;core&#x2F;foobar.js:123&quot;.<p>You can also add &quot;DEBT&quot; or &quot;REFACTOR&quot; to your IDE&#x27;s list of TODO tags, and use it in comments where you see something you think might need cleaning up. If you mark these places now and change some of them later, you&#x27;ll avoid doing damage; you&#x27;ll get a chance to learn more before changing things.<p>Put ticket numbers (Jira, whatever) in your comments too.<p>Think &quot;dig safe&quot;. You&#x27;re spraying painting warnings near significant opportunities to break things.
rdiddlyabout 6 years ago
It&#x27;s not exactly a guiding principle, just more of an analysis &amp; learning technique: Get ahold of a big piece of drafting paper, or a whiteboard, or one of those big-ass pads of paper they&#x27;re always putting on an easel for silly brainstorming sessions. Something physical, big, writable, and not a computer. Set it up or pin it up or tape it up, semi-permanently, and use it daily to diagram and map out each new thing you learn about the system and its interrelationships. I&#x27;m assuming here that you have the space to set something like that up. If you don&#x27;t, you&#x27;re in a 3rd world programming situation and you have my sympathies, but you can always do something graphical within the computer, too, especially if you have a big screen or multiple screens. I just have always found it quicker to do it in a physical medium.
poisonborzabout 6 years ago
To my experience <i></i>nothing works that isn&#x27;t directly besides the code &#x2F; isn&#x27;t generated by it<i></i>. So schema generation and&#x2F;or javadoc-like mechanism. Nobody will read or want to maintain separate structures, especially not arbitrary tomes like wikis.
huuugoabout 6 years ago
Keep the goals of the code in mind: providing its users value. It&#x27;s a tool, even if you think code should be art. The code&#x27;s purpose is not to look pretty or to get best marks in static analysis. It&#x27;s this code that earned the company the money they are spending now on you. Clean code pays off in the long run, but might make you go bust in the short term. In the beginning of a project, it&#x27;s usually a prototype and it needs to prove itself. Once it has generated some value, you can consider either spending time for quality improvements to negate the tech debt, or you re-write it with everything you have learnt from the previous version.
villeezabout 6 years ago
Run the code through in Softagram analyzer and start digging into the structures using the visual browsing capabilities in Softagram Desktop app. There is free trial at softagram.com where you can do it easily if you happen to have your codes in some Git repo in cloud services such as Bitbucket or GitHub... Shameless self promotion this is however, as I work for the company. But that approach I have also been personally using more than 10 years: static dependency analysis coupled with excellent visual dependency browsing. I think some expensive version of Visual Studio has also similar stuff available.
Jtsummersabout 6 years ago
Document everything as you explore it. I&#x27;m an advocate for literate programming but accept it&#x27;s not going to be accepted by most organizations. So I use it as a personal tool.<p>Tools: emacs, org mode, org babel.<p>Create a parallel directory structure, hypothetical project:<p><pre><code> .&#x2F;src .&#x2F;project&#x2F;src&#x2F;main.js .&#x2F;project&#x2F;src&#x2F;some-file.js </code></pre> Create a new directory structure with one org file per source file and one index org file:<p><pre><code> .&#x2F;project-org&#x2F;src&#x2F;main.org .&#x2F;project-org&#x2F;src&#x2F;some-file.org .&#x2F;project-org&#x2F;index.org </code></pre> (You can organize it differently, this has worked for me.)<p><i>index.org</i> will be a simple tree view of the folder hierarchy:<p><pre><code> * Project Name Description ** Source Files *** src **** [file:src&#x2F;main.org] **** [file:src&#x2F;some-file.org] </code></pre> You may add in some notes about the general purpose of each of those files.<p><i>main.org</i> copy the entire code into the main.org file like:<p><pre><code> * main.js #+BEGIN_SRC js &#x2F;&#x2F; all the code from main.js #+END_SRC * [[file:..&#x2F;index.org][Project Root]] </code></pre> Start splitting the contents of <i>main.js</i> into separate snippets, I don&#x27;t know Javascript very well so let me make some quick C example:<p><pre><code> * main.c #+BEGIN_SRC c :noweb yes :tangle yes &lt;&lt;includes&gt;&gt; &lt;&lt;structs&gt;&gt; &lt;&lt;functions&gt;&gt; #+END_SRC ** includes #+NAME: includes #+BEGIN_SRC c ,#include &lt;stdio.h&gt; &#x2F;&#x2F; [0] #+END_SRC ** structs #+NAME: structs #+BEGIN_SRC c &#x2F;&#x2F; structs #+END_SRC ** functions #+NAME: functions #+BEGIN_SRC c :noweb yes &lt;&lt;some-func&gt;&gt; &lt;&lt;main&gt;&gt; #+END_SRC *** main #+NAME: functions #+BEGIN_SRC c int main (...) {...} #+END_SRC *** some_func This function will initialize a block of memory to be used as a shared buffer between two processes. #+NAME: some_func #+BEGIN_SRC c void some_func (...) {...} #+END_SRC </code></pre> As you go through this you can make cross-references to other files and functions&#x2F;structures. Eventually you&#x27;ll find a smallest reasonable unit. A long, but clear, function doesn&#x27;t need to be dissected. But a short, complex one, may end up with each line broken out and analyzed.<p>I don&#x27;t just import a massive code base and do this in one go. Instead I import parts of it and break down all the related files to a particular topic (&quot;How does X happen?&quot;). I trace it from start to end, and then repeat with the next question. Good, modular code makes this much, much easier. The more tightly coupled, the harder it is to understand no matter the method.<p>[0] The comma is inserted by org babel to distinguish from it&#x27;s on #-prefixed content.
评论 #19268362 未加载
评论 #19268530 未加载
评论 #19268641 未加载
franeeabout 6 years ago
Took me 1 month to be productive.<p>Went in to a large rails codebase as a backend developer in the middle of a v1 to v2 rewrite and it had custom folders with their own conventions on where to put stuff.<p>All that with no documentation except for the setup.<p>What I did was just ask how the app works + specific workflows in front-end side of things and just connect the dots on the backend part.<p>Most of my troubles where on where to put modules because of their existing conventions.<p>Tips: 1. Ask a lot 2. Read the code 3. A debugger helps 4. Make sure you add tests for areas you touch.
aasasdabout 6 years ago
When improving existing code, only do gradual changes. Don&#x27;t do rewrites, don&#x27;t replace existing code with better solutions in one swoop.<p>This way, you won&#x27;t be in a situation when the new solution doesn&#x27;t work in some cases and you already thrown all the code it replaces under the bus. You&#x27;ll always have a mostly working app.<p>OTOH, if you introduce an alternative solution, finish migrating to it before beginning improvements in other places in the codebase.
tomduncalfabout 6 years ago
On a practical level, getting really familiar with grep&#x2F;similar for searching the codebase and using a debugger (I like the one built into VS Code for working with Node) will help you when trying to work out how it all fits together.<p>Depending on time and other constraints, upgrading the codebase to typescript would be a great way to both familiarise yourself with it and to make working with it more productive, but obviously you’d need client buy in.
JustSomeNobodyabout 6 years ago
1. Build the code. Don&#x27;t do anything else until you can build the code.<p>So simple, yet so many places get it wrong. Lazy devs check in code that is broken. Project structures that depend on you having things on your machine and in a particular place. Circular references. You name, I&#x27;ve cursed it.<p>If it&#x27;s a project that you should be able to check out and build locally, then you should be able to check the code out in any directory and build it. Period.
badfrogabout 6 years ago
Are you able to talk to the original author or the most recent maintainer? If so, I&#x27;d spend a couple hours reading the code on my own to get a basic familiarity, then sit down with the original author and ask them to give you an overview. They&#x27;ll probably even still remember which parts they consider bad or hacky, and knowing about that stuff could save you a lot of trouble.
AnimalMuppetabout 6 years ago
It&#x27;s a three-year-old code base? That&#x27;s <i>good</i>. It means that you can probably talk to some of the authors, to figure out what they were trying to do. A big 20-year-old code base is worse, because many of the original authors are gone, and there&#x27;s been a lot more time for it to be patched by people who didn&#x27;t understand what the original author was up to.
djklanacabout 6 years ago
Has anybody tried software intelligence tools? Seeing the architectural components and control flow of the code seems like a quick way to document the codebase and figure out which “clusters” to study. <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Software_intelligence" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Software_intelligence</a>
pelleabout 6 years ago
Git blame is one of the best tools for understanding the history and archeology of a large legacy code base.<p>There is nothing worse when you&#x27;re trying to do solve some problem that you discover a giant reformatting commit typically instituted by a youngish developer.<p>It obviously doesn&#x27;t remove the history, it just makes it so much harder to actually find out why the code was written as is.
franzwongabout 6 years ago
What is your problem with the code base actually?
评论 #19268514 未加载
edemabout 6 years ago
I&#x27;d suggest the book [Working Effectively with Legacy Code](<a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Working-Effectively-Legacy-Michael-Feathers&#x2F;dp&#x2F;0131177052" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Working-Effectively-Legacy-Michael-Fe...</a>).
Clananabout 6 years ago
Before modifying an existing line of code, understand what it&#x27;s purpose is&#x2F;was. Even if it doesn&#x27;t appear to have one, or make any sense, someone created it for a reason.<p>Also IDE&#x2F;search tools for determining where a function is used are great for removing stale, unused cruft.
ruskabout 6 years ago
If you have the time, make sure that you have adequate coverage from a suite of automated tests. This goes doubly so for a dynamic language like javascript. This will free you up to make changes and will make the whole process of introducing changes less nerve wracking.
评论 #19269687 未加载
dev_dullabout 6 years ago
If you inherit a big code base my suggestion is to always spend <i>a lot</i> of time going through the tests before jumping into the code. Learn about the business logic. What things have a lot of coverage? What broke a lot of caused regressions? You will learn so much.
gwbas1cabout 6 years ago
Unit tests &#x2F; automated tests are critical.<p>If you&#x27;re lucky, you will have a suite of unit tests and automated tests with high code coverage. Rely heavily on these tests as you refactor and debug.<p>If you don&#x27;t have working tests, consider writing them before making any major change.
sorryforthethroabout 6 years ago
That&#x27;s nothing. Try a 10 year old PHP codebase with remnants of a failed GWT integration.
评论 #19268961 未加载
JamesBarneyabout 6 years ago
Fear large changes to existing code. If you come into a large existing code base with a mindset of a smaller one you will cause 3 bugs for every one you fix.<p>Instead always try to make the smallest possible change to achieve your end goals(bug fix&#x2F;feature etc).
jrochkind1about 6 years ago
This book is pretty great:<p><a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Working-Effectively-Legacy-Michael-Feathers&#x2F;dp&#x2F;0131177052" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Working-Effectively-Legacy-Michael-Fe...</a>
tonymetabout 6 years ago
focus on outcomes. have an objective like better monitoring, better perf, better code coverage, and use that as your guiding principal. make sure any change benefits the customer or your team. avoid refactoring just to modernize, every change is a potential regression. and what is in style now will be legacy in due time.<p>once you decide on the objective, try to divide components up into interfaces so you can test and refactor a component with reduced side effects.<p>set a goal for each objective. like improve coverage 5% a month or no net neg.<p>long story short, be methodical and focus on outcomes
kilonabout 6 years ago
The only thing it gets in my nerves is lack of comments and documentation. The fallacy that code can be remotely as easy to understand as natural language text. The rest I can endure and tolerate.
ehntoabout 6 years ago
Get familiar with your debugger. You may be surprised by how deep in a call stack a bug can live. It will also make you intimate with the different ways your codebase is orchestrated together.
grigjd3about 6 years ago
Debuggers are your friend.
评论 #19268941 未加载
vladimir-yabout 6 years ago
If you need to maintain and extend the project I&#x27;d consider converting the JavaScript code to TypeScript with strict compilation option enabled.
sreedharbukyaabout 6 years ago
I have been there for couple of times now. First thing, I look into it to how to get refactor if i see something here.<p>Luckily, I had one of the best mentor, who taught me how to refactor the any piece of code without failing.<p>It is really works,If this code base test cases. If doesn&#x27;t have test cases, I think we have take one step back and do small functionality at time and move on from there.<p>Bad of big code repositories is mostly, they are still using some version of framework which is most uncomfortable to new bee for getting their head around.
jarechigaabout 6 years ago
Just squash every commit from the repo into a single commit with message &quot;Legacy code&quot; and force-push to master.<p>-David Winterbottom
ngneerabout 6 years ago
OpenGrok supports JS and helps to navigate the codebase easily, in turn helping you to reason about it. Good luck!
IloveHN84about 6 years ago
Rule #1 use the boyscout approach: clean where other left dirty stuff.<p>Sure, many here would argue that &quot;never touch a running system&quot; is a gold rule, but what if the running system is running in the wrong way?<p>If everyone attain her&#x2F;himself to this rule, there wouldn&#x27;t be what we call innovation.<p>Clear example: why develop Windows 7, 8, 10 &#x2F; Mac 10.10,.11,.12.. if their predecessors were working fine?
altmindabout 6 years ago
my #1 working with existing code bases is you need to get debugger working. nothing can explain the processing workflow better than tracing the execution.<p>also having any kind of schemas, protocol definitions and tests can help grasp whats happening in the system.
bitLabout 6 years ago
You can&#x27;t really. Especially in JavaScript world where velocity is everything. You&#x27;d spend at least ~1 year if it is really large and hit all kinds of hacks in the form of callbacks within callbacks within callbacks that are tuned in a way that (mostly) works. I&#x27;d recommend you to run away, seriously.
peterbradenabout 6 years ago
Learn to use grep well.
purple_ducksabout 6 years ago
- put a watch on the app dir filesystem<p>- put a watch on the network
egfxabout 6 years ago
Same exact boat. Case sensitive search.
评论 #19272590 未加载
mamcxabout 6 years ago
Devil advocate:<p>I made so many rewrites that it can be say is most of my job.<p>My first real project was move a FoxPro DOS app to Visual FoxPro. I probably not read any (old) code at all. The next, move torwards a N-Tier with SQL Server (that was circa 2000). Then move in other company a Fox desktop app to ASP.NET 1.<p>And that&#x27;s is only counting my first years.<p>----<p>How I could tackle this WITHOUT READING THE OLD CODE?<p>TALKING TO THE OLD DEVELOPERS!<p>If is possible for you, let them talk about how the old app work. Even better, let them (or anyone in marketing, support, etc) explain the problems that this app solve, and the new ones this app have failed to solve.<p>I have the luxury to mostly work with apps with a RDBMS in the back, and rarely fancy (and NASTY) architectures like micro-services. Understanding a RDBMS is orders of magnitude easier than the codebase:<p><a href="https:&#x2F;&#x2F;quotes.yourdictionary.com&#x2F;author&#x2F;fred-brooks&#x2F;31361" rel="nofollow">https:&#x2F;&#x2F;quotes.yourdictionary.com&#x2F;author&#x2F;fred-brooks&#x2F;31361</a><p><pre><code> Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; they’ll be obvious.</code></pre> -- Fred Brooks<p>So, before get deep in code: MAKE A DATABASE, and PICTURE THE FLOW OF DATA.<p>Sometimes, is even possible to cut a massive amount of (legacy) code that is the result of a iterative development (under pressure and without planning) that result in a terrible &quot;flow of data&quot;. Fix the flow, fix the schemas, and suddenly the code is short and easy!<p>Also:<p>Complicated software are infections of a complicated business requirements (ie: company). When something is a pile of mud, NOT FIX THE MUD.<p>Fix the business requirements until it get easier to handle. This also lead, most of time, to a massive reduction in messy code bases.<p>Also:<p>You have applied any of the sensible advice elsewhere. Instead of rewrite, you make testing and all that.<p>IF YOU FEEL IS STILL TERRIBLE AND YOU KNOW IN YOUR GUT YOU WILL GET STUCK HERE FOR ALL THE ETERNITY<p>cut that code without mercy. Not push along when you have, proved is a dead end. I made a mistake like this with a rewrite from a iOS App made by a consulting firm and lost 6 months(!!!!) trying to be reasonable.<p>This cost me the contract? You bet it. However, In the last 2 week I remove almost 60-70%(?) of the code and rewrite it to be more along the Apple guidelines. I still lost the contract but the next team? Finish it in a month.
luggabout 6 years ago
Grok.<p>Make a small change.<p>Loop.<p>-<p>When you enevitably come across a trade off, choose the one which is easiest to change later.<p>Everything else is just noise.<p>Large changes in legacy systems are often suffer from the second system effect among other problems.
poojapanwar27about 6 years ago
comment
meng3310467870about 6 years ago
aakybe How yus i do,you now pieng wued vase is just is code us fade1jsyeb ask?