Step 70: Read Code~ Karianne Berg
You’ve got to be willing to read other people’s code, then write your own, then have other people review your code ~ Bill Gates
We love writing code, but when it comes to reading it we usually hate it. Partly, because reading the code is hard while some are really bad code.
One way to improve programming skills is to Read Great Programs.
Why Read Code?
Reading someone else’s code can improve our own coding. Everyone’s thinking and problem-solving skills are different. So, whenever you try to read code think why someone else did something particularly.
How to Read Code?
Most of us have to read lots of code that does not meet clean coding standards. Below are a few techniques that can help understand better for those huge, unstructured, maintained-by-dozens-of-people, internally-inconsistent, undocumented code bases.
- Build and Run the Program: It’s particularly useful to study the external behavior by just running the program. With powerful IDEs we can use the debugger to walk through the lines of code and see the program execution flow.
- Find the High-Level Logic: Start with the entry point to the program eg, main() in C/C++/Java or Activity() or AndroidManifest.xml in Android. These will give a higher level view of how the program initializes itself or what the components are being used in the application.
- Draw Some Flowcharts: We can’t keep all the information in the head, so drawing some flowcharts or state diagrams or program execution flow will help understand the code much better.
- Examine Library Calls: If the program uses external libraries, we can read the documentation of them. For example; Reading DI library like dagger code in Android is a nightmare if you don’t have any idea how that works. So, the documentation of that library is your friend.
- Search for Key Words: Use your editor’s find/search or grep will help usages or call of particular variables or method.
- Leverage the Power of Code Comprehension Tools: IDEs are tools that can help us with finding who calls these methods, who implements this interface, what are the superclasses, where are the arguments passed from etc.
- Print the Code: We can log some code, and can visually see them in logging output tools like LogCat in Intellij.
- Write UnitTests: If we start writing Unit Test we will understand more what the particular class does.
- Clean Up the Code: When you start cleaning the code, ie perhaps breaking a big class into smaller or big method into smaller, it will make us learn code much better and faster. Even simple stuff, as reformatting, commenting the code gives us a good insight into the code.
TL;DR Next time you feel the need to improve your programming skills, don’t read another book. Read code.
97 Journey Every Programmer should Accomplish
“A journey of a thousand miles begins with a single step.”
Go to 69th Step
Step 69: Put the Mouse Down and Step Away from the Keyboard~Burk Hufnagel
This is the 69th Step towards gaining the Programming Enlightenment series. If you didn’t learn the 68th Step, read it.
Go to the 71st Step.
Step 71: Read the Humanities~ Keith Braithwaite
This is the 71st Step towards gaining the Programming Enlightenment series. If you didn’t learn the 70th Step, read it.