Reading the Source Code of a Particular Program
Hey friends! In this post, I'll share some thoughts I take concerning the conscious reading and study of source lawmaking. In my opinion, reading code is a process which can help you become a more skilful software developer. It has undoubtedly improved and influenced my software development skills.
Should I Read Source Code?
Yep, you should! The good news is, you're already doing this, day in day out when developing software. I've seen diverse statistics over the years showing that a high per centum of our time as developers is spent reading code, rather than writing it.
When you lot offset on a new feature or on a problems fix, you take to begin past understanding how existing code works. Y'all may tackle this from the unit of measurement tests which should clearly draw the desired behaviour of existing lawmaking, merely often you will terminate up delving into the implementation code every bit well.
For this post, I consider this form of reading a given. What I'm recommending hither is that you make reading code a more conscious procedure and well-nigh chiefly read lawmaking that yous have never seen before. Reading code that you lot originally wrote or that y'all have worked on in the by is less likely to impact and inform your future coding skills than exposing yourself to previously unseen code.
Becoming better at reading and interpreting code volition aid you when yous tackle new features or bugs. Yous will be able to scan relevant lawmaking for the information you need to begin work more than quickly. Much as we tin railroad train ourselves to read books more quickly, nosotros can go quicker at reading and comprehending software code.
Why Spend Time Reading Source Code?
Let's think for a moment about how young children larn to read and write. In the book, "Young Learners" by Caroline Linse, she states that you lot need to read a give-and-take before y'all tin write it. This sounds pretty logical to me. By showtime reading words in context, you begin to understand their meaning and appropriate apply.
By reading books, you extend your vocabulary and pick up techniques which y'all can later apply to your writing. This is just one reason we study literature, through our determinative years in school. Ultimately, as we read more than, nosotros larn what works and what doesn't.
The same then must surely exist true for software lawmaking. I'm a C# developer, but whatever your programming language of choice, in that location are many rules to the syntax and keywords that you tin can use. A few unproblematic keywords and concepts can be combined into infinite possibilities for the code yous produce. At that place are many ways to achieve the same outcome, just as there are many ways of maxim or writing the aforementioned thing. While all may technically accomplish the same goal, some tend to be more than advisable and easier to comprehend than others. In code, some techniques may be more than efficient than others too.
Have y'all even come beyond code written by a co-worker, or fifty-fifty yourself, which is hard to read? I know I have! Hard to read code is hard to maintain and can impede the development procedure. Just as in linguistic communication, where a poorly constructed paragraph tin be confusing and difficult to absorb, lawmaking can suffer the same fate. Those tricky paragraphs or blocks of code crave multiple read-throughs to parse the meaning and intent. Past learning to read more code nosotros can besides better our writing skills to avoid producing unintelligible code.
Improving 1'south skills requires some deliberate attempt. Studying the piece of work of those who are more experienced than yourself will influence your own code in the time to come. Doctors, every bit just i example, go through years of grooming even in one case qualified to do medicine. They work aslope more experienced doctors, watching at first, before attempting procedures themselves. Great doctors will also report papers from those in their profession to learn nigh new techniques and approaches.
This is something I feel we practice less consistently in software development. Some teams may manage this amend than others. Pair or mob programming is but i fashion to share skills between developers. My theory hither is that another vital technique for improving your own skills is to regularly betrayal yourself to new code as office of a healthy continuous learning bike.
A peculiarly relevant quote which summarises what I'k trying to say in this department is from the bang-up author, Stephen Rex.
"Can I be blunt on this subject area? If you don't have time to read, you don't have the time (or the tools) to write. Simple equally that."
― Stephen Rex, On Writing: A Memoir of the Craft
What Source Code Should I Read?
If y'all're still with me, I promise yous're sold on the concept of reading lawmaking every bit part of your routine to help you continually meliorate every bit a software programmer. The next logical question and then is what code should yous be reading?
At a broad level, I don't think it matters. The sheer fact that you lot are reading code is the of import thing. Call up, though that all software code is non created equal. Some of the source code that you read volition be markedly better written than other code. That's perfectly fine. Both proficient and bad lawmaking can help y'all refine your own approaches.
You don't have to hold with the code for the reading of it to be beneficial. Reading "bad" code can be hugely influential and informative. If you read some code and dislike the style or you spot a functional flaw, that's fine. In doing so, you have identified a pattern or approach yous don't like, or that doesn't work. This can save you time when writing your own code every bit you'll hopefully avoid repeating the same mistake.
That said, you besides want to be reading code which adds new and more solid techniques or approaches to your coding vocabulary. I'll discuss some sources of code in the next section.
Reading code from multiple sources is crucial as you'll consciously (and subconsciously) learn patterns which echo across the various sources. When you meet the same techniques, patterns and code trends used in many places, it's more than likely a well-proven approach.
I focus my time on studying the internals of some of the C# libraries which I apply nigh often. Not just does this hateful I improve my skills by reading lawmaking in general, but also I acquire about the inner workings of those libraries which tin further inform my apply of them. Information technology's a double win.
Something I don't do frequently enough, but I intend to endeavor, is to read code written in languages that I rarely or never use 24-hour interval-to-day. Becoming extremely familiar with 1 software language is undoubtedly useful, but extending your range of languages volition likewise accept benefits. If you think about it, many of u.s. encounter the benefit of learning a 2d, and perhaps even third, spoken linguistic communication. This makes communication with people from other countries easier and volition often further add to our noesis and use of our own native language.
Non only volition 2nd programming languages extend your career options, but they will also make you appreciate the similarities and differences from your master language. We frequently see features in one language, influence another. C#, for example, has more than and more functional syntax evolved first in F# appearing in each release. Some .Net features, such as Channels were influenced by Go. We tin can gear up ourselves for these features by having one eye on that second language.
Where Can I Find Proficient Source Code?
There are many potential sources for code. To name a few…
GitHub
This is a vast resource of lawmaking written in practically every language. Where amend place to outset? My personal favourites which take influenced my C# code, are the ASP.NET Cadre and CoreFx libraries from Microsoft. I find diving into the high-quality and considered lawmaking from the framework teams to be a bang-up mode to learn.
Frameworks like these are excellent sources to review since the code has loftier standards and is itself informed past the coding patterns used at Microsoft. I take personally learned enormously from reading and learning how the team structure extensible framework lawmaking. Just recently, I spent some time thinking about the Job Parallel Library and discovering more than virtually the use of CancellationTokenSource in some lawmaking I was writing. By studying places where Microsoft utilize this type, I was able to appreciate conventional approaches which they use in their code.
GitHub has reasonable search functionality, and so without fifty-fifty downloading the code, you can navigate through a repository and learn direct in the browser. For deeper dives, I adopt to clone the repository so that I can navigate the code in more detail.
I also like to widen my coding vocabulary by looking into the source of libraries I utilise, such equally Polly and MediatR. This exposes me to other coding styles that further augment my appreciation of how code can exist written. I don't necessarily like or hold with the construction of all code I read, but even then I am more consciously recognising what my own code preferences are. When writing code, I am more deliberately able to dodge potential traps and poor design decisions.
Web log Posts
The cyberspace is stock total of splendid blog posts on software development. You are certain to notice many useful technical blogs which include sample code snippets. These are splendid equally they are often small chunks of code which may be easier to digest in a cursory sitting. Usually, the code is annotated or explained in the web log post and so you can verify your analysis of the code against the writer's intent.
Books
There are many dandy technical books bachelor in both e-volume and physical impress formats. These ii will comprise code and clear explanations of how/why it works. In the concluding year, I learned a slap-up deal about writing high-performance code by reading "Pro .Cyberspace Retention Management" by Konrad Kokosa.
StackOverFlow
You're sure to discover some not bad and some not so cracking code examples on StackOverflow. Here the question and answer format can be handy as you get to observe multiple opinions of different coding styles. To focus on code more than specifically, check out the Code Review area on StackExchange too.
Internal Source Control
Earlier, I said an essential part of deliberate learning through reading code was exposing yourself to code that you have never seen previously. I remember that is extremely of import, but I do want to add that you tin can acquire a lot well-nigh work codebases too. At that place are sure to be projects and repositories you've seldom worked with. By taking a trivial fourth dimension to understand them, you can go a feel for how those work. Should yous ever end up supporting them, yous'll take a real head start and be more than familiar with lawmaking than perhaps fifty-fifty the original authors.
Every bit a new member on a squad or when joining a new arrangement, you tin swiftly get upward to speed on the internal coding standards and common patterns by studying the existing code. This will expedite your onboarding and guide your personal development.
This is by no means an exhaustive list. Wherever you notice code, take a little time to read and appreciate it at a deeper level than you normally would. You won't regret investing the time to amend your software evolution skills in this way.
How Should I Read Source Code?
I'1000 certain everyone will have their own preferences inside this department. Ultimately do what feels right for you! I'll share some of my ain approaches here which you may utilise to kick-start your code reading adventures.
I typically similar to focus on lawmaking which either does something that I want to replicate the style of in the future, or which deepens my noesis of code I use daily. As I said earlier, this adds double the return for the time I invest.
I tend to starting time work in GitHub. This strips me of my usual tools and centres my attention on truly understanding what is happening in the code. I endeavour to find a high-level abstraction as a starting point and so gradually dig deeper into the internals. I like to make notes in OneNote as I report the lawmaking, describing the flow and how the code works. These are brusk-course notes. This process is less nearly having a reference, although that can be useful, than information technology is about embedding the knowledge into my memory. Past describing it in writing, I find that it sticks with me much longer.
Navigating code and forming a mental picture of the logical period is a handy skill. If you can larn this through reading code, it'll pay dividends when y'all're next trying to fix a critical bug under pressure level.
In one case I accept an overview formed the adjacent step is to get deeper. For this, I tend to clone the repository and so that I can bring information technology into an IDE or VS Code. There I tin utilize the code navigation features to move around more swiftly and really delve into the behaviour of the code. At this point, I'g trying to understand why code has been written as it is. I like to recall nigh how I'd have chosen to arroyo a problem and contrast information technology with the actual implementation. Is the author'southward approach improve or more efficient? Should I file it away for when I next face a similar requirement?
Often the code may employ APIs that I've never discovered when writing lawmaking. I notice that when I'm coding I will tend to apply those APIs which I'thousand more familiar with. These may not exist the best methods to phone call or classes to rely upon, but as I know no culling, I keep using them. After reading some code, if at that place are APIs I haven't used earlier, I study the description and method signature(due south) to understand their designed use. This extends my code vocabulary.
This second phase likewise improves my skills of working with tools such as my IDE. I notice tricks and shortcuts to salvage fourth dimension when navigating the code. Something I exercise oftentimes is to pull out a class into a window on my 2nd monitor and then I can follow code flows more speedily. This is something I now do oft when I'm writing code as well.
I'm also on the lookout man for language features or patterns I don't recognise. I quickly learn most the features of the latest version of C# by seeing them used in the wild, so to speak.
What if I Don't Understand the Lawmaking?
That's absolutely fine! At first, you won't understand everything you read, and that is, in fact, our goal here. You lot desire to expose yourself to new APIs or new syntax in the code which yous read so that you further your ain code vocabulary. Reading code you understand hands, much like reading a uncomplicated book, is non going to expand your writing skills.
Effort to interruption things down into smaller chunks to work through in your head. What is the code doing and also why has it been written that way? Has the author used an approach to solving something y'all had not previously considered? As you come against language syntax or keywords you don't sympathise, stop and search for the documentation. Armed with an actual utilize instance, often the documentation will brand sense in the context you are approaching it from.
Similarly, when y'all encounter framework APIs which you accept never used earlier, visit the code documentation if there is any. For the Microsoft libraries, all public methods include XML comments. You can either read these directly or visit the .NET API Browser to attain near a class or method.
The meaning thing about consciously reading code is that it'due south your own fourth dimension and that your are making an investment in your skills. Contrast this to when yous are in a codebase, up against a deadline, trying to finish a bug set up or feature. You lot volition rarely have the fourth dimension to extend across the boundaries of the code yous are directly working with. That state of affairs is much less conducive to learning.
What I Have Learned from Reading Source Code
I've learned and so much from studying lawmaking, which is why I'chiliad so enthusiastic about promoting this practice. Modern-twenty-four hour period craftspeople will often study the work of masters of their craft. By doing so, they promise to 1 twenty-four hours attain their level of skill. Developers too tin advance their skills by studying from the experts in their field.
For me personally, as a C#, .Internet developer, I detect a lot of value in studying the Microsoft source code for the .NET Core framework and ASP.Cyberspace Cadre. In that location are many keen developers at Microsoft who have comprehensive expertise of specific elements of the language or runtime.
I'1000 a cocky-taught programmer, or as I saw someone call it on Twitter recently (I tin can't remember who, unfortunately), a community-taught developer. I've learned C# through trial and mistake, in concert with consciously studying code. Just equally scholars study great works of literature, there are many secrets to exist unlocked by reading slap-up lawmaking.
One related resource I recommend for those who are intrigued past software design and want to better their appreciation of the .NET Core framework is available on YouTube. The .Net squad publish almost of their weekly .NET Design Reviews on YouTube. These are meetings where the team review proposals for API changes to .Cyberspace Core. While I can appreciate that the thought of watching a meeting by choice, in your own fourth dimension may be besides much geek for some, it'south a great way to learn. The team on these calls is made up of genuine experts who know the language, runtime and framework then greatly, information technology's quite staggering to observe. Past listening to them discuss the proposed APIs yous go a genuine appreciation for the considerations that accept to be made on such a wide and widely used framework. There are often little gems of insight I gather just by watching these streams.
Summary
My goal for this post was to inspire a few readers to consider some more than conscious study and reading of code. I honestly believe this has helped me to become a improve developer and to learn more about the C# linguistic communication and the .Internet (Core) Framework. Investing a few hours regularly will pay off for you in the time to come. You'll have more diverse opinions on coding style and techniques. Y'all'll empathise at a deeper level how the framework you rely on daily really works, which in plough tin can inform your use of it. You'll get faster at reading lawmaking, and as a event, those adjacent code changes you accept to make volition have you less time from the requirement to the completed code.
Thanks for reading!
Accept you enjoyed this post and found it useful? If so, delight consider supporting me:
Buy me a coffee
Source: https://www.stevejgordon.co.uk/become-a-better-developer-by-reading-source-code
0 Response to "Reading the Source Code of a Particular Program"
Enregistrer un commentaire