[IGSTK-Developers] Get method possibilities
David Gobbi
dgobbi at atamai.com
Mon Oct 24 12:11:27 EDT 2005
Hi All,
The ImageReader Get methods that were just added to CVS are a
bit clunkier than I would like, due to the boolean return value. In
VTK and ITK, the methods would return the string (or a const
reference to the string) and forgo the boolean return value.
Typical VTK/ITK code looks like this:
if (reader->FileSuccessfullyRead())
{
std::string patientName = reader->GetPatientName();
std::string patientID = reader->GetPatientID();
std::string modality = reader->GetModality();
.. plus additional code to process the information
}
... at this point in the code, the patientName, patientID, and modality
variables are no longer in scope, so there is no danger of them being
used if the file hasn't been read yet.
With the methods that were just added (with the boolean return value),
the code will look like this:
std::string patientName;
if (reader->GetPatientName(patientName))
{
... use the patient name
}
std::string patientID;
if (reader->GetPatientID(patientID))
{
... use the patient ID
}
... and so on for modality
... at this point in the code, the "patientName", "patientID", and
"modality"
variables are all still in scope, even if the Get() methods failed.
This sort
of a programming pattern reminds me of FORTRAN.
Just my two cents.
- David
P.S. I can't resist adding an example that uses exceptions:
try
{
std::string patientName = reader->RequestPatientName();
std::string patientID = reader->RequestPatientID();
std::string modality = reader->RequestModality();
.. plus additional code to use the information, not executed if an
exception occurs
}
catch (InvalidRequest)
{
... send debug info to logger
}
More information about the IGSTK-Developers
mailing list