• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

negative booleaan again

 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21390
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could never think of an identifier that I liked as well. So I suffer through adding the negation.
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9061
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>
boolean done = false;
while ( done = false )
{
...
}
</pre>

I think you mean
while ( done == false )
Right?
 
pete hesse
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right Marilyn. That's one of those mistakes I make all the time. Fortunately the compiler bails me out!
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, the positive version is easier to read and keep track of in a long program.
Matthew Phillips
 
Pete Pan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gareth Leachman:
Those mainframe dudes

Whats wrong with us ???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic