Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting your head around other people's code

I'm occasionally unfortunate enough to have to make alterations to very old, poorly not documented and poorly not designed code.

It often takes a long time to make a simple change because there is not much structure to the existing code and I really have to read a lot of code before I have a feel for where things would be.

What I think would help a lot in cases like this is a tool that would allow one to visualise an overview of the code, and then maybe even drill down for more detail. I suspect such a tool would be very hard to get right, given that is trying to find structure where there is little or none.

I guess this is not really a question, but rather a musing. I should make it into a question - What do others do to assist in getting their head around other peoples code, the good and the bad?

like image 871
Joel Avatar asked Jan 26 '09 11:01

Joel


People also ask

Is it okay to look at other peoples code?

There's nothing wrong with reviewing other's code as long as you're not copying it with the intent on passing it off as your work or if you use their code, give them credit. Any programmer that says they learned to program without looking at other people's code is lying.

How do I get my head around programming?

Use Your Intuition This technique simply requires looking at the code and browsing until you figure it out. Don't jot down any notes. Just wrap your head around the issue. It is basically the “dig around and find out what's going on” approach.

How do you read a code written by someone else?

This 4 step process is simple and will save you a lot of time and effort; all you need to do is: Run the code and explore the results. Find the main function or the start point of the code. Run the code under the debugger and fully understand the code's mechanics.


1 Answers

Hmm, this is a hard one, so much to say so little time ...

1) If you can run the code it makes life soooo much easier, breakpoints (especially conditional) break points are you friend.

2) A purists' approach would be to write a few unit tests, for known functionality, then refactor to improve code and understanding, then re-test. If things break, then create more unit tests - repeat until bored/old/moved to new project

3) ReSharper is good at showing where things are being used, what's calling a method for instance, it's static but a good start, and it helps with refactoring.

4) Many .net events are coded as public, and events can be a pain to debug at the best of times. Recode them to be private and use a property with add/remove. You can then use break point to see what is listening on an event.

BTW - I'm playing in the .Net space, and would love a tool to help do this kind of stuff, like Joel does anyone out there know of a good dynamic code reviewing tool?

like image 133
MrTelly Avatar answered Sep 18 '22 13:09

MrTelly