Would appreciate tips on reading someone else's code and understanding it .
Asked by
sumitnxt (
108)
November 18th, 2009
( For programmers )
i need to understand someone’s code.
would appreciate tips on reading someone else’s code and understanding it .
Observing members:
0
Composing members:
0
20 Answers
First hint: if there are no coments smack the “someone else” in the face with a crowbar.
That said there’s really not much to say, you should be able to figure out what the code says as long as you have an understanding of the language he’s using, i suggest you work from the inside out, trying to understand what the subroutines are for and then from that wrking out why the bigger parts need them.
There are only tips to give the programmer to make his code more readable for other people. The only tip I have for you is: left to right, one word at a time.
If you can’t read it, than you are not the only one. It means the programmer did a very bad job and need to rearrange his code that every programmer out there can understand what he wrote. I like code that is written well and orgnized.
If you can get a copy of the code file for your own use, there are ways of reorganizing for the purpose of getting a better understanding. One major obstacle to understanding is subroutines that are too long, taking up several pages of code. One way of simplifying this is to break the long subroutine into several smaller ones. I am not saying to make permanent changes in the code. Just do this for your own use.
If the code does not have comments, make sure to add them. You may request to be allowed to add comments to the original code. It is unlikely that anyone is going to object.
One thing that really ticks me off is long and highly nested if statements. I see this all the time. The first thing I do in these cases is to use comments to mark the nesting level at the start and end of each if statement. This can be skipped if the if statement is really short. I find this really helpful in getting a handle on the code. Once you can isolate a particular if statement then you can consider transferring it to a separate subroutine.
Also look for obvious ways of simplifying the code. You must of course be careful here. One thing that I see a lot is statements like the following:
if x= true then
y=true
else
y= false
The obvious simplification here is to replace this monster with y=x
Good luck!
@LostInParadise So basically you’re doing the programmer’s work :) I understand only in a perfect world all code is well documented and understandable, but the changes you made in the code (to understand it) might as well stay permanent (with the programmers consent).
All depends on what kind of code your talking about, you need to be more specific, there are way’s to target certain codes and strings in different languages..
The really good programmers will have documentation within the code itself.
$SOUND 4096 ’ set the sound buffer to the maximum size
DEFINT A-Z ’ declare all variables integer type
READ Frequency,Duration ’ read first Frequency and Duration pair
WHILE Frequency <> -1 ’ keep playing notes until the end of song
DELAY .05 ’ general delay for music clarity
IF Frequency = 0 THEN
’
’ If Frequency is 0 then pause for the Duration
’ value (in hundredths of a second)
’
SELECT CASE Duration
@DrBill That doesn’t make a coder good, that just makes them neat and organized. Some of the best coders i know are sloppy..
@markyy There is always a danger in modifying code, because, even if it is convoluted and hard to read, it works, and you don’t want to risk introducing bugs. At some point it makes sense to refactor the code, but that has to be a very deliberate project supported by regression testing.
@OutOfTheBlue
Just because it works, does not necessary make it good. Good programmers write where others can follow it.
@DrBill I disagree, if a program does what you tell it to with out any errors then it’s perfect. As far as other people following code, most coders don’t want you to “Follow there code”.....
Some people are just dicks.
@OutOfTheBlue That’s simply false. No program in the history of programs has lived in isolation. There will ALWAYS be a need to go back into the program, modify it, investigate it, add to it, etc. For that reason, a program that’s technically correct at the moment its written but impossible to read is NOT a good program. Far from perfect.
The only time there’s not a need to modify an existing program is in academia. In the real world, programs are living organisms.
I agree with the above.
Also, a good editor can help. For example, I like features like code folding, color coding, etc.
I’ve charged a couple of different clients to clean up code from past shitty coders. It was annoying to have to do, but I told them that it was the only way I, or anyone else would work with it. There’s no excuse for not writing clean code.
@forestGeek Agreed. Anyone who doesn’t recognize that a code’s quality is not solely dependent on functionality has never worked in the real world, with real co-workers and real clients.
One more thing in addition to what I already said. Follow the data. This is especially true of database applications. If you know what type of information comes in and what goes out you have an important handle on what is going on, even if you do not yet know how the data is used.
@MrItty Again i disagree, there are TONS of programs that do NOT need to be fixed or updated and there are TONS of programs where the programmer simply does not want you going in and doing anything PERIOD! I been coding for almost 10 years in several different languages, sometimes it’s best to leave shit alone or have the programmer do it them self.
I never said a sloppy coded program or website was right, but it damn sure isn’t hurting anything unless it’s for a client, but even then, most programmers code is coded differently so don’t expect it all to the look the same, some people understand it better than others so they may be working short cuts or work arounds, there are to many variables to list..
THE ONLY TIME YOU SHOULD BE GOING IN SOMEONES ELSE’S PROGRAM IS IF YOU ARE ASKED TO BY THE PROGRAMMER!!! Or unless the programmer was fired, then just make a new app or program yourself instead of using someone else’s work NUFF SAID!!!!!!!!
@OutOfTheBlue You’ve been coding for “almost 10 years”, where exactly? Certainly not in any professional environment, in a company of more than 10 people.
When you have a company of thousands of people, co-workers get fired, promoted, reassigned, etc. Having the programmer who wrote the code 5 years ago is frequently not an option. Writing an all new application is idiotic if all that is required is a 3 line fix. Telling your boss that you have to spend 3 weeks to write a program that does 99% of what the existing program does because you can’t understand the original program is going to get you fired real quick.
@MrItty Your right, your always right, you will always be right.
Answer this question