Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #5168 > unrolled thread
| Started by | Gene Wirchenko <genew@ocis.net> |
|---|---|
| First post | 2011-06-09 23:03 -0700 |
| Last post | 2011-06-17 11:43 -0300 |
| Articles | 20 on this page of 81 — 21 participants |
Back to article view | Back to comp.lang.java.programmer
Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-09 23:03 -0700
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-10 07:33 +0000
Re: Call by Result Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-06-10 10:03 +0100
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-10 09:47 +0000
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-10 08:23 -0300
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-10 13:12 +0000
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-10 15:44 -0300
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-12 16:29 +0000
Re: Call by Result Silvio <silvio@moc.com> - 2011-06-12 23:00 +0200
Re: Call by Result Silvio <silvio@moc.com> - 2011-06-12 23:06 +0200
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-14 10:52 +0000
Re: Call by Result Silvio <silvio@moc.com> - 2011-06-14 17:13 +0200
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-14 17:58 -0300
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-14 21:18 +0000
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-14 20:14 -0300
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-15 07:13 -0300
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-15 12:50 +0000
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-15 18:52 -0300
Re: Call by Result lewbloch <lewbloch@gmail.com> - 2011-06-15 07:06 -0700
Re: Call by Result Michael Wojcik <mwojcik@newsguy.com> - 2011-06-15 21:33 -0400
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-16 11:03 +0000
Re: Call by Result "H.J. Sander Bruggink" <sander.bruggink@uni-due.de> - 2011-06-16 13:09 +0200
Re: Call by Result Lewis Bloch <lewisbloch@google.com> - 2011-06-16 06:59 -0700
Re: Call by Result "John B. Matthews" <nospam@nospam.invalid> - 2011-06-16 23:14 -0400
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:31 -0700
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-12 14:27 -0300
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-10 07:20 -0700
Re: Call by Result Patricia Shanahan <pats@acm.org> - 2011-06-10 09:30 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:44 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:26 -0700
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-11 15:07 +0000
Re: Call by Result Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:00 +0530
Re: Call by Result Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-11 19:36 +0000
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-11 21:06 +0000
Re: Call by Result Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-12 10:53 +0000
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-12 09:28 -0700
Re: Call by Result Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-12 17:48 +0000
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-12 13:19 -0700
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-11 15:39 -0700
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-12 11:07 +0000
Re: Call by Result markspace <-@.> - 2011-06-12 07:02 -0700
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-12 09:39 -0700
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-14 10:29 +0000
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-14 09:14 -0700
Re: Call by Result markspace <-@.> - 2011-06-14 09:21 -0700
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-14 09:53 -0700
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-14 09:57 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-12 21:44 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-12 21:40 -0700
Re: Call by Result Patricia Shanahan <pats@acm.org> - 2011-06-10 01:30 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:45 -0700
Re: Call by Result Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-10 07:50 -0400
Re: Call by Result Silvio <silvio@moc.com> - 2011-06-10 15:35 +0200
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-10 13:56 +0000
Re: Call by Result Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-06-10 07:22 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:56 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:57 -0700
Re: Call by Result Patricia Shanahan <pats@acm.org> - 2011-06-10 13:50 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-12 21:53 -0700
Re: Call by Result Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-13 07:20 -0300
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-13 08:08 -0700
Re: Call by Result Wojtek <nowhere@a.com> - 2011-06-11 16:35 -0700
Re: Call by Result Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-12 11:05 +0000
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-12 21:56 -0700
Re: Call by Result Wojtek <nowhere@a.com> - 2011-06-14 00:40 -0700
Re: Call by Result Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-11 21:18 +0000
Re: Call by Result markspace <-@.> - 2011-06-10 08:43 -0700
Re: Call by Result RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-06-10 17:01 +0100
Re: Call by Result Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-10 16:55 +0000
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:59 -0700
Re: Call by Result Wojtek <nowhere@a.com> - 2011-06-11 16:24 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-12 21:59 -0700
Re: Call by Result Paul Cager <paul.cager@googlemail.com> - 2011-06-13 07:53 -0700
Re: Call by Result Wojtek <nowhere@a.com> - 2011-06-14 00:43 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-10 11:55 -0700
Re: Call by Result Steven Simpson <ss@domain.invalid> - 2011-06-11 09:09 +0100
Re: Call by Result Cholo Lennon <chololennon@hotmail.com> - 2011-06-16 11:30 -0300
Re: Call by Result Paul Cager <paul.cager@googlemail.com> - 2011-06-17 02:38 -0700
Re: Call by Result Gene Wirchenko <genew@ocis.net> - 2011-06-17 12:06 -0700
Re: Call by Result lewbloch <lewbloch@gmail.com> - 2011-06-17 06:40 -0700
Re: Call by Result Cholo Lennon <chololennon@hotmail.com> - 2011-06-17 11:43 -0300
Page 4 of 5 — ← Prev page 1 2 3 [4] 5 Next page →
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-06-13 08:08 -0700 |
| Message-ID | <qp9cv6lq401o1an6k4t9fupdji7bhulv5b@4ax.com> |
| In reply to | #5285 |
On Mon, 13 Jun 2011 07:20:22 -0300, Arved Sandstrom
<asandstrom3minus1@eastlink.ca> wrote:
>On 11-06-13 01:53 AM, Gene Wirchenko wrote:
[snip]
>> Much code looks the same in multiple languages. Which language
>> is
>> a=b*c+d
>> in?
>
>Ideally none. Which brings us back to Patricia's point: what you would
>_actually_ use in place of 'a' tends to look different based on what
>language you're using.
No, not I. What *someone else* might actually use could differ
depending on language. *I* try to keep them the same or similar
between languages. I understand that there are differences between
languages that have to be allowed for, but variable names is not
usually one of them.
[snipped lots of good points]
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Wojtek <nowhere@a.com> |
|---|---|
| Date | 2011-06-11 16:35 -0700 |
| Message-ID | <mn.5be37db60c490f13.70216@a.com> |
| In reply to | #5199 |
Gene Wirchenko wrote : > I am also someone who has been programming professionally for > about twenty-five years. In that time, I have worked out conventions > across different languages for variable-naming. So have I (30 years actually, ah I can see retirement from here...). And I have had training and/or self-taught in over 15 languages. Shrug, so what. However I religiously use the conventions of the language I am writing in. Amongst other things it allows future maintenance programmers to read the code without needing to do mental gymnastics to convert code style before they can understand what the code is doing. This is important if you ever work in a professional environment. Your code WILL be looked at by someone else sometime in the future. The lone programmer (consultant or hired) writing obfuscated code to their own personal style/standards would not be tolerated in any of the companies I have worked for. -- Wojtek :-)
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2011-06-12 11:05 +0000 |
| Message-ID | <it26i2$n3q$2@localhost.localdomain> |
| In reply to | #5228 |
On Sat, 11 Jun 2011 16:35:03 -0700, Wojtek wrote: > The lone programmer (consultant or hired) writing obfuscated code to > their own personal style/standards would not be tolerated in any of the > companies I have worked for. > Unfortunately, I've seen a bit much of that, ranging from the idiotic: - an entire COBOL system written with data names of the form NN999 where, e.g. NN was MT for all data names in all the mag.tape files used by a program and 999 ran sequentially from MT001 for the first tape, MT002 for the first record MT003 for the first field in the first record.... and IIRC field and record names could and did differ between programs. The same was done in PROCEDURE DIVISION except that the paragraph names were no longer sequential. to - a lone contractor by the name of Ken Eagle who called any additional numeric data names KEN1, KEN2,... and any strings EAGLE1, EAGLE2,... This was all a long time also hopefully it would no longer happen. However, as the competence of management doesn't seem to have improved much over the years, I wouldn't hold my breath. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-06-12 21:56 -0700 |
| Message-ID | <ru5bv6ddijl55r3f20i1s774ul5484dpko@4ax.com> |
| In reply to | #5228 |
On Sat, 11 Jun 2011 16:35:03 -0700, Wojtek <nowhere@a.com> wrote:
>Gene Wirchenko wrote :
>> I am also someone who has been programming professionally for
>> about twenty-five years. In that time, I have worked out conventions
>> across different languages for variable-naming.
>
>So have I (30 years actually, ah I can see retirement from here...).
>And I have had training and/or self-taught in over 15 languages. Shrug,
>so what.
Yup. Why have a bunch of different formats for what is about the
same thing?
>However I religiously use the conventions of the language I am writing
>in. Amongst other things it allows future maintenance programmers to
>read the code without needing to do mental gymnastics to convert code
>style before they can understand what the code is doing.
>
>This is important if you ever work in a professional environment. Your
Did you read the bit where I said that I have been doing so for
about twenty-five years? You quoted it.
>code WILL be looked at by someone else sometime in the future.
Could be. I will follow such conventions as have been set.
>The lone programmer (consultant or hired) writing obfuscated code to
>their own personal style/standards would not be tolerated in any of the
>companies I have worked for.
My code is not obfuscated. Sometimes, I am the one setting the
standard by right of being the only progammer. If not, I ask for the
standards and then follow them.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Wojtek <nowhere@a.com> |
|---|---|
| Date | 2011-06-14 00:40 -0700 |
| Message-ID | <mn.70287db69ce569c5.70216@a.com> |
| In reply to | #5283 |
Gene Wirchenko wrote : > On Sat, 11 Jun 2011 16:35:03 -0700, Wojtek <nowhere@a.com> wrote: > Yup. Why have a bunch of different formats for what is about the > same thing? Hmm, this statement has a slight disconnect with this statement: > Could be. I will follow such conventions as have been set. > My code is not obfuscated. Sometimes, I am the one setting the > standard by right of being the only progammer. If not, I ask for the > standards and then follow them. If you are the originator of a language then you can set whatever standards (um, conventions) you want. And others will follow them to keep consistent with you. But if you are coding in a generally used language then it behooves you to follow the generally accepted conventions. To do otherwise will confuse some future maintenance programmer and will also present your work as amateurish. Visual Basic by convention capitalizes function names. Java by convention does not. And K&R C and PHP do not use camel case. VB: public function DoMyWork() as String Java: public string doMyWork() C: char* do_my_work() PHP: function do_my_work() I find all of these readable, however I would NEVER mix the different ways of naming a method. There is one thing that has pretty well been consistent over the years. ALL the code I have ever written HAS been looked at by someone else, no matter how off-the-cuff and temporary I thought it was when I wrote it. And since I have had many juniors working for me, it is my responsibility to teach them the accepted way of presenting code in whatever language I happen to be using at the time. No matter how much they think it is not necessary. A convention is just that. A generally accepted way of doing things. -- Wojtek :-)
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
|---|---|
| Date | 2011-06-11 21:18 +0000 |
| Message-ID | <slrniv7mt6.phi.avl@gamma.logic.tuwien.ac.at> |
| In reply to | #5182 |
Stefan Ram <ram@zedat.fu-berlin.de> wrote: > This is just the realization of »Tell, Don't Ask«. > Here, one asks the object: > if( getter.isValid() )... > Here, one tells the object what to do: > new StringGetter().get( [string|...], [report|...] ); If you'd like coding like that, you *might* like Tcl. Passing scriptlets to commands to have them executed (conditionally, repeatedly, deferredly, just later, within a different context, ...) is just simply normal in Tcl. Even the "if" statement itself is merely a command that gets passed scriptlets and executes one (or maybe even none - if no condition is true and no "else"-scriptlet given) ...
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-06-10 08:43 -0700 |
| Message-ID | <iste3e$68k$1@dont-email.me> |
| In reply to | #5168 |
On 6/9/2011 11:03 PM, Gene Wirchenko wrote:
> Dear Java'ers:
>
> I wish to call by result with a method. Is it possible? If not,
> can it be easily simulated in an unnasty way?
>
> I am writing a simple preprocessor. I have a few spots where a
> string needs to be parsed. I want to call something like this:
> String ReturnString="";
> boolean DidItWork=GetString(ReturnString);
> if (!DidItWork)
> // too bad
> It is not acceptable to have a special String value mean failure. I
> want the method to be able to return any potential string.
I'm kind of surprised by all the answers here also. I'm glad Stefan
pointed out the definition of "call by result," which I also did not know.
However call-by-result is just a use case of pass-by-reference, so why
not use that? In Java the standard quicky semantic is to use an array.
String[] returnString = new String[1]; // just 1 return string
boolean didItWork = GetString( returnString );
if( didItWork ) {
String result = returnString[0];
.... process result here
} else {
.... too bad
}
returnString is just a holder, as was mentioned earlier, but temporary
arrays are often used for pass by reference in Java (which only has pass
by value), so any programmer is likely to grasp what is going on here
quickly.
[toc] | [prev] | [next] | [standalone]
| From | RedGrittyBrick <RedGrittyBrick@spamweary.invalid> |
|---|---|
| Date | 2011-06-10 17:01 +0100 |
| Message-ID | <4df23fe3$0$12157$fa0fcedb@news.zen.co.uk> |
| In reply to | #5168 |
On 10/06/2011 07:03, Gene Wirchenko wrote:
> Dear Java'ers:
>
> I wish to call by result with a method. Is it possible?
No, strings are immutable, so GetString() cannot alter the value of
ReturnString.
> If not, can it be easily simulated in an unnasty way?
I believe it is better to use a Java pattern which other Java
programmers will be familiar with.
>
> I am writing a simple preprocessor. I have a few spots where a
> string needs to be parsed. I want to call something like this:
> String ReturnString="";
> boolean DidItWork=GetString(ReturnString);
> if (!DidItWork)
> // too bad
> It is not acceptable to have a special String value mean failure. I
> want the method to be able to return any potential string.
>
I think Stephan Ram's answer is roughly what I would do.
String returnString;
Thagomizor thangomizor = new Thagomizor();
thangomizor.thangomize();
if (!thangomizor.didItWork()) {
throw new TooBadException();
} else {
returnString = thangomizor.getString();
}
--
RGB
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2011-06-10 16:55 +0000 |
| Message-ID | <istiae$gj7$1@localhost.localdomain> |
| In reply to | #5186 |
On Fri, 10 Jun 2011 17:01:36 +0100, RedGrittyBrick wrote:
> I think Stephan Ram's answer is roughly what I would do.
>
> String returnString;
> Thagomizor thangomizor = new Thagomizor(); thangomizor.thangomize();
> if (!thangomizor.didItWork()) {
> throw new TooBadException();
> } else {
> returnString = thangomizor.getString();
> }
Likewise, this is my usual style too, though its quite possibly a carry-
over from the way I write C.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-06-10 11:59 -0700 |
| Message-ID | <baq4v6dca53n4ag3lt96434vji6s70rikn@4ax.com> |
| In reply to | #5186 |
On Fri, 10 Jun 2011 17:01:36 +0100, RedGrittyBrick
<RedGrittyBrick@spamweary.invalid> wrote:
>On 10/06/2011 07:03, Gene Wirchenko wrote:
[snip]
>> I am writing a simple preprocessor. I have a few spots where a
>> string needs to be parsed. I want to call something like this:
>> String ReturnString="";
>> boolean DidItWork=GetString(ReturnString);
>> if (!DidItWork)
>> // too bad
>> It is not acceptable to have a special String value mean failure. I
>> want the method to be able to return any potential string.
>I think Stephan Ram's answer is roughly what I would do.
>
>String returnString;
>Thagomizor thangomizor = new Thagomizor();
>thangomizor.thangomize();
>if (!thangomizor.didItWork()) {
> throw new TooBadException();
>} else {
> returnString = thangomizor.getString();
>}
Lengthy code (compared with what it could be) to no additional
benefit.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Wojtek <nowhere@a.com> |
|---|---|
| Date | 2011-06-11 16:24 -0700 |
| Message-ID | <mn.5bd87db6bff699fd.70216@a.com> |
| In reply to | #5200 |
Gene Wirchenko wrote :
>> I think Stephan Ram's answer is roughly what I would do.
>>
>> String returnString;
>> Thagomizor thangomizor = new Thagomizor();
>> thangomizor.thangomize();
>> if (!thangomizor.didItWork()) {
>> throw new TooBadException();
>> } else {
>> returnString = thangomizor.getString();
>> }
>
> Lengthy code (compared with what it could be) to no additional
> benefit.
The benefit is that it is instantly recognizable by any competent Java
programmer as to what it does and how any problem is handled.
If you want minimal source code size, then Java is not for you.
--
Wojtek :-)
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-06-12 21:59 -0700 |
| Message-ID | <o56bv6t60gg8gji407u6tirhf6sorhm2gp@4ax.com> |
| In reply to | #5227 |
On Sat, 11 Jun 2011 16:24:39 -0700, Wojtek <nowhere@a.com> wrote:
>Gene Wirchenko wrote :
>>> I think Stephan Ram's answer is roughly what I would do.
>>>
>>> String returnString;
>>> Thagomizor thangomizor = new Thagomizor();
>>> thangomizor.thangomize();
>>> if (!thangomizor.didItWork()) {
>>> throw new TooBadException();
>>> } else {
>>> returnString = thangomizor.getString();
>>> }
>>
>> Lengthy code (compared with what it could be) to no additional
>> benefit.
>
>The benefit is that it is instantly recognizable by any competent Java
>programmer as to what it does and how any problem is handled.
Mine is also recognisable, etc.
>
>If you want minimal source code size, then Java is not for you.
I do not. What I want is reasonable code size. Such segments as
the above are not too bad if there are only a few, but make it half a
dozen, say, and suddenly a short method is taking up most of a page.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Paul Cager <paul.cager@googlemail.com> |
|---|---|
| Date | 2011-06-13 07:53 -0700 |
| Message-ID | <2b5e9cc3-e7c8-490b-bd9f-f91f1220751b@d1g2000yqm.googlegroups.com> |
| In reply to | #5284 |
On Jun 13, 5:59 am, Gene Wirchenko <ge...@ocis.net> wrote:
> What I want is reasonable code size. Such segments as
> the above are not too bad if there are only a few, but make it half a
> dozen, say, and suddenly a short method is taking up most of a page.
I'm not sure I understand the problem here.
I think what you want to be able to write (but Java won't let you) is
this:
String ReturnString="";
boolean DidItWork=GetString(ReturnString);
if (!DidItWork) {
handleErrorOrThrowException();
} else {
doSomethingWithString();
}
Patricia and others have suggested that the idiomatic way in Java to
do it is to use an exception like this:
try {
doSomethingWith(getString());
} catch (SomeErrorOccuredException e) {
handleErrorOrThrowException();
}
That looks perfectly reasonable to me.
[toc] | [prev] | [next] | [standalone]
| From | Wojtek <nowhere@a.com> |
|---|---|
| Date | 2011-06-14 00:43 -0700 |
| Message-ID | <mn.702b7db652db03d7.70216@a.com> |
| In reply to | #5284 |
Gene Wirchenko wrote :
> On Sat, 11 Jun 2011 16:24:39 -0700, Wojtek <nowhere@a.com> wrote:
>
>> Gene Wirchenko wrote :
>>>> I think Stephan Ram's answer is roughly what I would do.
>>>>
>>>> String returnString;
>>>> Thagomizor thangomizor = new Thagomizor();
>>>> thangomizor.thangomize();
>>>> if (!thangomizor.didItWork()) {
>>>> throw new TooBadException();
>>>> } else {
>>>> returnString = thangomizor.getString();
>>>> }
>>>
>>> Lengthy code (compared with what it could be) to no additional
>>> benefit.
>>
>> The benefit is that it is instantly recognizable by any competent Java
>> programmer as to what it does and how any problem is handled.
>
> Mine is also recognisable, etc.
There is a difference between (glance, ok) and (glance, read, consider,
ok).
--
Wojtek :-)
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-06-10 11:55 -0700 |
| Message-ID | <8jp4v6pe7pfin30rr27attfk2i8bus0fg3@4ax.com> |
| In reply to | #5168 |
On 10 Jun 2011 13:47:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:
>Gene Wirchenko <genew@ocis.net> writes:
>>I wish to call by result with a method.
[snip]
>http://en.wikipedia.org/wiki/Evaluation_strategy
>
> Did you all knew this before you answered? The reference
> has to be /uninitialized/ for an evaluation strategy to be
> called "call by result"!
The various call types are not exotic. If you do not know COLD
the terms call-by-reference, call-by-value, call-by-result, and
call-by-value-result (also known as copy-restore), you have a bit of
studying to do. Well, call-by-name is somewhat exotic, but it is
thought to have been a bit of an accident.
>>String ReturnString="";
>>boolean DidItWork=GetString(ReturnString);
>
> But in Java we cannot pass uninitialized references!
A weakness. My UNfavourite Java compilation error message is
that the variable might not have been initialised in code where it
does not matter.
> Especially, in this case, the reference is not uninitialized.
I have to do that to kowtow to Java.
[snip]
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Date | 2011-06-11 09:09 +0100 |
| Message-ID | <1ekbc8-ug2.ln1@news.simpsonst.f2s.com> |
| In reply to | #5168 |
On 10/06/11 07:03, Gene Wirchenko wrote:
> I am writing a simple preprocessor. I have a few spots where a
> string needs to be parsed. I want to call something like this:
> String ReturnString="";
> boolean DidItWork=GetString(ReturnString);
> if (!DidItWork)
> // too bad
I don't think the following suggestion has been covered exactly. Split
GetString() into two methods: the first testing for the string, storing
it in the containing class, and returning true or false; the second
simply retrieving the last string.
I'm assuming that GetString is non-static for this.
class Parser {
/**
* Attempt to parse a string. If successful, the result
* is obtained from {@link #getLastString()}.
*
* @return true if a string was parsed
*/
public boolean parseString() { ... }
public String getLastString() { ... }
}
if (parser.parseString()) {
doSomethingWith(parser.getLastString());
}
[toc] | [prev] | [next] | [standalone]
| From | Cholo Lennon <chololennon@hotmail.com> |
|---|---|
| Date | 2011-06-16 11:30 -0300 |
| Message-ID | <itd41d$pjf$1@speranza.aioe.org> |
| In reply to | #5168 |
On 10/06/2011 03:03, Gene Wirchenko wrote:
> Dear Java'ers:
>
> I wish to call by result with a method. Is it possible? If not,
> can it be easily simulated in an unnasty way?
>
> I am writing a simple preprocessor. I have a few spots where a
> string needs to be parsed. I want to call something like this:
> String ReturnString="";
> boolean DidItWork=GetString(ReturnString);
> if (!DidItWork)
> // too bad
> It is not acceptable to have a special String value mean failure. I
> want the method to be able to return any potential string.
>
> Sincerely,
>
> Gene Wirchenko
IMHO using exceptions is not always the best answer to this problem (as
other suggested).
How about using a simple generic class? (of course, it could be
corrected or improved. A similar idea could be found in other languages
(such as C++ (boost)):
class Optional<T> {
private T value;
private boolean valid;
public Optional(T value) {
setValue(value);
}
public Optional() {
this.valid = false;
}
public boolean isValid() {
return this.valid;
}
public T getValue() {
return this.value;
}
public void setValue(T value) {
this.value = value;
// TODO Shall I set to true when value is null?
this.valid = true;
}
public void invalidate() {
this.value = null;
this.valid = false;
}
}
// Test
Optional<String> result = GetString();
if (!result.isValid()) {
// too bad
...
}
else {
System.out.print(result.getValue());
...
}
Regards
--
Cholo Lennon
Bs.As.
ARG
[toc] | [prev] | [next] | [standalone]
| From | Paul Cager <paul.cager@googlemail.com> |
|---|---|
| Date | 2011-06-17 02:38 -0700 |
| Message-ID | <67cda338-2802-40fa-a8e5-fcd1fe8ca5e0@gc3g2000vbb.googlegroups.com> |
| In reply to | #5334 |
On Jun 16, 3:30 pm, Cholo Lennon <chololen...@hotmail.com> wrote: > IMHO using exceptions is not always the best answer to this problem (as > other suggested). > Not always, no. But in this case there are hints that the OP regarded the lack of a String as an error condition - he used phrases such as "mean failure" and "DidItWork". In that case I think an exception is appropriate (and less work than many of the other suggestions!). I believe that cases where neither of the following two options are appropriate are relatively rare: - returning null, meaning the "thing" was not found, but that's only to be expected. - throwing an exception, meaning that you can't return anything because an error happened.
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-06-17 12:06 -0700 |
| Message-ID | <319nv61qjsrkc74gup33l514mtc3crj2m1@4ax.com> |
| In reply to | #5340 |
On Fri, 17 Jun 2011 02:38:34 -0700 (PDT), Paul Cager
<paul.cager@googlemail.com> wrote:
>On Jun 16, 3:30 pm, Cholo Lennon <chololen...@hotmail.com> wrote:
>> IMHO using exceptions is not always the best answer to this problem (as
>> other suggested).
>Not always, no. But in this case there are hints that the OP regarded
>the lack of a String as an error condition - he used phrases such as
>"mean failure" and "DidItWork". In that case I think an exception is
>appropriate (and less work than many of the other suggestions!).
It would be an error in the contexts that I was thinking of, but
that does not make exceptions the way to go.
I now also have a method for getting the next non-blank, non-tab
character from a line. A character being found is correct when
parsing
$quotes "" '' [] ~
and picking up the quotes characters. However, I also use this method
to check for characters after the logical statement end, and in this
case, finding such a character is an error as when parsing
$include "testin.dat" x
(the "x" being wrong).
I do not like the idea that I might have the normal flow of
execution going through a catch block.
[snip]
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | lewbloch <lewbloch@gmail.com> |
|---|---|
| Date | 2011-06-17 06:40 -0700 |
| Message-ID | <d8102399-7dd6-44dc-9819-c222bbf28653@h12g2000pro.googlegroups.com> |
| In reply to | #5334 |
On Jun 16, 7:30 am, Cholo Lennon <chololen...@hotmail.com> wrote:
> On 10/06/2011 03:03, Gene Wirchenko wrote:
>
>
>
>
>
>
>
>
>
> > Dear Java'ers:
>
> > I wish to call by result with a method. Is it possible? If not,
> > can it be easily simulated in an unnasty way?
>
> > I am writing a simple preprocessor. I have a few spots where a
> > string needs to be parsed. I want to call something like this:
> > String ReturnString="";
> > boolean DidItWork=GetString(ReturnString);
> > if (!DidItWork)
> > // too bad
> > It is not acceptable to have a special String value mean failure. I
> > want the method to be able to return any potential string.
>
> > Sincerely,
>
> > Gene Wirchenko
>
> IMHO using exceptions is not always the best answer to this problem (as
> other suggested).
>
> How about using a simple generic class? (of course, it could be
> corrected or improved. A similar idea could be found in other languages
> (such as C++ (boost)):
>
> class Optional<T> {
> private T value;
> private boolean valid;
>
> public Optional(T value) {
> setValue(value);
It's dangerous to call an overridable method from a constructor.
> }
>
> public Optional() {
> this.valid = false;
Redundant assignment, but arguably it adds clarity. I don't think it
does, but one could argue that it does.
> }
>
> public boolean isValid() {
> return this.valid;
> }
>
> public T getValue() {
> return this.value;
> }
>
> public void setValue(T value) {
> this.value = value;
> // TODO Shall I set to true when value is null?
> this.valid = true;
> }
>
> public void invalidate() {
> this.value = null;
> this.valid = false;
> }
>
> }
>
The original post establishes 'null' as an out-of-band value. Your
code hints at that possibility in a comment, but doesn't enforce the
requirement. If 'null' is out of band, then the boolean variable is
redundant and duplicative. If 'null' is in band, then the boolean
makes more sense, as it would have to to justify the complexity.
--
Lew
[toc] | [prev] | [next] | [standalone]
Page 4 of 5 — ← Prev page 1 2 3 [4] 5 Next page →
Back to top | Article view | comp.lang.java.programmer
csiph-web