File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Style guide question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Style guide question" Watch "Style guide question" New topic
Author

Style guide question

Chuck Holowecky
Greenhorn

Joined: Feb 09, 2006
Posts: 17
On the last section of the style guide there is an example of a method called isLeapYear. It returns a boolean value. Instead of returning a variable with a boolean value, one can insert an expression that evaluates to a boolean value?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

I'm not sure what you mean by your question, could you write up (using the example) what you're thinking of?

As a side note, I tend to think of complex boolean operations in smaller pieces such as "is the value within limits" = (x < 100 && x > 0), "is the value a multiple of 3" = ((x % 3) == 0), etc.

So I would write plain statements like:
boolean isXWithinBoundary = x < 100 && x > 0;
boolean isXAMultipleOf3 = (x % 3) == 0;

The point of the example is that often times in code you have to debug why something returned true/false when you expected it to return the opposite. Its much easier from a debugging perspective if you have each "rule" listed as a separate boolean so you know which rule failed.


My Blog: Down Home Country Coding with Scott Selikoff
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Chuck Holowecky:
Instead of returning a variable with a boolean value, one can insert an expression that evaluates to a boolean value?


I'm not really clear on the question, but I think you are asking if you can assign the result to a variable rather than returning the result. The answer to that is "yes".


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Chuck Holowecky
Greenhorn

Joined: Feb 09, 2006
Posts: 17
This is the section of code that I'm refering to. It seems odd to me to pass a return value that is an expression, instead of a single value or variable. I'm assume that the expression is evaluated and then the results (true/false) are passed back to whoever call the method.


boolean isLeapYear(int year)
{
boolean y4 = ( (year % 4) == 0 );
boolean y100 = ( (year % 100) == 0 );
boolean y400 = ( (year % 400) == 0 );
return ( y400 || (y4 && ! y100) );
}
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
You're right on. isLeapYear returns the single value that ( y400 || (y4 && ! y100) ) is evaluated to.

Vladimir Nesov
Greenhorn

Joined: Jun 18, 2006
Posts: 20
It's often good to return expression, especially instead of code like

[ June 29, 2006: Message edited by: Vladimir Nesov ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Style guide question