5
\$\begingroup\$

I am searching for already written (intermediate level) Java programs, where I would like to read code written by other people and to analyze it to become a better programmer (maybe because I am too biased with my own way of programming).

Is there a place where such code exists, to review with explanations and nice comments?

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ i think rosettacode will be best place to look at. \$\endgroup\$ – Runcorn Jan 22 '15 at 7:53
12
\$\begingroup\$

As stated on our tour page,

Code Review is a question and answer site for seeking peer review of your code. It's built and run by you as part of the Stack Exchange network of Q&A sites. We're working together to improve the skills of programmers worldwide by taking working code and making it better.

I, for one, have found that participating on Code Review has been the most interesting and rewarding way to learn how to improve my programming skills. It's interesting, because there is a huge variety of code, more than you would likely find in any single tutorial website. It's rewarding because the benefits are mutual: you learn both when you ask and when you answer.

It's probably obvious that you can learn by reading Code Review answers when you post a question. It's not as obvious that posting answers is a learning experience too:

  • Experiencing the struggle to understand other programmers' code teaches you what not to do.
  • Coming up with a better solution than what was posted in the question is challenging.
  • Other users' votes and comments on your answer will let you know if your advice is on the right track.
  • Watching what happens after you post your answer is often instructive: will your answer be accepted, or will someone else come up with observations that you hadn't thought of?
  • Sometimes you encounter code in a question that is beyond your ability to critique. Then, you read what others have to say and learn from those remarks.
  • Sometimes you aren't sure if the answer you write is good, and that becomes inspiration to ask a question of your own.

Anyway, I encourage you to browse, lurk, ask, and answer on Code Review. I hope you have an enjoyable and educational experience.

| |
\$\endgroup\$
  • 5
    \$\begingroup\$ By the way, you don't have to be a programming expert to write a Code Review answer. Any concrete suggestion for improvement is welcome, if you can justify it. Here is a simple answer I wrote yesterday. \$\endgroup\$ – 200_success Jan 22 '15 at 11:38
2
\$\begingroup\$

That place would be this site.

| |
\$\endgroup\$
  • \$\begingroup\$ I guess the question got answered by migration. \$\endgroup\$ – Pimgd Jan 22 '15 at 8:21
2
\$\begingroup\$

Not entirely sure if you're actually asking the right question. Yes, there are sites with working example code, and there are sites and books with programming exercises. IMO, the latter (exercises) are going to teach you more in the long run. Of course, even if you solve an exercise and end up with a working program, that doesn't mean your code is "good", there might be better ways to tackle the problem, or there might be things you've overlooked.

That's where codereview comes in: Post the exercise/question and your solution, and ask us to review it. We'd be happy to do so (that's what this site is all about).

Looking for sites with ready-made code, and some explanation on how the code works is a good way of getting you started, but it's impossible to explain what choices were made during the writing of said code. Take these 2 (C) examples:

unsigned long fib_recursion(int n)
{
    if (n < 2)
        return 1;
    return (unsigned long) fib_recursion(n-1) + (unsigned long) fib_recursion(n+2);
}

//compared to:
unsigned long fib_non_recursive(int n)
{
    unsigned long tmp,
                  a = 3;
                  b = 1;
    for (int i = 0; i < n; ++i)
    {
        tmp = a;
        a = 3 * a - b;
        b = tmp;
    }
    return b;
}

Both seemingly produce the same result, but both work very differently indeed. Depending on your needs, you might want to use the recursive version (most of the time, you will). But then again, recursion can cause problems depending on what language you're using (certain scripting languages aren't too keen on recursion, and Java isn't known for having impeccable TCO).
So a good review would explain (briefly) why one might choose the iterative approach, rather than recursion, and why the types are what they are (unsigned vs signed, long rather than int) and what the risks are (unsigned + arithmetic can throw you off at times).

If you were to write a fully working example of this code, you should therefore include at least 2 options, and explain the differences, and explain what the differences are (which is better in terms of performance, which is more maintainable/reusable, which is safer,...). In most cases, these differences are hard to define. Performance-wise, it all depends on the language/compiler/runtime (JVM) and the data (the value of int n in this case).

The last thing any resource giving you full code examples, with documentation should always say, then is: "These are just a few of many possible solutions, which one you choose is all down to how, what and where you need this functionality. Always test the code you use, profile it, and perhaps look into your own solution". Which brings us back to square one: the best way to learn is to write your own solution/implementation (take inspiration from sites like rosettacode and previous code snippets posted on code-review), test it properly, and if you're still unsure about it, post it here. We'll be happy to take a look!

| |
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .