Learn to Read Other Peoples' Code C++
The Well-nigh Efficient Way to Read Code Written past Someone Else
How to speed up the struggle of reading others' code in 4 steps?
As developers, regardless of our specialty, whether it existence data science, front end, or back end, we spend more than than 75% of our time reading code written by others. That task can be such a demanding task.
That beingness said, the ability to read others' code efficiently i due south one of the skills that could make one's task in software engineering science much more pleasant. Unfortunately, it is also a skill that is widely overlooked past schools, bootcamps, and companies. It is one of those skills that everyone assumes yous know, or be good at, just because yous know how to write code.
The thing nigh writing code is, anybody has their own coding style. Reading lawmaking is non similar reading a novel or a story; it's not just well-nigh reading instructions on the screen. Instead, when you read code written past someone else, you're not simply reading their code. You're trying to become into their thoughts process and what they were thinking when they wrote that code.
Needless to say, that is an extremely challenging task. Only, it's a job that you can brand exponentially easier. In this article, I will walk you through my 4 steps process of reading and understanding other people'due south code.
To explain the different steps, I volition go through a code I wrote for a web scraping tutorial.
Pace #i: Run the code and encounter what it does
Whenever you lot get a lawmaking that you need to read and sympathise, the outset thing — and nearly apparent, I may argue — to practice is to run the lawmaking and meet what information technology does. What does information technology take as inputs? What are the outputs?
So, let'due south get ahead and run the code above and explore the results.
Not all codes will event in a nautical chart; some codes' output volition be a text output. Regardless of the output type, we tin can explore information technology to guess — if you don't already know — the general idea of what the lawmaking was written to practise.
All the same, this might not give you a lot of details near the details of the lawmaking, or if this code was a office of a bigger project, you lot might not know how it all connects, but y'all will acquire how to build it and run information technology. Also, you volition get to know about the libraries it uses and the frameworks it depends on.
Nevertheless, running the code will give you the data you need to understand it better, and to make certain you got every dependency, you need to kickoff working and expanding it.
Step #2: Find the main role or the start point
Now that nosotros know what the lawmaking does and what the output is/are, we can start looking deeper into its details. To do that, nosotros need to pinpoint the start of the code. If yous're using a programming linguistic communication with a must main
office, like C, C++, or Coffee, start from there and walk your fashion through to the other functions.
If you're using Python, however, not all codes will have a primary
role, but y'all can employ the indentations to know where the code starts. For example, in the code above, nosotros have multiple functions, then the starting point of the code is on line 80 .
So, we tin offset going through the comments first — if there is any — and go through the entire main department of the code without going into the details of the subfunctions.
Going through the primary
function gives you the general flow of the code, what each subfunction does, not how they work, only what they practice.
Step #3: Run the code on debugging mode
In one case you're done carefully reading the master part of the code, y'all may find it useful to run the code in debugging mode. The reason for that is, when you run lawmaking in the debugger, information technology allows yous to observe how your code interacts with the retention.
Information technology volition show yous how each variable changes with every pace in the code. Doing so will give you a more in-depth agreement of the inner functionality of the code and its unlike functions.
Once you see how each variable in the code changes with every line, you lot can start adding your own comments in the lawmaking, explaining to yourself what every line of the code does.
Step #4: Build a mindmap of the connections between different parts of the code
One thing I establish to assist tremendously is to build a mindmap of the code connections while I am running it in the debugger mode. The debugger fashion shows you a clear connection between the dissimilar code items.
Get-go with the name of the lawmaking file in the centre of your mindmap and so branch out with the different functions and how they connect. Endeavor ti encourage the variables in the code into the mindmap as well, maybe not all of them, but the ones that have the most effect on the overall results.
Moreover, try to include the inputs and outputs of the code as well every bit their types or expected types. Here's the mindmap for the code in a higher place.
Edifice the mindmap will save you and then much time whenever y'all interact with this codebase. It will assist you lot know the connexion in example y'all want to add or remove any parts of the lawmaking.
Takeaway
Reading code written by other developers tin can be such a challenging task; you need to sympathise their logic, their style, and their specific choices. I have read then many codes written by programmers of different levels and ages. Having experienced that, I came with my own 4 steps process to ease up reading, exploring, and agreement codes written by basically someone who is not me.
This four footstep process is simple and will salve you lot a lot of time and endeavour; all you need to do is:
- Run the code and explore the results.
- Notice the main function or the start signal of the code.
- Run the code nether the debugger and fully understand the code's mechanics.
- Build a mindmap of the connections between the different lawmaking elements and utilise it at any time you interact with the lawmaking.
I hope you lot find these steps useful in saving you much time and endeavor in your side by side code exploration take a chance.
"Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as role of the try to write new code. …[Therefore,] making it easy to read makes it easier to write."
― Robert C. Martin,
lingerfeltmosencestiss.blogspot.com
Source: https://towardsdatascience.com/the-most-efficient-way-to-read-code-written-by-someone-else-cb1a05102b76
0 Response to "Learn to Read Other Peoples' Code C++"
Post a Comment