Prev: Open source licensing being questioned by anti-copyrighttypes
Next: Suggession for your website
From: Rhino on 28 May 2010 19:02 I'm trying something Eric Sosman suggested on another thread but I have doubts about one of the idioms so I thought I'd check here and see if there was a better way. A method name pad() returns a special class called PadResult. PadResult is defined as follows: import CompletionStatus; /** * Simple holder class contains the result of invoking the pad() method. * * <p>If the pad() method encounters no errors along the way, it returns a CompletionStatus set to NO_ERROR and a String containing the * result of the padding of the input value. If the pad() method encounters trouble along the way, it returns a CompletionStatus of ERROR * and an error message.</p> */ public class PadResult { CompletionStatus status; String result; public PadResult(CompletionStatus status, String result) { this.status = status; this.result = result; } public CompletionStatus getStatus() { return status; } public String getResult() { return result; } } And here is the enum CompletionStatus: /** * This enum contains a list of possible completion statuses for methods. */ public enum CompletionStatus { /** Constant for ERROR. */ ERROR, /** Constant for NO_ERROR */ NO_ERROR; } The method is then invoked as follows: PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy with trailing blanks and make the final result 3 characters long Since this should fail because the requested final length would truncate the input string, pad() will return CompletionStatus.ERROR and an error message via PadResult. I'm checking the value of the CompletionStatus as follows: if (padResult.getStatus()==CompletionStatus.ERROR) { System.out.println(padResult.getResult()); } It's the if statement that concerns me here. This code works fine (I can test for CompletionStatus.NO_ERROR by simply changlng the right side of the if) but it doesn't look right. It reminds me of mistakes I made when I was new to Java comparing Strings to each other via the == operator to see if they had the same VALUE but discovering that == doesn't determine equality of value. I've tried using a switch() statement - switch(padResult.getStatus()) { case NO_ERROR: System.out.println("The padded value is " + padResult.getResult()); break; default: System.err.println("Error!! --> " + padResult.getResult()); break; } That also works but is a little long for my tastes. Is there a better way to do this if statement? If so, what is it?
From: Jeff Higgins on 28 May 2010 19:13 On 5/28/2010 7:02 PM, Rhino wrote: > I'm trying something Eric Sosman suggested on another thread but I have > doubts about one of the idioms so I thought I'd check here and see if there > was a better way. > > A method name pad() returns a special class called PadResult. PadResult is > defined as follows: > > import CompletionStatus; > > > /** > > * Simple holder class contains the result of invoking the pad() method. > > * > > *<p>If the pad() method encounters no errors along the way, it returns a > CompletionStatus set to NO_ERROR and a String containing the > > * result of the padding of the input value. If the pad() method encounters > trouble along the way, it returns a CompletionStatus of ERROR > > * and an error message.</p> > > */ > > public class PadResult { > > > CompletionStatus status; > > String result; > > > public PadResult(CompletionStatus status, String result) { > > > this.status = status; > > this.result = result; > > } > > > public CompletionStatus getStatus() { > > > return status; > > } > > > public String getResult() { > > > return result; > > } > > } > > > > And here is the enum CompletionStatus: > > > > /** > > * This enum contains a list of possible completion statuses for methods. > > */ > > public enum CompletionStatus { > > > /** Constant for ERROR. */ > > ERROR, > > > /** Constant for NO_ERROR */ > > NO_ERROR; > > > } > > > > > > The method is then invoked as follows: > > PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy with > trailing blanks and make the final result 3 characters long > > Since this should fail because the requested final length would truncate the > input string, pad() will return CompletionStatus.ERROR and an error message > via PadResult. > > I'm checking the value of the CompletionStatus as follows: > > > > if (padResult.getStatus()==CompletionStatus.ERROR) { > > System.out.println(padResult.getResult()); > > } > > > > It's the if statement that concerns me here. This code works fine (I can > test for CompletionStatus.NO_ERROR by simply changlng the right side of the > if) but it doesn't look right. It reminds me of mistakes I made when I was > new to Java comparing Strings to each other via the == operator to see if > they had the same VALUE but discovering that == doesn't determine equality > of value. > > I've tried using a switch() statement - > > switch(padResult.getStatus()) { > > case NO_ERROR: > > System.out.println("The padded value is " + padResult.getResult()); > > break; > > default: > > System.err.println("Error!! --> " + padResult.getResult()); > > break; > > } > > That also works but is a little long for my tastes. > > > > Is there a better way to do this if statement? If so, what is it? > <http://www.informit.com/articles/article.aspx?p=433387>
From: Arne Vajhøj on 28 May 2010 20:08 On 28-05-2010 19:02, Rhino wrote: > I'm trying something Eric Sosman suggested on another thread but I have > doubts about one of the idioms so I thought I'd check here and see if there > was a better way. > > A method name pad() returns a special class called PadResult. PadResult is > defined as follows: > public class PadResult { > > > CompletionStatus status; > > String result; The accessability level of package does not seem right. > public enum CompletionStatus { > > > /** Constant for ERROR. */ > > ERROR, > > > /** Constant for NO_ERROR */ > > NO_ERROR; > > > } > > The method is then invoked as follows: > > PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy with > trailing blanks and make the final result 3 characters long > > Since this should fail because the requested final length would truncate the > input string, pad() will return CompletionStatus.ERROR and an error message > via PadResult. > > I'm checking the value of the CompletionStatus as follows: > > if (padResult.getStatus()==CompletionStatus.ERROR) { > > System.out.println(padResult.getResult()); > > } > > It's the if statement that concerns me here. This code works fine (I can > test for CompletionStatus.NO_ERROR by simply changlng the right side of the > if) but it doesn't look right. It reminds me of mistakes I made when I was > new to Java comparing Strings to each other via the == operator to see if > they had the same VALUE but discovering that == doesn't determine equality > of value. > Is there a better way to do this if statement? If so, what is it? I would compare with CompletionStatus.NO_ERROR, because it is a lot more likely that you will have more than one error status than more than one no error status. I would seriously consider instead of the enum to have a boolean plus a String, because true/false and a textual explanation is really what you need. If an error is something that warrants an exception then you could simply the code by using an exception. Some error situations could even warrant an uncheck exception (deriving from RuntimException). Arne
From: Rhino on 28 May 2010 20:43 "Arne Vajh�j" <arne(a)vajhoej.dk> wrote in message news:4c005ae9$0$281$14726298(a)news.sunsite.dk... > On 28-05-2010 19:02, Rhino wrote: >> I'm trying something Eric Sosman suggested on another thread but I have >> doubts about one of the idioms so I thought I'd check here and see if >> there >> was a better way. >> >> A method name pad() returns a special class called PadResult. PadResult >> is >> defined as follows: > >> public class PadResult { >> >> >> CompletionStatus status; >> >> String result; > > The accessability level of package does not seem right. > > >> public enum CompletionStatus { >> >> >> /** Constant for ERROR. */ >> >> ERROR, >> >> >> /** Constant for NO_ERROR */ >> >> NO_ERROR; >> >> >> } >> >> The method is then invoked as follows: >> >> PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy >> with >> trailing blanks and make the final result 3 characters long >> >> Since this should fail because the requested final length would truncate >> the >> input string, pad() will return CompletionStatus.ERROR and an error >> message >> via PadResult. >> >> I'm checking the value of the CompletionStatus as follows: >> >> if (padResult.getStatus()==CompletionStatus.ERROR) { >> >> System.out.println(padResult.getResult()); >> >> } >> >> It's the if statement that concerns me here. This code works fine (I can >> test for CompletionStatus.NO_ERROR by simply changlng the right side of >> the >> if) but it doesn't look right. It reminds me of mistakes I made when I >> was >> new to Java comparing Strings to each other via the == operator to see if >> they had the same VALUE but discovering that == doesn't determine >> equality >> of value. > >> Is there a better way to do this if statement? If so, what is it? > > I would compare with CompletionStatus.NO_ERROR, because it is > a lot more likely that you will have more than one error status > than more than one no error status. > Okay, that's fair. But am I writing the comparison correctly? That == operator looks wrong somehow, even if it works. I'm concerned that this might be like comparing two Strings with the == operator; sometimes it will show equality and make you think it is confirming equality of value between the two strings but it is not; you have to use equals() to compare String values. But I'm not sure what the equivalent is for an enum. > I would seriously consider instead of the enum to have a boolean > plus a String, because true/false and a textual explanation is > really what you need. > > If an error is something that warrants an exception then > you could simply the code by using an exception. > > Some error situations could even warrant an uncheck > exception (deriving from RuntimException). > I'm more or less satisfied that the situations I'm trying to handle right now don't warrant exceptions so I'm trying to work out the coding without using exceptions. (I've actually got code that throws and catches exceptions nailed down.) Thanks for your help, Arne! -- Rhino
From: Rhino on 28 May 2010 20:54 "Jeff Higgins" <oohiggins(a)yahoo.com> wrote in message news:htpioj$rau$1(a)news.eternal-september.org... > On 5/28/2010 7:02 PM, Rhino wrote: >> I'm trying something Eric Sosman suggested on another thread but I have >> doubts about one of the idioms so I thought I'd check here and see if >> there >> was a better way. >> >> A method name pad() returns a special class called PadResult. PadResult >> is >> defined as follows: >> >> import CompletionStatus; >> >> >> /** >> >> * Simple holder class contains the result of invoking the pad() method. >> >> * >> >> *<p>If the pad() method encounters no errors along the way, it returns a >> CompletionStatus set to NO_ERROR and a String containing the >> >> * result of the padding of the input value. If the pad() method >> encounters >> trouble along the way, it returns a CompletionStatus of ERROR >> >> * and an error message.</p> >> >> */ >> >> public class PadResult { >> >> >> CompletionStatus status; >> >> String result; >> >> >> public PadResult(CompletionStatus status, String result) { >> >> >> this.status = status; >> >> this.result = result; >> >> } >> >> >> public CompletionStatus getStatus() { >> >> >> return status; >> >> } >> >> >> public String getResult() { >> >> >> return result; >> >> } >> >> } >> >> >> >> And here is the enum CompletionStatus: >> >> >> >> /** >> >> * This enum contains a list of possible completion statuses for methods. >> >> */ >> >> public enum CompletionStatus { >> >> >> /** Constant for ERROR. */ >> >> ERROR, >> >> >> /** Constant for NO_ERROR */ >> >> NO_ERROR; >> >> >> } >> >> >> >> >> >> The method is then invoked as follows: >> >> PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy >> with >> trailing blanks and make the final result 3 characters long >> >> Since this should fail because the requested final length would truncate >> the >> input string, pad() will return CompletionStatus.ERROR and an error >> message >> via PadResult. >> >> I'm checking the value of the CompletionStatus as follows: >> >> >> >> if (padResult.getStatus()==CompletionStatus.ERROR) { >> >> System.out.println(padResult.getResult()); >> >> } >> >> >> >> It's the if statement that concerns me here. This code works fine (I can >> test for CompletionStatus.NO_ERROR by simply changlng the right side of >> the >> if) but it doesn't look right. It reminds me of mistakes I made when I >> was >> new to Java comparing Strings to each other via the == operator to see if >> they had the same VALUE but discovering that == doesn't determine >> equality >> of value. >> >> I've tried using a switch() statement - >> >> switch(padResult.getStatus()) { >> >> case NO_ERROR: >> >> System.out.println("The padded value is " + padResult.getResult()); >> >> break; >> >> default: >> >> System.err.println("Error!! --> " + padResult.getResult()); >> >> break; >> >> } >> >> That also works but is a little long for my tastes. >> >> >> >> Is there a better way to do this if statement? If so, what is it? >> > <http://www.informit.com/articles/article.aspx?p=433387> Interesting article; I wonder if we might call it a "poor man's version" of the Robust Java book you mentioned earlier :-) The first page is the most interesting for the situation I am trying to code. He seems to be assuming (in the third example of page 1) that chargeCustomerCard(variable1, variable2) is returning a boolean and nothing else. That's fine and I understand the example for that case. But what about the case where the method is returning something else? For instance, my pad() method's main output is the string in the first input parameter, padded out according to instructions in the other parameters. The initial version of pad() returned only the padded string or threw an exception if there were problems with the input parameters. But if I'm not going to throw an exception and _do_ want to return an error message (or even a message number that could be looked up), I tend to need something like this PadResult class that I've concocted for this thread so that I can pass back either an "it worked" and a padded String or an "it failed" and an error message. What do you think? Is PadResult or something like it my best alternative here, assuming we agree that an exception is overkill for a case where an input parameter is just miscoded? -- Rhino
|
Next
|
Last
Pages: 1 2 3 4 5 6 7 Prev: Open source licensing being questioned by anti-copyrighttypes Next: Suggession for your website |