Which is better style?

Started by
16 comments, last by BattleMetalChris 14 years, 2 months ago
Which is better style, 1)

string str = convert<string>(123,BASE_16);
where convert has the following prototype :

template<typename ReturnType, typename InputType>
ReturnType convert(const InputType, const size_t base);
or this way : 2)

string str;
convert(123,str,BASE_16);
where the convert function has the following prototype :

template<typename ReturnType, typename InputType>
void convert(const InputType src,ReturnType& dest, const size_t base);
I feel like the second one looks better.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Advertisement
I personally favor #1 strongly. Functions exist to take params and return results. Mixing them up in the parameter list is (imo) distasteful.
Definitely #1.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Yeah number one is far superior.
#1.
It depends. Can the operation fail? It often makes sense to use an output parameter for the result and reserve the return value for indicating success/failure, most often in cases where a) the output itself can't be used to reliably detect success/failure, and b) you can't use exceptions. I prefer #1 when I have a choice, though.
What on earth do you prefer #2 for? The only time I consider anything like that is when I don't want to copy some big object for a return value, and even then creating and initializing the variable on two lines is annoying.
Quote:Original post by theOcelot
What on earth do you prefer #2 for? The only time I consider anything like that is when I don't want to copy some big object for a return value, and even then creating and initializing the variable on two lines is annoying.


One would use #2 when trying to avoid redundant heap allocations when parsing a lot of text, perhaps hundreds of thousands of lines, thereby reducing running time by a factor of 10.

YMMV, depends on implementation of std::string and whether RVO/NRVO can handle objects which invoke new in constructor.
If you really expect to need #2 in specific circumstances for performance reasons, you can always implement #1 in terms of #2, and provide both:

// Ugly version for people who have identified a needtemplate<typename ReturnType, typename InputType>void convert(const InputType& src, ReturnType& dest, const size_t base) {  // evil conversion logic}// Pretty version for people who like pretty codetemplate<typename ReturnType, typename InputType>ReturnType convert(const InputType& src, const size_t base) {  ReturnType result;  convert(src, result, base);  return result;}
Quote:Original post by Antheus
Quote:Original post by theOcelot
What on earth do you prefer #2 for? The only time I consider anything like that is when I don't want to copy some big object for a return value, and even then creating and initializing the variable on two lines is annoying.


One would use #2 when trying to avoid redundant heap allocations when parsing a lot of text, perhaps hundreds of thousands of lines, thereby reducing running time by a factor of 10.


Sure, other efficiency issues. But Concentrate seems to prefer it aesthetically. That's what I don't get.

This topic is closed to new replies.

Advertisement