Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Style guide question

 
Chuck Holowecky
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Chuck Holowecky
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right on. isLeapYear returns the single value that ( y400 || (y4 && ! y100) ) is evaluated to.

 
Vladimir Nesov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's often good to return expression, especially instead of code like

[ June 29, 2006: Message edited by: Vladimir Nesov ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic