Chairperson and Program Coordinator for the Computer Science Technology Program at Dawson College Instructor and Program Consultant for the School of Extended Learning Computer Institute at Concordia University I have been passionate about programming since buying an Apple][+ in 1980. I paid the extra $450 to bring the RAM up to 48K! Ken has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

NetBeans in the Classroom: Code Templates

04.30.2014
| 2184 views |
  • submit to reddit
Ken Fogel is the Program Coordinator and Chairperson of the Computer Science Technology program at Dawson College in Montreal, Canada. He is also a Program Consultant to and part-time instructor in the Computer Institute of Concordia University's School of Extended Learning. He blogs at omniprogrammer.com and tweets @omniprof.

I have had to reveal an ugly truth about being a programmer to my students. The truth is that all the errors that occur in their code are their own fault. It’s not Java’s fault, not Oracle’s fault, and not even Microsoft’s fault. It’s their fault. Or to put it another way, they are responsible for the bugs in their code.

Ants at the Montreal Insectarium, photographed by Ken Fogel

Rather than teach them about the debugger and breakpoints, I present a simple technique for debugging: use System.out.println().

For someone just starting out, there is no better way to learn about what is going on inside your machine. In my second Java course, I introduce the Logger but, for finding problems in their first assignment in their first programming course, nothing beats System.out.println().

Code Templates in NetBeans

One of the best features of NetBeans is the Code Templates. A template allows you to enter an abbreviated form of a statement or expression, followed by the Tab key, and then NetBeans writes it out for you.

You can find the list of supplied templates by going to Tools->Options->Editor->Code Templates. You can edit any of the templates or create new ones. For the beginning programmer, the favourite template is "sout".



Place your cursor at the point in the code where you want to display the value of a variable, type the letters s o u t and then press Tab. System.out.println(“”); appears on the line and all you need to do is fill in what you want to examine.

A last point that I make to my students is to comment out the statement when they are done with it rather than delete it. I explain that there is a fundamental law of programming that states that five minutes after you delete a line of code you wish you hadn’t!

Published at DZone with permission of its author, Ken Fogel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Anton Arhipov replied on Fri, 2014/05/02 - 1:32am

"fundamental law of programming that states that five minutes after you delete a line of code you wish you hadn’t!"

Local history feature might be very helpful for such cases. The approach for just commenting out the code has a fundamental flaw - you're left with a ton of commented out code that you "might" want to use later, which is just silly.

Ken Fogel replied on Fri, 2014/05/02 - 7:24am in response to: Anton Arhipov

You are correct that for you and I leaving comments in our code might result in their being more comments than code. For someone new to programming leaving commented code can be useful as they experiment with different ways to get the code to work. Once experience kicks in then they should be using git or SVN and just look at previous versions of their code. My second level Java course introduces repositories. Thanks for the comment.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.