Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Making if blocks simple

 
Ranch Foreman
Posts: 141
5
IntelliJ IDE Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the use of the if block like this :



I think the use of not operator in this case just makes the if-condition a *little* bit harder to understand. Most coders are smart enough to understand it easily, but why make them think harder ? Why not put a simpler condition like this :



Does this make sense ? Assume that you have tests which check this if block.

 
Marshal
Posts: 68908
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. A good teacher ought to have taught you that in the first lecture about control structures.
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the particular example you posted, Yes, definitely. But there are times where it's better to use if (! condition). How to identify those? What I would write depends on the English (for me) description of the condition. Ideally the Java code should mirror that description closely, so if the English description includes a negative expression then the Java description should include "!".

For example:

"If the list isn't empty then..."



Often you'll encounter this form when there's a then-clause but no else-clause. But it's true that you won't encounter the "if (! condition)" option as frequently as the usually more understandable "if (condition)" option.
 
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People often don't write full if-else statements. I don't remember the last time I wrote 'else'. That's because a lot of logic becomes easier with 'guards': You test that a certain precondition doesn't hold and then perform an early return.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There used to be a "rule" which said "There must be only one return statement in a method". A lot of people defended that rule but you and I agree that it's a bogus rule. Deal with the special cases up front and return, or throw exceptions, as necessary, before getting on with the normal processing for the method.
 
Campbell Ritchie
Marshal
Posts: 68908
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That rule is called structured programming. I think it is a good idea for beginners to start off with structured programming and later learn where it is all right to breach that rule. The first place you breach that rule is when you write this sort of thing:-
 
Ranch Foreman
Posts: 95
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Using if(! bool) can be used as a fail-fast strategy.
 
Saloon Keeper
Posts: 21963
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to an article I once read, the author claimed to have based an entire college course on "NOT", because it was so tricky. Heck, for decades, the IBM mainframe Job Control Language was so twisted that what you had to code to cause a program to run - or not - could be read as "IF NOT "x" THEN DO NOT execute "Y".

One of the few things I miss about Java not having a C-style pre-processor is this statement:
It allowed you to code things like "if ( NOT (x > b) ) { .... }", which is obviously more visible than that skinny little "!" by itself.

And incidentally, in the Unified Expression Language employed by various Enterprise Java components such as JSPs and JavaServer Faces, there is, in fact a "not" operator that is interchangeable with "!" in EL expressions. Java itself, alas, is not so fortunate.
 
Campbell Ritchie
Marshal
Posts: 68908
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is about the only example I can remember where a #define looks useful.
 
Tim Holloway
Saloon Keeper
Posts: 21963
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is about the only example I can remember where a #define looks useful.



That's because you're not having to do this:


The Arduino modules I'm working with don't have the sophistication or memory space to host an actual debugger.

There are other Stupid Preprocessor Tricks I've found useful over the years, but thankfully I don't spend that much time doing C/C++ these days. Except on the Arduino projects.
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic