![]() |
| | #1 |
| Banned Join Date: Jun 2005
Posts: 594
| Writing Code Anyways the point of this post is when you are writing code i know it is good practice to indent and make your code as readable as possible and to also comment your code, what i would like to know is the following : 1. Is it best to define variables you will be using in your code at the begining of the code, or slightly before there use, or some other variation? 2. When declaring functions is it best to have a prototype at the begining of your code and the actual function at the bottom, or just write the function at the begining of the code? 3. When writing a class, is it required / good practice, to include a destructor? |
| ILoveVectors is offline | |
| | #2 | |||
| Software Developer Join Date: Feb 2003 Location: University of Waterloo
Posts: 1,916
| Quote:
Quote:
However, it does make code a lot cleaner to do it one way or the other (and not both). ie: If someone else (or maybe even you) is looking at your code, it will be much easier if the main function is easy to find. Personally, when I am trying to code something really fast, I don't bother with prototypes, but if I'm trying to make my code look much more professional, I prototype all of my functions so that main is at the beginning. Quote:
Yes in the case that you *must* have a deconstructor to avoid memory leaks in the case that you are handling dynamic memory. No in the case that if you have no dynamic memory, it's not really necessary to have a destructor and is rather pointless IMHO. | |||
| jverkoey is offline | |
| | #3 | |||
| Registered User Join Date: Apr 2003
Posts: 2,662
| Quote:
Quote:
Quote:
| |||
| 7stud is offline | |
| | #4 |
| Banned Join Date: Jun 2005
Posts: 594
| Ok thank you for your replies, very good information. |
| ILoveVectors is offline | |
| | #5 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| In C++, you generally wait to declare and initialize variables until you actually need them. In general, don't write code you don't need to, so writing an empty destructor for no reason is unnecessary. If your class looks like it might need an explicit destructor, but doesn't, then a comment to that effect would be appropriate. |
| Daved is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Writing Code but am having problems | Choppers | C Programming | 20 | 06-25-2009 06:18 PM |
| Writing code for a program in C | Sure | C Programming | 7 | 06-11-2005 01:33 PM |
| professional programmers, do you spend more time writing or modifying code | Terrance | A Brief History of Cprogramming.com | 29 | 11-25-2002 10:54 PM |
| Interface Question | smog890 | C Programming | 11 | 06-03-2002 05:06 PM |