empty "if" statement - it doesn't look nice, does it?

X

xhoster

Tomasz Chmielewski said:
Although an empty "if" statement is technically OK in Perl, it doesn't
look very nice (and in some languages is even forbidden).

Let's suppose we have a code like:

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}

What would be a good way to get rid of this empty "if" statement?
Or is it OK to have it?

It's OK with me, especially if it only temporarily empty and something
is likely to go there in a future modification of the code. Or even if
it is permanently empty but might contain a useful comment.

if ($aldjflajdsf>98 && $lkjadf<10987) {
## This means the frobnitz isn't compatible with the dohickey
} else {
print "blah blah blah"
}

Although it would usually be better to arrange your variable names and
value such that they are more self-explanatory.

Xho
Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
S

sln

Negating it or using "unless" would yield an unwanted result (but I
didn't explain it very precisely, sorry for that).
We want to print the text only if neither $a nor $b were equal a given
number.


Example 1 - does "nothing" (uses empty "if"):

my $a = 1;
my $b = 2;

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}


Example 2 - negated, but the result is wrong:

my $a = 0;
my $b = 2;

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}


Text was printed, although $b was equal 2. Not what we wanted.

Using "next" or "last" in the empty statement would break the things if
this "if" was inside of yet another "if".
"goto" function? Doesn't look much better than an empty "if".

I didn't read the rest of the reply's but I'm sure somebody said this:
print "We're here only if a is not 1 and b is not 2!\n";

Your here if "a is not 1" OR "b is not 2". Not AND.

Essentially:

if (!($a == 1 && $b == 2))
{
print "We're here only if a is not 1 OR b is not 2!\n";
}

Which, if you "boolean negate" all things in parenthesis (multiply by -1), can be reduced to:

if ($a != 1 || $b != 2)
{
print "We're here only if a is not 1 OR b is not 2!\n";
}


Conclusion, there is no LOGICAL reason to have an empty block just to negate an if statement.
ALL statements can be resolved using BOOLEAN logic, all..

However, as a long time C++ programmer, I can tell you that I do this all the time.
Why?

As a placeholder if the condition were to someday be true.

However the reality is that

this notation: if (!($a == 1 && $b == 2))
is preffered
over this: if ($a != 1 || $b != 2)

because it separates the classes into distinct "what is" and "what isn't",
which is what you wan't.


sln
 
S

sln

I didn't read the rest of the reply's but I'm sure somebody said this:


Your here if "a is not 1" OR "b is not 2". Not AND.

Essentially:

if (!($a == 1 && $b == 2))
{
print "We're here only if a is not 1 OR b is not 2!\n";
}

Which, if you "boolean negate" all things in parenthesis (multiply by -1), can be reduced to:

if ($a != 1 || $b != 2)
{
print "We're here only if a is not 1 OR b is not 2!\n";
}


Conclusion, there is no LOGICAL reason to have an empty block just to negate an if statement.
ALL statements can be resolved using BOOLEAN logic, all..

However, as a long time C++ programmer, I can tell you that I do this all the time.
Why?

As a placeholder if the condition were to someday be true.

However the reality is that

this notation: if (!($a == 1 && $b == 2))
is preffered
over this: if ($a != 1 || $b != 2)

because it separates the classes into distinct "what is" and "what isn't",
which is what you wan't.


sln

Following up on my own post..
Ok, I just read the other posts.

I am not saying you don't have the 'logic' portion of your brain
when I make this recomendation. Instead, I'm offering a suggestion on how
to develop it.

Its a natural ability to resolve boolean expressions.
Programmers, over time develop this naturally.
Realistaclly, probably %40 of initial codeing errors, and
%20 of remaining errors after fixes, are attributed to errors
in logic, boolean errors.

I call them "conceptual" errors. They are what testers are for.

The people who are very good in boolean logic, can read code
as fast as people read books. They are well paid.

Reading the boolean logic in code is the first line of error detection
when debugging large code. It scrapes off the first layer.
Its re-read for deeper "conceptual" errors, design errors.
By that time, all the flaws are known. The fix is harder.
Well paid people.

If you were good in math since childhood, say intermediate algebra,
could almost do it in your head, you will probably be a good programmer
if you study.

So my suggestion is that if you have access to college, you take a course
called "Formal Symbolic Logic 101". It's all about Boolean symbolism,
sort of Electrical Engineering'ish. In essence, it takes the words, sentences,
paragraphs of .. debate speaches, for instance... or news articles, or whatever..
And formulates BOOLEAN equations to represent them. The equations are then resolved,
ie: reduced, to test the validity of the representation. Its TRUE or FALSE, and why.
Although there are many more constructs of logic in language (human language), that
comprise the conditions, in that context there are many more gradations of fallicy
than can be incorporated into computer language. As such, there are logic laws,
rules, falicy's, that can be easily identified.

The big thing in "Formal Symbolic Logic" is that logic is incorporated to interpret
language. And guess what? The very same formal logic symbolism laws are used in CS and EE and ME
and all the other sciences.

