How to get idea from other people's codes?
Posted by Popular_Camel8575@reddit | learnprogramming | View on Reddit | 7 comments
So I can write code just fine. The problem comes when there's a really complex problem that I can't figure out all by myself and have to resort to looking for solutions online. The hurdle I come across is reading other people's codes. It makes me want to give up. I cannot for the life of me figure out how they work or understand the system of their programs. Thus, I can't solve the problem in my code. What are your tips for understanding other people's codes? Do I have to go through the trouble of running their program just to see how it functions? Lemme know your tips and advice on how to handle this
NeighborhoodOld6737@reddit
Reading other peoples code got way easier for me when I stopped trying to understand the entire program at once. Pick one function, one weird variable, one output path and follow that thread. The full codebase starts looking less like a wall of noise
vegan_antitheist@reddit
Sounds to me like you can't write code just fine. What kind of code do youneven mean? I work mostly for banks and they want do to be as easy and stupid as possible because it's important it can be tested and maintained. Everything complicated or technical has to be done by the framework.
TigerAnxious9161@reddit
Try to find what can be done better
lukkasz323@reddit
Learn how to use debugger, where the entry point of the program is, and how it flows from there.
Alternative you can do it the other way around and go from a random piece of code back in the direction of entry point, but this might be harder to do depending on the code base, language, and whether you look at it on GitHub or from your own editor, because:
sometimes there will be no import at the top of the file, or functions aren't pure and have side effects that go out of bounds of the file.
AI is generally decent at creating a basic overview of a codebase, if you don't know where to start looking.
A profiler is good for checking where the most important parts area, and where there are just utilities, that are used maybe once per program.
Fresh_Instruction178@reddit
Don't read their whole solution. Find the one function or data structure doing the interesting work, ignore the rest. Most code is scaffolding, the actual insight is usually 5-10 lines. Trace backwards from the output.
Potential_Copy27@reddit
I'm not entirely sure what you mean - though I can see if said code contains something new, it can be confusing to grasp sometimes.
When I go for new code (or audit output from an AI), I:
Whenever I encounter something like this, I essentially try to boil it down to a simple logical "lego brick".
As a bit of an obscure type, let's take C#'s
ConcurrentBag<T>generic as an example - you can derive all sorts of things from it by analyzing all the docs and all the StackOverflow and Reddit answers you want, but that is too much to retain for a brain at work...You need to derive the essential usage of a piece of code - to take
ConcurrentBag<T>again; what it essentially is, is a thread-safeList<T>that can be written to from any thread. Along with eg.Enumerable.AsParallel().ForAll()and a method call, you can easily process large batches of data in parallel on multiple CPU cores in C#.While powerful, it's still a very small logical "lego piece" in my head - I may not remember the code exactly or know 100% how it works in detail, but I remember enough to plop it in and take a valid guess, or at least to search for the proper syntax/snippet...
After using that given piece of code a few times, it becomes routine - you call upon that logical "lego piece" that your brain has linked with some code and/or muscle memory...
szank@reddit
To be able to read one one needs to read code. The more the better.
Spinning up a debugger and putting break points in places of interest also helps a lot to figure out how the data flows through.