Linked by Bjorn Raupach on Thu 17th Jul 2008 06:01 UTC
Java Today was one of those days when I wished Java would support multiple return values. I had to develop a rather CPU-intensive algorithm which would compute a solution for a knotty constraint problem. Having a solution alone is sometimes not enough and you also need to add some parameters which measure the quality of the computed outcome. Most of these accompanying parameters can or have to be computed within the algorithm itself, but Java allows you to return only one value either an object or a primitive type. People working with Lisp, MATLAB or Perl, just to mention a few, don't have a problem like this at all. Functions supporting multiple return values is already implemented at the language level and frameworks make heavy use of this. But as a Java programmer you are pretty much stuck here and need to consider some other means to come out of this situation. In the following I would like to give some hints on that topic. Hopefully they are of help for anyone having the same problem every now and then.
Thread beginning with comment 323613
To view parent comment, click here.
To read all comments associated with this story, please click here.
renox
Member since:
2005-07-06

Er, not really. const is a standard feature in C++ for declaring, well, constants.

Except that const are not always constants in C++.


It's hardly a hack: certainly no more than inventing silly ways to return multiple values from a function.

I find much more readable to have the input variable on one part of the function and the output variables on another part, of course readability isn't C++ strong point.

Reply Parent Bookmark Score: 2

ebasconp Member since:
2006-05-09

"Er, not really. const is a standard feature in C++ for declaring, well, constants.

Except that const are not always constants in C++.
"

Agree, "const-ness" defines a set of properties and behaviors over our methods and variables.

Let's say:

const Attribute& GetAttribute() const;

A method marked with "const" says: "I will not modify the object state, I will only provide information"; this feature prevents a lot of errors and undesired access because it is hardly viral (for this example, you cannot invoke to a "non-const" method defined in Attribute).

The "const Attribute& " returns a reference to an attribute defined inside our object (a really nice approach to avoid object copy or memory management "ambiguity") If I get a const Attribute& I'm getting a working state and I have not to worry about its memory handling.

"const-ness" should be implemented in other C-like (I knew D implements it partially) languages because provides nice behavior and improves the code readibility too.

Reply Parent Bookmark Score: 2

renox Member since:
2005-07-06

Agree, "const-ness" defines a set of properties and behaviors over our methods and variables.

Bleah, I don't like C++'s overloading of keywords for type and method signature..

If I get a const Attribute& I'm getting a working state and I have not to worry about its memory handling.

Not always: if there's another reference with read-write access to the same object, it can be modified or the const may be casted away.

I knew D implements it partially

Yup, but it caused many, many discussions: head vs tail const, const vs invariant.. Const is a concept really hard to design properly.

And the solution retained is ineleguant as for C++ compatibility's sake, the name const is retained for read-only views (which may not be really constant!), and true constants are named 'invariant'.
IMHO 'const' should be replaced by 'view' as in "view but don't touch (others may do it though)"
and true constant named const.

Reply Parent Bookmark Score: 2