Its an elective in Junior College, take it!


sln
 
S

sln

On Mon, 11 Aug 2008 20:19:13 GMT, (e-mail address removed) wrote:
Following up on my own post..
Ok, I just read the other posts.

I am not saying you don't have the 'logic' portion of your brain
when I make this recomendation. Instead, I'm offering a suggestion on how
to develop it.

Its a natural ability to resolve boolean expressions.
Programmers, over time develop this naturally.
Realistaclly, probably %40 of initial codeing errors, and
%20 of remaining errors after fixes, are attributed to errors
in logic, boolean errors.

I call them "conceptual" errors. They are what testers are for.

The people who are very good in boolean logic, can read code
as fast as people read books. They are well paid.

Reading the boolean logic in code is the first line of error detection
when debugging large code. It scrapes off the first layer.
Its re-read for deeper "conceptual" errors, design errors.
By that time, all the flaws are known. The fix is harder.
Well paid people.

If you were good in math since childhood, say intermediate algebra,
could almost do it in your head, you will probably be a good programmer
if you study.

So my suggestion is that if you have access to college, you take a course
called "Formal Symbolic Logic 101". It's all about Boolean symbolism,
sort of Electrical Engineering'ish. In essence, it takes the words, sentences,
paragraphs of .. debate speaches, for instance... or news articles, or whatever..
And formulates BOOLEAN equations to represent them. The equations are then resolved,
ie: reduced, to test the validity of the representation. Its TRUE or FALSE, and why.
Although there are many more constructs of logic in language (human language), that
comprise the conditions, in that context there are many more gradations of fallicy
than can be incorporated into computer language. As such, there are logic laws,
rules, falicy's, that can be easily identified.

The big thing in "Formal Symbolic Logic" is that logic is incorporated to interpret
language. And guess what? The very same formal logic symbolism laws are used in CS and EE and ME
and all the other sciences.

Its an elective in Junior College, take it!


sln
Also might help if there are unary expressions like this:

$var &= ~($badval << $bits);
 
J

John Bokma

However, as a long time C++ programmer, I can tell you that I do this
all the time. Why?

As a placeholder if the condition were to someday be true.

I LOLed
 
D

Dave Weaver

Tomasz Chmielewski said:
It does.
It only uses the "empty if" when both "a is 1" and "b is 2".

Then the inverted statement will skip the code when both "a is 1"
and "b is 2":

if ( $a != 1 || $b != 2 ) {
# code
}

or
if ( not ( $a == 1 && $b == 2 ) ) {
# code
}

which is *exactly* the same as your example using the "empty if"
when both "a is 1" and "b is 2".

i.e. both of the above are identical to your original:

if ( $a == 1 && $b == 2 ) {
# empty if
}
else {
# code
}

Which is what Peter said above.
 
B

Bart Lateur

Tomasz said:
Negating it or using "unless" would yield an unwanted result (but I
didn't explain it very precisely, sorry for that).
We want to print the text only if neither $a nor $b were equal a given
number.


Example 1 - does "nothing" (uses empty "if"):

my $a = 1;
my $b = 2;

if ($a == 1 && $b == 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}

Your text is wrong. You'll get there if a is not 1 OR b is not 2.

And this behaves exactly the same as

unless ($a == 1 && $b == 2) {
print "We're here only if a is not 1 and b is not 2!\n";
}
Example 2 - negated, but the result is wrong:

my $a = 0;
my $b = 2;

if ($a != 1 && $b != 2) {
# Essentially an empty "if" statement
} else {
print "We're here only if a is not 1 and b is not 2!\n";
}


Text was printed, although $b was equal 2. Not what we wanted.

The text is wrong again.

You'll get there is a is 1 OR b is 2

And again, you'll get exactly the same result if you do

unless ($a != 1 && $b != 2) {
print "We're here only if a is not 1 and b is not 2!\n";
}

BTW look up De Morgan's laws (first hit in Google: the Wikipedia page).

It tells you to distribute the NOT across terms of a boolean expression,
you have to swap AND and OR

!(A && B)

is equivalent to

!A || !B

just like

!(A || B)

is equivalent to

!A && !B
 
J

Jürgen Exner

Tomasz Chmielewski said:
Peter Makholm schrieb:

if !( $a == 1 && $b == 2)

Doesn't look correct to me?

Right, it's a syntax error. You need to write

if (!($a == 1 && $b == 2))
No, it wouldn't work in a desired way.

Nonsense, of couse it does.. Have you ever had even basic teaching in
Boolean logic?
What I meant: negating the "if ($a == 1 && $b == 2)" wouldn't make what
I want.

Obviously you have no clue whatsoever. Negating the condition of an
if-statement will swap the 'then' and 'else' parts without causing any
semantic difference.
Maybe that is not what you want, but then maybe you should restate your
goal because by all sensible interpretations of you question that is
exactly what you were asking for.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top