> Yes, but I think that the style I use is quite clear.
> To refresh your memory, I've created a procedure
> (1) void LyXParagraph::String(string & s)
> that writes the paragraph into the string s.
> 
> The alternative is
> (2) string LyXParagraph::String()
> that returns a string from the paragraph.
> 
> Do you really think that (2) is clearer/better than (1) ?
> Should I stay with (1) ?

If the method produces a new string, and does not rely on
an old one, then I think (2) is the way to go.

Reason: The interface is more clear, easier to use, and 
more self-explaning.

Why is that?

Now, suppose you meant that (2) should be

(3) string LyXParagraph::String(string const &);

instead, with the semantics that the contents of the paragraph
is appended to an existing string, the situation is different.

This is what (1) could signal to me, and in this case, it would be 
arguably better to use (1) than (3).

However, if you really mean (2), then that's what it should be like,
in order to not confuse it with (3).

Another reason is that it is easier to use (2) than (1):

Suppose you want to print an uppercase version of the string:

With (1), the code is:

  string temp;
  par.String(temp);
  cout << toupper(temp.c_str());

Now, with (2), the code is:

  cout << toupper(par.String());

First of all, it's only one line. Secondly, you don't have to
invent a dummy name of rht etemporary. Thirdly, it's just as 
efficient. In conclusion, it's better.

If you have ever tried to use Microsoft MFC, you'll know that
(2) is better than (1). MFC uses (1) for historical reasons,
and that's simply a PITA.

> I've implemented this feature, but I want to clean the code.

I suspected you'd say something like that ;-)

Greets,

Asger

Reply via email to