35

It has taken me a long time to get to a point where my students regularly indent properly. I basically use a 4-part strategy. (My work here in the context of AP Computer Science, so my examples are all in Java) First, right from the beginning, and repeatedly, I talk about the two audiences for code: the computer and people. These two audiences have ...


21

Our two deities are clarity and efficiency. Standardization makes a great deal of sense in industry, where large numbers of programmers may work with the same lines of code and must be able to make sense of each others' choices. However, I don't see value in enforcing particular style guides in an educational setting. Different companies can require ...


20

I don't believe that your question is entirely valid; some languages require jumping. The first principle, therefore, is to follow the norms of your language. However, I suspect that you are asking about languages that discourage (but do not ban) jumping, such as Java, or C++. In these cases, I agree with Peter that the solution is to give them the ...


18

I think there are two phases here: the first is just being aware of what the correct idioms and best practices are, and second is attempting to apply what you learned and getting feedback. For the first phase, you can get a lot of mileage by doing research and casting a wide net. In particular, I personally find the following Google queries to be pretty ...


16

I think placing some emphasis on style and conventions is important, yes -- learning how to follow style guides is a skill worth training and learning to be detail-oriented is an important meta-skill. That said, I think it's fine to introduce some leniency by allowing students to use whatever convention they want as long as they're consistent, especially ...


15

Indentation (in most languages) is just for readability. This aids in maintainability and other good stuff, all of which an intro student is completely unaware of or uninterested in. So it is best taught by making the student read code they have not written. (e.g. what does this code do?, etc.) Show them some sufficiently complex, badly formatted ...


11

you not going to like this answer, but Don't teach it!! At least, don't teach it unless your teaching a 300-400 level course in college. Most code optimization is very tiny benefit, especially since modern compilers are able to do many of the optimizations for you and it's entirely possible for someone to optimize wrongly an slow something down instead. ...


11

I think an important aspect to any best-practices list is the rationale behind it. It is entirely too common for a programmer to, for example, insist that gotos and global variables are evil and then proceed to use exceptions and singletons to create the exact same problems that got those features proscribed in the first place. So I suggest that when ...


10

I've faced resistance too. Here are some ideas: Students would not turn in an assignment to an English teacher that was not properly punctuated and proofread, even if that teacher will be the only reader. They should not turn in an assignment to a CS teacher that is not properly formatted, documented, and tested. Companies want to hire programmers capable ...


9

Indentation is important so that you can see what code will be run based on the existing conditions. So for a simple example, consider: for (x=0; x<100; x++) { if (item % 2 == 0) { print x; print " is even"; } else { print x; print " is odd"; } } Looking at this code, without reading it I can see from the ...


9

In my experience, immediate feedback is helpful in speeding along development, but rarely enhances understanding. In fact, I am often frustrated with the immense level of help that is provided to my strugglers, because they wind up moving words around until whatever they've written stops being underlined. The only exception to this has been for norms that ...


9

There are a ton of blog posts and several books on the topic of readable code, containing far more eloquent and complete arguments than one can make here (or that I could come up with myself). Depending on their level it's well worth sharing some extracts of well-written texts directly to show the importance of understandable code. The most fundamental ...


7

I am a strong advocate for code interviews. I make principles of style into about 50% of a lab grade (though I do permit kids to go back and refactor after an interview for full credit.) For some context, I discuss regularly that there are two audiences of code: computers, and people, and that these audiences have very different needs. Since my high ...


7

Lots of good comments here. Showing bad code and asking them to understand it will particularly drive the point home. I've taken to having coding standards in my courses. No one followed them until there were points attached, of course. Initially, it was protect my eyesight, but it helps the students write clearer code. I also have a couple exercises ...


7

I tell my students this. Pick a style and be consistent. You will see religious wars over for(...) { code } and for(...){ code } Both are FINE. Pick one and be consistent. I find the first easier to use because it makes finding curly brace errors butt simple. But some prefer the other and that is AOK with me.


