views:

157

answers:

4

I'm teaching myself Java, and it's (effectively) my first programming language. Sometimes when I complete an exercise I wonder if I've done things in the right way.

For example, perhaps I write a little program that successfully copies a file. Have I dealt appropriately with the things that can go wrong? Is my code unusual, or hard to read? Is there a more standard way to accomplish the goals of this program?

So I'm wondering if posting such code with a request for general feedback is appropriate. My hesitation is that it can lead to discussion, rather than a definitive answer, and it could seem to be undesirable clutter. Nevertheless, such questions (unlike the meta-question that I'm asking now) would be programming questions, and there are many objective answers that could be offered.

So let me know. Is this good use of SO? Or acceptable as community wiki? Or should I find a friend to review my code, and use SO for clearly defined, objective questions?

+3  A: 

Don't ask for general comments - identify a specific bit of the code you are concerned about and ask a specific question about that bit of code.

anon
+1  A: 

It's fine to ask homework questions here. Generally it's best to tag them with the "homework" tag if for no other reason than it tells potential answerers that you (probably) are learning and that we should probably give you advice and points to consider rather than definite answers.

It's also fine to post code as long as you're asking a relatively specific question and so long as the code isn't relatively brief (reduce it to a meaningful snippet).

cletus
+3  A: 

I think that every budding programmer faces this question sometime... "Am I writing good code?" The major problem with finding an answer to that is that the answer can be straightforward or sometimes it may be as complex as being able to decide between multiple opinions offered (as you yourself mentioned).

The way I answered this question for myself was to do my research... search the web for similar topics and study the code posted by people on blogs, articles, forum discussions and then make my own decision as where my code stands in comparison. It helps a lot that there is a wealth of technical information already available on almost every conceivable topic, ranging from simple tasks that most new programmers face to complex tasks. This research also helped me learn that there are about 10 different ways of accomplishing any programming objective and each of those ways is best suited for a particular scenario (one may be most performant, another most secure, and one just plain deprecated).

I would discourage asking every "homework" question on this or any other forum, whenever you are faced with doubts. If you must, then ask after you have done your research so that you are already better equipped to choose the best answer.

Cerebrus
A: 

It's OK to write bad code while you're still learning. I don't think most people can learn the basics at the same time as learning about efficiency, code clarity, and the dozens of other things professional programmers should consider.

Community review of your code could be a valuable teaching tool, but in the first few years learning how to figure stuff out for yourself may be even more valuable.

If you focus on writing code in a professional style you won't be focusing your energy on learning the fundamentals.

RossFabricant