We have moved to https://dokuwiki.librecad.org/

Lots of content was already moved to the new wiki, but there is still work to do. If you want to contribute, please register a new account at https://dokuwiki.librecad.org/

This wiki will be kept for a while to keep search engine results valid. Moved sites may be deleted here in future.

Difference between revisions of "LibreCAD 3 - LibreCAD 3 - code style"

From LibreCAD wiki
Jump to: navigation, search
Line 10: Line 10:
 
</pre>
 
</pre>
  
 +
 +
'''Immutables:'''
 +
Are immutables must be created 'valid', throw an exception if a immutable is attempted to be created that is invalid.
 +
 +
example:
 +
<pre>
 +
Circle::Circle(const Coordinate& center, double radius) : _center(center) {
 +
    if (radius < 0.0) {
 +
        throw "Invalid radius";
 +
    }
 +
 +
    _radius = radius;
 +
}
 +
</pre>
  
 
'''Exceptions:'''
 
'''Exceptions:'''

Revision as of 00:03, 16 May 2014

Code style guide

Accessor naming:

use foo->layer()

instead of

 foo->getLayer();


Immutables: Are immutables must be created 'valid', throw an exception if a immutable is attempted to be created that is invalid.

example:

Circle::Circle(const Coordinate& center, double radius) : _center(center) {
    if (radius < 0.0) {
        throw "Invalid radius";
    }

    _radius = radius;
}

Exceptions:

Only throw exceptions in rare situations that are effectively unreasonable.

For example:

document->entityByID(...); // return null if a entry does not exists


hide shared_ptr behind a typedef:

    typedef std::shared_ptr<Circle> Circle_SPtr;
    typedef std::shared_ptr<const Circle> Circle_CSPtr;

This so we can 'shorten' our shred pointers, example:

  std::list<std::shared_ptr<const Circle>> myListLong; // Less readble
  std::list<Circle_CSPtr> myListReadable; // better readable


Shared pointer passing:

never pass shared_ptr by reference:

Good:

Line::Line(const geo::Coordinate& start, const geo::Coordinate& end, const Layer_SPtr layer) : CADEntity(layer), Vector(start, end) {..}

Bad:

Line::Line(const geo::Coordinate& start, const geo::Coordinate& end, const Layer_SPtr &layer) : CADEntity(layer), Vector(start, end) {..}