Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Cattle Drive and the fly likes negative booleaan again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "negative booleaan again" Watch "negative booleaan again" New topic
Author

negative booleaan again

Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
Busy with SortedNames TextFileIn suggests
* TextFileIn in = new TextFileIn("file.txt");
* boolean done = false ;
* while ( ! done )
etc.
If you do not like this (as you do ) change done into
boolean goOn = true ;
while ( goOn )
{...
Reason to think about avoiding negative booleans again?
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

I could never think of an identifier that I liked as well. So I suffer through adding the negation.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
I agree. But the consequence is what should a greenhorn or ranchhand do if he likes a 'negative' way of expressing himself?
Ok, here we are kindly requested to follow the nitpickers meaning aren't we ?
pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
How about this?
boolean done = false;
while ( done = false )
{
...
}
It preserves positive logic, is easy to read and gets around the naming problem. This is how I was taught back in my mainframe days, and IMHO is still useful.
Hope this helps.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
<pre>
boolean done = false;
while ( done = false )
{
...
}
</pre>

I think you mean
while ( done == false )
Right?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
Right Marilyn. That's one of those mistakes I make all the time. Fortunately the compiler bails me out!
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
IMHO, the positive version is easier to read and keep track of in a long program.
Matthew Phillips


Matthew Phillips
Pete Pan
Ranch Hand

Joined: Mar 14, 2001
Posts: 44
never executes since in the while loop you are ASSIGNING false to done, not COMPARING it. Java doesn't warn you about that one like a good C++/C compiler would.
Originally posted by pete hesse:
How about this?
boolean done = false;
while ( done = false )
{
...
}
It preserves positive logic, is easy to read and gets around the naming problem. This is how I was taught back in my mainframe days, and IMHO is still useful.
Hope this helps.

Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
No, only if you reverse!
while (false = done ) ...
Now a C-compiler would catch the error!
So in C it maybe wise to catch typing errors to create a habbit
to take constants in comparison first ??
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Originally posted by Gareth Leachman:
Those mainframe dudes

Whats wrong with us ???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: negative booleaan again