C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2005, 08:01 PM   #1
Banned
 
Join Date: Jun 2005
Posts: 594
Writing Code

Hello im new to the board, i have been programming in c++ for some time now, at least from what i have taught myself.

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   Reply With Quote
Old 06-12-2005, 08:36 PM   #2
Software Developer
 
jverkoey's Avatar
 
Join Date: Feb 2003
Location: University of Waterloo
Posts: 1,916
Quote:
Originally Posted by ILoveVectors
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?
This is completely up to you. In most situations it would be best to declare your variables at the beginning. However, it could also be more convenient for you, as a programmer, to declare the variables as you need them so you know which variables are needed when.

Quote:
Originally Posted by ILoveVectors
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?
This is up to the programmer. Most of the time (and especially once your projects start getting bigger) you will be putting prototypes in header files and the implementations in separate source files.
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:
Originally Posted by ILoveVectors
3. When writing a class, is it required / good practice, to include a destructor?
Yes and no.
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   Reply With Quote
Old 06-12-2005, 08:51 PM   #3
Registered User
 
Join Date: Apr 2003
Posts: 2,662
Quote:
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?
I think you should define the variable as close to the spot the variable is used as possible. That way you don't have to go searching for the variable when you are reading the code.

Quote:
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?
Neither. The best way is to have a function declaration in a .h file and a function definition in a .cpp file separate from main. The idea is that you can reuse your functions in other programs by inserting them into your project and including the .h file in the file containing main().
Quote:
3. When writing a class, is it required / good practice, to include a destructor?
The compiler provides you with a default destructor, so unless you need to do more, don't define one--unless you really enjoy typing.
7stud is offline   Reply With Quote
Old 06-12-2005, 08:57 PM   #4
Banned
 
Join Date: Jun 2005
Posts: 594
Ok thank you for your replies, very good information.
ILoveVectors is offline   Reply With Quote
Old 06-13-2005, 12:27 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22