473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

interface question: what should return a transformation method?

Hi all,

for a transformation method I mean a method which will transform an
object changing its internal state.

Do you think it is a good idea to return the pointer to the modified object like this:
Object* Object::transform()
?

In this way it is possible to combine different transformation methods
to get the very convenient and compact notation:

obj.stretch().munge().dilate().colorize();

On the other hand, the Java standard library and some C++ libraries
(e.g. libpt) seem to prefer to return a boolean (true in case of a
successful operation).

Any thoughts? Many thanks in advance.
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
Dec 13 '07 #1
3 1197
Stefano Sabatini wrote:
for a transformation method I mean a method which will transform an
object changing its internal state.

Do you think it is a good idea to return the pointer to the modified
object like this: Object* Object::transform()
?
A reference to it is probably better.
In this way it is possible to combine different transformation methods
to get the very convenient and compact notation:

obj.stretch().munge().dilate().colorize();
No, if you return a pointer, you will need to use ->:

obj.stretch()->munge()...
On the other hand, the Java standard library and some C++ libraries
(e.g. libpt) seem to prefer to return a boolean (true in case of a
successful operation).
It is impossible to tell without the context. If the operation *can*
fail, and you *do want* not to proceed with 'munge' after the failed
'stretch', then you shouldn't chain them, you should instead do

if (obj.stretch() && obj.munge() && ...)

which will short-circuit and return false from the first operation
that doesn't finish "correctly". If you need to know which operation
failed, you need to wrap each in its own 'if'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '07 #2
On 2007-12-13, Victor Bazarov <v.********@comAcast.netwrote:
Stefano Sabatini wrote:
>for a transformation method I mean a method which will transform an
object changing its internal state.

Do you think it is a good idea to return the pointer to the modified
object like this: Object* Object::transform()
?

A reference to it is probably better.
>In this way it is possible to combine different transformation methods
to get the very convenient and compact notation:

obj.stretch().munge().dilate().colorize();

No, if you return a pointer, you will need to use ->:

obj.stretch()->munge()...
>On the other hand, the Java standard library and some C++ libraries
(e.g. libpt) seem to prefer to return a boolean (true in case of a
successful operation).

It is impossible to tell without the context. If the operation *can*
fail, and you *do want* not to proceed with 'munge' after the failed
'stretch', then you shouldn't chain them, you should instead do

if (obj.stretch() && obj.munge() && ...)

which will short-circuit and return false from the first operation
that doesn't finish "correctly". If you need to know which operation
failed, you need to wrap each in its own 'if'.
Mmh... yes it makes perfect sense, only now I wonder how the C++ exception
handling mechanism would handle such a thing (that is if
try {
obj.stretch().munge().dilate().colorize();
} catch (StretchingException) {
...
} catch (MungingException() {
...
} ...
}
would work).

But that is clearly another question.

Thanks for your reply Victor!
Regards.
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
Dec 13 '07 #3
On Dec 13, 4:40 pm, Stefano Sabatini <stefano.sabat...@caos.org>
wrote:
On 2007-12-13, Victor Bazarov <v.Abaza...@comAcast.netwrote:
[...]
It is impossible to tell without the context. If the operation *can*
fail, and you *do want* not to proceed with 'munge' after the failed
'stretch', then you shouldn't chain them, you should instead do
if (obj.stretch() && obj.munge() && ...)
which will short-circuit and return false from the first operation
that doesn't finish "correctly". If you need to know which operation
failed, you need to wrap each in its own 'if'.
Mmh... yes it makes perfect sense, only now I wonder how the C++ exception
handling mechanism would handle such a thing (that is if
try {
obj.stretch().munge().dilate().colorize();
} catch (StretchingException) {
...
} catch (MungingException() {
...
} ...}
would work).
Why shouldn't it. An exception interupts the expression being
evaluated. Whether it is an appropriate solution or not depends
on a number of other factors, in particular, what types of
failure are you expecting.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
2
by: PatrickSA | last post by:
Hi, Am new to web services, so apologies for the basic nature of the question - and apologies in advance if this is the wrong newsgroup. We're building a new web service and I'm looking around...
10
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly...
3
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
1
by: yadolov | last post by:
I have COM-object with Iface interface: typedef struct { .... } TStruct; interface Iface : IUnknown {
8
by: Lamefif | last post by:
// C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.