Tech/LxEngine/Coding Style

From Athile

Jump to:navigation, search
This page is a work-in-progress. It is not yet complete.
It may contain inaccurate, incorrect information. Use at your own risk.
 todo Add justification for these style decisions

Contents

Principles


If it's complicated, it's not done yet - That's a good principle for the code. It's alright for a first revision of some code to be a bit complex because it takes work to make things simple. The key idea to remember is that the code is not done when it 'works'; it needs to be iterated over until it is simple.

Conventions


Classes and Concepts

LxEngine developers should be familiar with the following classes and concepts and, for consistency, use them when appropriate (rather than alternate implementations):

  • lambda functions [1]
  • auto keyword [2]
  • std::function<>
  • std::vector<> [3]
  • std::map<> [4]
  • std::shared_ptr<>
  • std::weak_ptr<>
  • std::unique_ptr<>
  • boost::format<> [5]
  • boost::any<> [6]
  • boost::algorithm::string [7]
  • HTML DOM [8]
  • Javascript
  • JSON data
  • GLM library [9]

Detail

File Layout

module\
    group\
        submodule\
            functions.hpp
            functions.inl
            class0.hpp
            class0.inl
            class1.hpp
            class1.inl
            ...
        submodule.hpp


Robust, Non-Performance Critical Function

//---------------------------------------------------------------------------//
// function name
//---------------------------------------------------------------------------//
//! brief description
/*!
    \ingroup module_group_submodule
 
    Here is the long description of the function.  More details. Etc.
    Etc.
 
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
    veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
    commodo consequat. Duis aute irure dolor in reprehenderit in voluptate 
    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint 
    occaecat cupidatat non proident, sunt in culpa qui officia deserunt 
    mollit anim id est laborum.
 */
static inline
float
my_function_name (Object* pObject, int a, float b, float b)
{
    //
    // Check pre-conditions before potentially modifying any persistent state.
    // Want to potentially throw exceptions only with transient state modifications.
    //
    lx_assert (a != 7, "Do not pass in 14, not 7.  The result will be the same but faster");
    lx_check_error(pObject != nullptr);
    lx_check_error(pObject->method1() != false, "Object in incompatiable state");
 
    try
    {       
        int intermediate = pObject->doSomeWork(a, b);
        for (int i = 0; i < a; ++i)
        {
            intermediate *= pObject->doMoreWork(a);
        }
        pObject->finishWork(intermediate * c);
 
        lx_check_warn(intermediate == 0, "Object intermediate was zero");
        lx_assert(pObject->statusComplete() == true);       
    }
    catch (lx0::error_exception& e)
    {
        e.location(__FILE__, __LINE__);
        e.detail("Failed to compute widget factor delta");
        e.detail("a=%1%, b=%2%, c=%3%", a, b, c);
        throw;
    }
    catch (l0x::fatal_exception& f)
    {
        _write_minimal_emergency_crash_data(pObject);
        f.detail("Object data saved to crash file");
        throw;
    }
}

Todos

Ideally todos should be in the following format:

void 
MyClass::myMethod (int arg1, int arg2)
{
    ///@todo Implement myMethod to <short summary of implementation>
    /*
        This should be a longer description of what the implementation
        involves.  It should include as much description as possible
        while the issue is fresh in mind.
 
        Multiple paragraphs are fully warranted in this case since
        if it was minor, it should not be left as a todo.  If it is not
        minor, then it likely requires some description - namely the
        challenging aspects that caused the item to be left as a todo.
    */
    lx_error("Code not implemented!");
}

There are four parts to the above:

Glossary

 

Issues

Use size_t or int for values that shouldn't ever negative?

Example: should Image::width() return a size_t or an int?

Response:

Navigation
Toolbox