On 2012-10-01 12:27 PM, Nathan Froyd wrote:
I recently filed a bug (bug 792169) for adding NS_NewIMutableArray, in
service of deleting nsISupportsArray.  The reviewer asked me to use
more standard C++ instead of perpetuating the NS_New* idiom and I did
so, with a static |Create| member function.

However, looking through other code--especially the various bits under
dom/ that have been added for B2G--I see that the NS_New* style is alive
and well.

Points for NS_New*:

+ Avoid exporting internal datastructures;
+ Possibly more efficient than do_CreateInstance and friends;
- Usually requires %{C++ blocks in IDL files;
- Less C++-y.

Points for new and/or static member functions:

+ More C++-y;
+ Less function call overhead compared to NS_New* (modulo LTO);
- Drags in more headers.

...and there are probably other things that I haven't thought of for
both of them.

So which style do we want to embrace?  Or are there good reasons to
continue with both?

There might be times where you want the concrete type of the thing that your ctor function returns to be opaque, in which case it might make sense to go with the NS_NewFoo style of method. But in all other cases I would prefer Foo::Create or something like that (or just new Foo, if you don't need to do fallible things in the ctor). Basically the more our code looks like C++, the better it is for people who have worked on other code bases.

Cheers,
Ehsan
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to