Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I recently downloaded some large code bases and have had light reads through them, but what can I gain from this?

How can I be sure what the author is doing is the right way to go about things?

(One of the the code bases was the Zeus Trojan source code ;D)

share|improve this question
    
Usually, an inflated sense of self worth. –  quant_dev Jul 4 '11 at 21:06
    
You can never be sure that someone is doing the "right" thing. But you can pick up some good practises which will gain in value once you understand their reason of being. Even better, see if you can follow the train of thought of someone you consider as being more capable and develop your own "common sense" in terms of programming. –  James Poulson Aug 19 '11 at 19:04
    
Best guess is you will learn no to do anything like 80% of the code you read... –  Rig Jul 17 '12 at 6:21
add comment

4 Answers

up vote 5 down vote accepted

(Answer assumes that you've picked code bases from a couple of projects that are relatively successful and well used).

If you mean the best technical solution then you can't but there are lots of definitions of right.

If this is working code from a popular application then that in itself is a reasonable definition of right, and that's one big part of what you can learn - you can see how people have solved problems in the real world and trying to work out the compromises and trade-offs they've made (and perhaps why they've made them).

The other thing is that you can see how people have solved particular problems you might be interested in so pick a piece of functionality you're curious about, or one that's relevant to the sort of programming you do or want to do, and start digging through it.

But I'd definitely try and focus what you're doing. Skim reading code will teach you very little. Pick an area you want to understand, or find a bug or change you'd like to make and focus on that. Just reading code is a passive activity and for most people that's a fairly ineffective way of learning.

share|improve this answer
1  
+1, especially the last paragraph –  StuperUser Jul 4 '11 at 10:37
add comment

It depends what aspect of the program you are looking at e.g. code, architecture, tests?

It will be better to read a good book on good practices for the area you are looking at beforehand (Code Complete 2nd Edition is fantastic all-round if you're just starting) so you'll know why code that looks good is good.

Otherwise, as you say, you won't know whether the code as an example is "correct", and may not understand the intentions and reasons of particular aspects of the code without context of higher level concepts.

share|improve this answer
add comment

doing it the right way - A programmer is doing it the right way if his code fulfills all requirements. Which will be proven through tests.

So, on the other hand, if you don't know, what the code should do and which programming guidelines (styleguide) had to be fulfilled and if you don't have test cases and test data at hand, then you won't be able to determine, if it was done the right way.

Even if you find some nice looking algorithm, you should know, what it was supposed to do and you should know if it passes all tests before you add it to your own repository.

share|improve this answer
    
"A programmer is doing it the right way if his code fulfills all requirements. Which will be proven through tests": some requirements cannot be tested easily. How for example can you test if the code is readable, or does not violate DRY principle, or does not have premature optimization? –  MainMa Jul 4 '11 at 9:39
    
@MainMa - That's difficult, indeed, but you'll have to do it in order to validate if it has been done the right way. Or skip that validation and be happy with kinda like it, yes/no. And that's the main message: you'll never know if things have been done right by just staring on some unknown code. Never. –  Andreas_D Jul 4 '11 at 9:45
    
Runtime is not always part of the spec, but should be considered when writing code –  inspectorG4dget Jul 4 '11 at 10:04
add comment

What can I gain from examining others code

Well to start off i would say code is written once read many. The first thing you would ever realize is can i understand what is being done here or what the process is. Not to often do you find code that is readable and have rational comments which indeed are unto date with the code.

To gain on you will need to benchmark the code and if needed try your own version of some part of the code, see how you fair of in terms of readability and later on for performance. At times you may put up an better algorithm also and also likely if understood fully you could use the patterns used in the code.

In short it will always expand your knowledge base and fuel your passion to program/code and above all Read code which is a difficult skill

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.