Today's studying day focused primarily on various implementation choices and how they affect performance and that exception safety that usually destroys most C++ programs.
Step 1: Like a lazily instantiated Singleton, if an exception is going to stop your code before you can use a variable, define it after the problem section for a little optimization.
Step 2: It's preferential to stick with casting (of the hot, new, and not-C-like C++ cast!). Whether it be the casting away of constness (const_cast), determining an object in an inheritance hierarchy (dynamic_cast, but due to the cost of calling 4-5 strcmps if the level is deep enough, save a smart pointer to derived classes or use a virtual function for access), unportable casting (pointer to int? Well, there's reinterpret_cast...) and implicit conversion forcing (static_cast). Despite the costliness of dynamic_cast, the C++ casts are simpler to identify and are definitely preferred. Praise be.
Step 3: Avoid returning things like pointers that may be const and protected from being changed, but that reaaaally private data member can be accessed from the const reference.
Step 4: Exceptions are bad. Either don't throw one, or ensure that if an exception is thrown, the data remains in a valid or otherwise unchanged state. A neat way to do this (cost aside) is the copy and swap method, in which a copy of data is made and changes are applied to it. If the exception is thrown, the copy is ditched and the original is clean. Else, swap it!
Step 5: Inlines can be very useful, especially in templates. Both are in header files, so it makes sense to combine the two wherever appropriate? Nope. If one has to change implementation on inlines, that requires recompilation (instead of relinking or even swapping dll files). Best off if the function is short, frequently used, and trivial enough to not require extra implementation changes down the road.
As for the EEG software that I've been working with, I finally found where the Unity file is so I can change files! Oddly enough, everything was lowercase so nothing worked (Unity being all case-sensitive about its classes matching its files) so I worked through that and no errors are found. Now I just have a pile of JSON gobblegook that I can't read, so I'll have to speak to the person in charge.
No comments:
Post a Comment