We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Boolean's definitions in 2 approaches (newbie)
Page Index Toggle Pages: 1
Boolean's definitions in 2 approaches (newbie) (Read 610 times)
Boolean's definitions in 2 approaches (newbie)
Jul 12th, 2009, 1:21am
 
  Hi guys! Could you help me with explanation of simple question about booleans two different approaches:
1)  public boolean truth = false;  // declare
                        ...
    if( ... ){ truth = true ;}          // conditions for truth
                        ...
    if(truth=true){ ... }               // use

2) public boolean truth() {         // declare
   return truth;
 }
                        ???                 // use
                        ???                 // conditions

I can't cath up the 2nd   Undecided
Re: Boolean's definitions in 2 approaches (newbie)
Reply #1 - Jul 12th, 2009, 2:44am
 
Note: some people do tests like if (bIsActive == true) but I find it redundant. I prefer to write: if (bIsActive) (but others might disagree).

Basically, you wonder about the difference between a global (or more exactly public class level) variable and the usage of getters and setters. Except you don't have a setter in your question...
Ie. you are supposed to provide both setTruth() and getTruth() (although for boolean returns, there is the more common form isTruth()).

Most articles and books on object oriented programming will advice to use the second form. It is called encapsulation: hiding implementation details behind a public API. That API is a kind of contract, people using your code (including yourself!) is guaranteed that these functions won't change (or if it does, implementers will warn you, and take some other precautions) even if the implementation changes (eg. replacing return truth; by return Test() || x >= 0;).

Now, Processing sketches are often small, so the first approach is generally OK. Unless you make a library... Smiley

To answer your question, if you use accessors, it should look like:

if( ... ) { setTruth(true); }          // conditions for truth
if (isTruth()) { ... }               // use


The two functions are quite trivial, I don't give them...
Re: Boolean's definitions in 2 approaches (newbie)
Reply #2 - Jul 12th, 2009, 1:43pm
 
 OK, I understood... Also it's hard to use lots of booleans and do not forget to watch thier state and change it.
 So what kind of "accessors"  (if i'm right saying that), in your opinion, is more better to use for large-size sketches? There is only one I know is "public void dosmth" (besides the booleans)...
Re: Boolean's definitions in 2 approaches (newbie)
Reply #3 - Jul 12th, 2009, 1:52pm
 
  Forget to say.
If I use:

bollean b1, b2, b3, b4, b5, b..., bn;
                   ...
if(b1){
         if(b2){
                   if(b3){
        do 1st thing...;
}}}
if(b1){
         if(b2){
                   if(!b3){
        do 2nd thing...;
}}}
     What could you say about that?
Re: Boolean's definitions in 2 approaches (newbie)
Reply #4 - Jul 12th, 2009, 2:38pm
 
I am not sure I fully understood your question.
When I see your last example, I am tempted to say: use array of booleans.
Unless these are actually named booleans, like bChangeDirection, bIsDragging, bHasHit, bReverse, etc.

Also:
if (b1) { if (b2) { if (b3) { DoStuff(); } } }
can be written:
if (b1 && b2 && b3) { DoStuff(); }
Re: Boolean's definitions in 2 approaches (newbie)
Reply #5 - Jul 13th, 2009, 12:17am
 
So what kind of "accessors"  (if i'm right saying that), in your opinion, is more better to use for large-size sketches? There is only one I know is "public void dosmth" (besides the booleans)...
  I mean what is better to use instead of boolean variables when there are lots of conditions and functions?
  I'm intresting because you said: "The two functions are quite trivial, I don't give them..."
Re: Boolean's definitions in 2 approaches (newbie)
Reply #6 - Jul 13th, 2009, 12:46am
 
Trivial functions:
Code:
void setTruth(boolean b) { truth = b; }
boolean isTruth() { return truth; }


"what is better to use instead of boolean variables when there are lots of conditions and functions?"
I am not sure if there is a better method. It sure depends on your exact use case, I don't what usage you do of your booleans.
It might be some complex logic which need to be written manually, or something generic that can be solved with arrays and loops.
Re: Boolean's definitions in 2 approaches (newbie)
Reply #7 - Jul 13th, 2009, 1:33am
 
  Thanks a lot! It's more clear now   Smiley.
Page Index Toggle Pages: 1