Writing Code

This is a discussion on Writing Code within the C++ Programming forums, part of the General Programming Boards category; Hello im new to the board, i have been programming in c++ for some time now, at least from what ...

  1. #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?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

    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().
    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.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Ok thank you for your replies, very good information.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code but am having problems
    By Choppers in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 06:18 PM
  2. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  3. professional programmers, do you spend more time writing or modifying code
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 11-25-2002, 09:54 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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