7

There is more to your question than just "how to program". For that, however, a good guide is to learn who/where a language was developed and emulate the code that those people write. For Scala, for example, get Programming In Scala by Odersky et al, who created the language. Textbooks are not in general a good guide in general. Many do a terrible job, using ...


7

This might be controversial, but I would make a point to explain that goto is not always considered harmful (and explain that the context of Dijkstra's "Go To Statement Considered Harmful" was about using available control structures). In C, there aren't very good control structures for releasing resources; in the absence of them, goto works well, and ...


7

Too much, too fast "First semester". If I parse correctly, you say 60% students are new to programming. You say "The following points knock them out: they dont know how to test, they dont know how to check for memory leaks, they have no concept of capsulating". And you say "group assignments". Taken together, it looks like this course is "introductory" ...


6

Best Practices serves two purposes, allowing future maintainers (which easily could be themselves) to understand what the code is supposed to do, and helping the them avoid errors, and find errors while writing the code. Not all parts of best practices serve both purposes. I think the avenue to take is focus on reduction of errors first. That targets their ...


6

Make them suffer through debugging spaghetti code. Come up with a few examples that make liberal use of the language features you don't want them to use. 100-150 lines of code with 1 or 2 small bugs because of these statements will be a nightmare for them to sort through. Make it a sort of game, too. Have students compete in groups to see who can debug the ...


6

Your list is a bit narrow in one sense. I assume it is well matched to your specific course, but probably doesn't represent "best practice" in general. For example, valgrind is limited to linux, which suits you better than me. But the idea of including memory testing, for example, is a good idea no matter the specific tool. Similarly for git. There are ...


6

Lint I would not program in C without a lint tool e.g. gcc -Wall or pclint/flex-lint, unfortunately the latter two are proprietary. However you need to show that error/warning messages are your friend. There are not an accident. Someone spent time writing them, to help you. Read them, and fix the underlying problem. Often I have seen people finding ways ...


5

Maybe the easiest way to practice is to have an element of peer review. I find it's much easier as a reviewer to insist on good practice and make 'helpful' suggestions than to follow these rules myself. You might need to be careful to balance the level of reviewer and reviewee though. Justifying the time spent is probably harder. Some of these might work: ...


5

You can introduce the idea of best practices. Show them examples of right vs wrong for each of the practices you want to teach them: public void method(){ System.out.println("this is incorrect indentation"); String example="It makes the code difficult to read.\n"; example+="If you can't read the code, you'll see how difficult it is to work on it"; int x=5; ...


5

The purpose of indentation (to express the intended control flow) and the value are subtly different things. For a 5 line script, there is really not much value in indentation or comments - the code is a throw-away. The real value in cleanly presented code (of which indentation is only a part) comes when a person is reading the code some time after it was ...


5

First let me say that I think it is a good idea to automate as much as possible, and that there are tools that will help this. The mystery test However my experience with professionals is that there will always be a few that try to defeat the checker, not maliciously (they just don't get it; they are minimising error-messages, not minimising errors). So ...


5

Harvard's grading policy for CS50 is worth looking into. There are four components for the grade on problem sets (each of which involves submitting code). The overall grade is calculated as scope * (3 * correctness + 2 * design + 1 * style) Scope: to what extent does the code implement the features required by the specification? Correctness: to what ...


5

A quick exercise Write the most garbled code of your life (I know, I feel your pain) - terrible variable/function names, no indentation, code-golfed lines here and there, weird tricks, no comments, and so on. Insert a small, barely-noticeable-in-good-code error. Ask your students to find the error. (If you wish, tell them that this exercise is graded as ...


5

We use the Eclipse plugin Checkstyle. The plugin is configured with a set of rules (defaults styles are available with the plugin), and then generates a set of warnings for any violations of these rules. Toward the end of our CS1, we introduce students to the tool (the students have been taught our expectations throughout the semester). After they are ...


Only top voted, non community-wiki answers of a minimum length are eligible