The Cellar  

Go Back   The Cellar > Main > Technology
FAQ Community Calendar Today's Posts Search

Technology Computing, programming, science, electronics, telecommunications, etc.

Reply
 
Thread Tools Display Modes
Old 11-28-2010, 07:47 PM   #1
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
Quote:
Originally Posted by Perry Winkle View Post
Your solution depends on side effects.
This is, of course, semantics, but no, it doesn't.

I had a solution to design: how to test for two conditions.

I figured, while either one is false, correct that one.

Quote:
while !(a) or !(b)
{
if !(a)
call (a)

if !(b)
call (b)
}

What could be simpler, or easier to understand?

Let's say I am leaving the house. Did I lock the door (a)? Did I turn off the lights (b)? While either one is false: if (a) is false I lock the door, if (b) is false I turn off the lights. When neither one is false I am done. This is common sense.

I understand that if I were doing this in the "real world" things would be more involved and this might cease to be feasible for a variety of reasons that I have yet to consider, but in essence, this is my conception of how loops work and what they are supposed to (are DESIGNED to) do.

What I don't understand is why you guys find what I did confusing. I used regular, human logic.

Quote:
In the real world, you want functions to have as few side-effects as possible. Ideally, a function would have no side-effect other than its return value. Some languages even ENFORCE this restriction.
What are the undesirable "side effects" of my functions??? My functions didn't do anything but return a value until I moved some parts of main function into them so you guys could understand the elegant simplicity of my while/if/if loop.

I could put it back this way:

Code:
while ((!passLength(password)) || (!containDigit(password))) 
{
if (!passLength(password))
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password1: ";
cin.getline(password, SIZE);
(passLength(password)); //(a)
}

if (!containDigit(password))
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password2: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b)
} 
}
Does that help you to understand that I designed it this way on purpose? This is not accidental, there are no "side effects" ...
__________________
******************
There's a level of facility that everyone needs to accomplish, and from there
it's a matter of deciding for yourself how important ultra-facility is to your
expression. ... I found, like Joseph Campbell said, if you just follow whatever
gives you a little joy or excitement or awe, then you're on the right track.

. . . . . . . . . . . . . . . . . . . . . . . . . . Terry Bozzio

Last edited by Flint; 11-28-2010 at 08:03 PM.
Flint is offline   Reply With Quote
Old 11-29-2010, 10:59 AM   #2
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
Quote:
Originally Posted by Flint View Post
This is, of course, semantics, but no, it doesn't.

I had a solution to design: how to test for two conditions.

I figured, while either one is false, correct that one.


What could be simpler, or easier to understand?
Quote:
while !(a) or !(b)
{
if !(a)
call (a)

if !(b)
call (b)
}
You could remove both if statements altogether. With the side effects that a() and b() have, this code would be equivalent:

Quote:
while !(a) or !(b)
{
}
This is because !(a) is actually calling a(). If the test fails, then the password is changed (the side effect in question). This loop will run until the password makes it through both tests without being changed.

Quote:
What I don't understand is why you guys find what I did confusing. I used regular, human logic.
A big one is that you seem to think that the a's are different in:
Quote:
if (!a)
call a()
They are the same. Whatever is done in one will be done in the other.
Quote:
What are the undesirable "side effects" of my functions??? My functions didn't do anything but return a value until I moved some parts of main function into them so you guys could understand the elegant simplicity of my while/if/if loop.

I could put it back this way:

Code:
while ((!passLength(password)) || (!containDigit(password))) 
{
if (!passLength(password))
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password1: ";
cin.getline(password, SIZE);
(passLength(password)); //(a)
}
 
if (!containDigit(password))
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password2: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b)
} 
}
Does that help you to understand that I designed it this way on purpose? This is not accidental, there are no "side effects" ...
(assuming there is no longer a getline in the tests, the lines I crossed out can be removed)
That is completely different. The tests no longer can change the password. That is logically a good program, though it could be more efficient.

Moving the parts of main() into the tests did not result in an equivalent program. You could not with this programn (as I did above with the other one) remove the entire contents of the while loop and have an equivalent program.
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote
Old 11-29-2010, 12:52 PM   #3
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
Quote:
This is because !(a) is actually calling a().
No. It isn't. It is crucial for you to realize that this is where you are wrong.

!(a) where (a) is a bool function evaluates true or false, it doesn't literally execute the function.



Really, it doesn't. It doesn't do that.



Really.
__________________
******************
There's a level of facility that everyone needs to accomplish, and from there
it's a matter of deciding for yourself how important ultra-facility is to your
expression. ... I found, like Joseph Campbell said, if you just follow whatever
gives you a little joy or excitement or awe, then you're on the right track.

. . . . . . . . . . . . . . . . . . . . . . . . . . Terry Bozzio
Flint is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 09:15 PM.


Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.