Style guidelines for programming in JAVACommenting Any good programmer makes sure that her/his code is well-commented. Every file should have a comment at the top indicating the author and contents of the file (and possibly other things, such as the date last modified). It is not necessary to comment every function, but functions which are quite complex or important should be commented. Ask yourself, if I had just been up all night and was looking at this code for the first time, would I understand it?? Variable and method names Variables and functions should have names indicating their purpose, contents or result. There are a few "traditions" (for instance, i and j are variable names often used for counters), but in general, variables should have names other than "xx" and "y12". The idea is to make your code easy to read, modify and understand. Non-related classes should be put in different files. This makes code more modular, easy to move from one application to another. Indentation Be consistent with your indentation. Indent when you open a new scope (inside functions, ifs, whiles, fors....) You can set up your Emacs to indent the amount and ways that suit you. Other items It is not generally good programming practice to use global variables. If you find yourself using a constant frequently, consider making a final declaration for it. There are two ways of dealing with data in classes. One is to make the data private, and define "accessor" (reading and writing) functions for each piece of data as necessary. The other is to make the data public. The way you handle it will vary depending on the type of the data. In general, it is a good idea to keep data private, but it may be easier to make it public. Finally, Design before you write. This means thinking before sitting down at the computer, writing class declarations before writing the member functions, and writing and testing one function at a time. These simple things will make programming in any language much easier.
|