views:

371

answers:

10

For the past 5 or so years, I've been working with vastly different projects and systems. Code for those projects differed significantly in terms of quality, style, size. I saw examples of both: clean as well as disgusting 1000 lines functions and if conditions code.

I still don't feel quite competent at reading other people's code, understanding systems' internals, etc.

I would like to find out what techniques you use to improve your skill at reading and understanding other people's code, how to get up to speed quickly, what tools, methods, etc commonly employed.

Let me give this answer that many will just gun for it: you can improve reading code skills by reading code

There is a relevant post for improving programming skills that can be useful and goes on par with this post: What is the single most effective thing you did to improve your programming skills?

+7  A: 

The thing that's helped me, more than anything else, has been refactoring.

Take a project (this can be a work project, personal project, open source project, whatever), and start refactoring.

Over time, the more you refactor other people's code, the better you'll "get into their head" and understand their way of thinking, whether good or bad.

Reed Copsey
A: 

The problem is people must write clean and good code, so any programmer would read it.

x2
I don't agree. Writing good, clean code is valuable - but it's also incredibly valuable to understand how to wade through crappy, horrible code, and more importantly, fix it. Dealing with crap code is part of programming professionally - no matter how good YOUR code is, eventually, you'll have to work with code that's horrible.
Reed Copsey
unfortunately we don't live in a perfect world :-)
vehomzzz
Just like in defensive programming, you should always aim at the lowest common denominator. Learn to read crap code. If you can read that then reading the good stuff becomes a joy.
EBGreen
@EBGreen I read a lot of crappy code -- that helps, but also teaches me to write crappy one in process...
vehomzzz
If you know it is crappy (since you say that you read a lot of crappy code) then it is a choice on your part to write more crappy code.
EBGreen
+1  A: 

"you can improve reading code skills by reading code"

Correct.

What code to read?

Any open source project that appeals to you. Most open source coding is remarkably good.

Don't read random code though. Read stuff you actually care about. The more you understand the problem domain, the better you can appreciate the solution you're reading.

S.Lott
+6  A: 

i personally believe in the

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." -Martin Fowler

so my approach would be to find that person and blow his head off so s/he doesn't spead this virus.

venksster
+1  A: 

Other than simple experience leading to reading code better, I suggest attaching a debugger to the application, add some break points, and analyze data as it goes through the system. That way you KNOW what is going on, instead of presuming (by logic or any other means) what is going on.

Jay
+2  A: 

The things that's helped me, more than anything else, has been testing and refactoring. You cannot say: "I understand this code" until you know how to break it with a test.

dfa
A: 

I agree with reading open source coding. There is a book by O'reilly on Beautiful Code - but it sucks in my opinion. I would suggest books that I have read to prepare for programming interviews, and that are often suggested (all the links that I have put up are Google Books links:

[Programming Pearls] is a good one (get the newer edition, though the concepts are the same), [The Practice of Programming] is another classic.

These are pretty practical books with real code world examples

Also, practice writing test code for already existing projects. This tends to help you out by causing you to be forced to understand exactly what the function does. If the results aren't what you expected, you are forced to think about why the person's code doesn't work as you expected for those tests. Browsing around StackOverflow and seeing what people with high reputation have to say always helps too.

nicoslepicos
+3  A: 

Don't just read open-source code, join one. This way you will can ask "why was it written this way" vs. just simply reading it. More often than not, there was an underlying reason for doing something abnormal, beit authentic or not.

Also, if you are actively involved, you will be able to see the progression of the codebase. You'll become familiar with the style of development from different people. This may provide different avenues for your brain to walk through when you encounter a non-affiliated project that you need to read through.

Chance
+1  A: 

You learn to swim by swimming, you learn to read by reading. First try reading small code snippets written by others on your favorite area.

I started doing so by reading Python codes at code.activestate.com on my favorite topics(math,algorithms etc).

Advantages:

  • Python codes are cleaner so you don't get frustrated in the first attempt.
  • Starting with small manageable things help (a lot).
  • Using tools like FREEMIND to organize your thoughts also helps.
  • Then you can move on to more complex and dirty codes.
TheMachineCharmer
+1  A: 

2 things in particular

Refactor When you see code that murkys up the waters of your IDE, refactor it so it makes sense so it's easy to ready

Comment Use refactoring to compartmentalize 'how' but use your commenting to show why.

Just like Steve McConnell says in Code Complete 2, follow the Psuedocode Programming Process (in this case, after the code is written). It will help you and any future developers read the code.

asp316