Posting some comments on code examples I found.
I know it's tempting to copy and paste parts of allready existing
code. But keep in mind that not all code we have is an example of
how to do it right.
Please carefully review code you want to reuse and fix the flaws.
data/ResolvableData.h
+++++++++++++++++++++
#include "zypp/TranslatedText.h"
#include <string>
#include <list>
#include <iostream>
#include
---------------------
- There are two kind of includes: 'system' (enclosed in <>) and 'local'
(enclosed in "").
zypp/base/PtrTypes.h is not a system include, thus should be enclosed
in "".
- Try to avoid including <iostream> in header files. Use <iosfwd>.
data/ResolvableData.h
+++++++++++++++++++++
using namespace zypp::base;
---------------------
- NEVER use 'using ...;' in a header file!
There are litte exceptions to this rule, but they drag individual symbols
into another namespace.
- NEVER NEVER NEVER use 'using namespace ...;' in a header file e.g. to
save typing.
yum/PrimaryFileReader.h
+++++++++++++++++++++++
#undef ZYPP_BASE_LOGGER_LOGGROUP
#define ZYPP_BASE_LOGGER_LOGGROUP "parser"
-----------------------
ZYPP_BASE_LOGGER_LOGGROUP defines the log class used when writing
lines to y2log.
2007-05-02 14:02:09 <3> Fibonacci(28655) [zypp] ....
^^^^
It should be obvious that this define must never appear in a header.
Not every module that includes PrimaryFileReader.h want's to use log
class "parser". Adding/removing includes must not change the log
class of existing code.
yum/PrimaryFileReader.h
+++++++++++++++++++++++
/**
* Constructor
* \param primary_file the primary.xml.gz file you want to read
* \param function to process \ref _package data.
*
* \see PrimaryFileReader::ProcessPackage
*/
PrimaryFileReader(const Pathname &primary_file, ProcessPackage callback, ParserProgress::Ptr progress);
-----------------------
- In short: ProcessPackage is a POINTER to a (callback)function that
should somehow consume the package data pased. ParserProgress::Ptr
is a POINTER to some progress reporting object. (Right?)
One can not assume that any caller provides a callback or is
interested in receiving progress information.
So you must test pointer for != NULL before using them.
yum/PrimaryFileReader.h
+++++++++++++++++++++++
class PrimaryFileReader
{
private:
...
zypp::data::Package *_package;
-----------------------
yum/PrimaryFileReader.cc
+++++++++++++++++++++++
if (_package) delete _package;
_package = new zypp::data::Package();
...
-----------------------
http://www.amazon.com/Effective-C%2B%2B-Addison-Wesley-Professional-Computing/dp/0321334876/ref=pd_bbs_sr_1/104-5163606-1722339?ie=UTF8&s=books&qid=1178108834&sr=8-1
1) Class has a pointer to allocated data :(
You MUST EITHER provide copy ctor and assign operator OR forbid
copy and assign (e.g. see base/NonCoyable.h) in order to protect
against duplicate free
OR USE A SMART POINTER.
The code leaks memory in case of errors and exceptions. There is no
dtor asserting allocated memory gets freed in case of something
unexpected happening.
Providing a dtor would be ok, if there were no exceptions thrown
inside the ctor. But the ctor throws. ....
USE A SMART POINTER.
It's provided to silently do all the things we tend to forget but
which have to be done.
USE RAII
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
Resource Acquisition Is Initialization, often referred to by the acronym RAII,
is a popular programming technique in C++ and D. The technique
combines acquisition and release of resources with initialization and
uninitialization of variables.
--
cu,
Michael Andres
+------------------------------------------------------------------+
Key fingerprint = 2DFA 5D73 18B1 E7EF A862 27AC 3FB8 9E3A 27C6 B0E4
+------------------------------------------------------------------+
Michael Andres YaST Development ma@novell.com
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg)
Maxfeldstrasse 5, D-90409 Nuernberg, Germany, ++49 (0)911 - 740 53-0
+------------------------------------------------------------------+
--
To unsubscribe, e-mail: zypp-devel+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-devel+help@opensuse.org