• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Style guide question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) );
}
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right on. isLeapYear returns the single value that ( y400 || (y4 && ! y100) ) is evaluated to.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic