Thread: What is a better style?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    What is a better style?

    I have some functions which perform string transformations. Since they use the original string, but return a copy of the modified string, what is better style?
    Code:
    string transform1(const string& s) { //uses const reference
    	string s2 = s; //make a copy
    	//do some stuff on the copy
    	return s2;
    }
    
    string transform2(string s) { //passed by value
    	//do some stuff on the copy already passed in
    	return s;
    }
    Last edited by jw232; 09-14-2008 at 10:50 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd say that in this case, it really is a matter of style, and both are fine. The former has the advantage of being more consistent since strings are normally passed by (const) reference; the latter has the advantage of being a little less verbose.

    Note that the const for the latter is wrong. You actually want to change s, so s should be non-const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    I see. Thanks for the correction. Is there any performance difference?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there any performance difference?
    The MinGW port of g++ 3.4.5 does produce different assembly code for the two cases, but I am afraid that I have neither the time nor skill at the moment to determine if this makes for any real difference in efficiency.

    That said, I daresay that there is no real difference: the time will likely be dominated by the copying of characters, which is the same in both cases. If you are particularly concerned, profile your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If the string you pass is not already a variable, the latter is faster.
    Code:
    string s = transform1("Hello, World!");
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM