What C++ coding standard do you use?

Posted by gablin on Programmers See other posts from Programmers or by gablin
Published on 2010-09-16T23:16:34Z Indexed on 2011/02/17 15:33 UTC
Read the original article Hit count: 384

Filed under:
|
|

For some time now, I've been unable to settle on a coding standard and use it concistently between projects. When starting a new project, I tend to change some things around (add a space there, remove a space there, add a line break there, an extra indent there, change naming conventions, etc.).

So I figured that I might provide a piece of sample code, in C++, and ask you to rewrite it to fit your standard of coding. Inspiration is always good, I say. ^^ So here goes:

#ifndef _DERIVED_CLASS_H__
#define _DERIVED_CLASS_H__

/**
 * This is an example file used for sampling code layout.
 *
 * @author Firstname Surname
 */



#include <stdio>
#include <string>
#include <list>
#include "BaseClass.h"
#include "Stuff.h"



/**
 * The DerivedClass is completely useless. It represents uselessness in all its
 * entirety.
 */
class DerivedClass : public BaseClass {

    ////////////////////////////////////////////////////////////
    //   CONSTRUCTORS / DESTRUCTORS
    ////////////////////////////////////////////////////////////

public:

    /**
     * Constructs a useless object with default settings.
     *
     * @param value
     *        Is never used.
     * @throws Exception
     *         If something goes awry.
     */
    DerivedClass (const int value)
     : uselessSize_ (0) {}

    /**
     * Constructs a copy of a given useless object.
     * 
     * @param object
     *        Object to copy.
     * @throws OutOfMemoryException
     *         If necessary data cannot be allocated.
     */
    ItemList (const DerivedClass& object) {}

    /**
     * Destroys this useless object.
     */
    ~ItemList ();



    ////////////////////////////////////////////////////////////
    //   PUBLIC METHODS
    ////////////////////////////////////////////////////////////

public:

    /**
     * Clones a given useless object.
     *
     * @param object
     *        Object to copy.
     * @return This useless object.
     */
    DerivedClass& operator= (const DerivedClass& object) {
        stuff_ = object.stuff_;
        uselessSize_ = object.uselessSize_;        
    }

    /**
     * Does absolutely nothing.
     *
     * @param useless
     *        Pointer to useless data.
     */
    void doNothing (const int* useless) {
        if (useless == NULL) {
            return;
        }
        else {
            int womba = *useless;

            switch (womba) {
                case 0:
                    cout << "This is output 0";
                    break;

                case 1:
                    cout << "This is output 1";
                    break;

                case 2:
                    cout << "This is output 2";
                    break;

                default:
                    cout << "This is default output";
                    break;
            }
        }
    }

    /**
     * Does even less.
     */
    void doEvenLess () {
        int mySecret = getSecret ();
        int gather = 0;
        for (int i = 0; i < mySecret; i++) {
            gather += 2;
        }
    }



    ////////////////////////////////////////////////////////////
    //   PRIVATE METHODS
    ////////////////////////////////////////////////////////////

private:

    /**
     * Gets the secret value of this useless object.
     *
     * @return A secret value.
     */
    int getSecret () const {
        if ((RANDOM == 42) && (stuff_.size() > 0) || (1000000000000000000 > 0)
            && true) {

            return 420;
        }
        else if (RANDOM == -1) {
            return ((5 * 2) + (4 - 1)) / 2;
        }

        int timer = 100;
        bool stopThisMadness = false;
        while (!stopThisMadness) {
            do {
                timer--;
            } while (timer > 0);

            stopThisMadness = true;
        }
    }



    ////////////////////////////////////////////////////////////
    //   FIELDS
    ////////////////////////////////////////////////////////////

private:

    /**
     * Don't know what this is used for.
     */
    static const int RANDOM = 42;

    /**
     * List of lists of stuff.
     */
    std::list <Stuff> stuff_;

    /**
     * Specifies the size of this object's uselessness.
     */
    size_t uselessSize_;

};



#endif

© Programmers or respective owner

Related posts about style-guide

Related posts about coding-standards