C++ Tips: Difference between revisions

Jump to navigation Jump to search
166 bytes added ,  19 October 2009
no edit summary
No edit summary
Line 17: Line 17:
* Prefer early-return style. Don't make people scroll to see which code-path they're inside; use <tt>return</tt> to manage control flow, instead of deep nesting of <tt>if</tt> clauses. If you are nesting more than three level deep, consider making a helper function for the inner levels, or see if you can invert the logic to reduce indentation.
* Prefer early-return style. Don't make people scroll to see which code-path they're inside; use <tt>return</tt> to manage control flow, instead of deep nesting of <tt>if</tt> clauses. If you are nesting more than three level deep, consider making a helper function for the inner levels, or see if you can invert the logic to reduce indentation.


* Use exceptions. Exception support is not well developed, but they provide a clean way to handle genuinely unusual situations without (easily ignorable) bool or integer return codes. The main loop catches exceptions, so use the SimGear exception classes, provide good location and message strings when you throw, and all should be well. Using exceptions can greatly simplify code, by removing continual failure / error checks in the main code.
* Use exceptions. Exception support is not well developed, but they provide a clean way to handle genuinely unusual situations without (easily ignorable) bool or integer return codes. The main loop catches exceptions, so use the SimGear exception classes, provide good location and message strings when you throw, and all should be well. Using exceptions can greatly simplify code, by removing continual failure / error checks in the main code. Ensure you differentiate between fatal errors (use <tt>sg_error</tt>) and problems specific to a subsystem or operation (use <tt>sg_exception</tt> or some subclass).


* Ensure headers are independent and minimal (see the discussion of forward declarations further down). This means each header should include everything it needs to parse, and no more. Header should feature the following (in order):
* Ensure headers are independent and minimal (see the discussion of forward declarations further down). This means each header should include everything it needs to parse, and no more. Header should feature the following (in order):
580

edits

Navigation menu