# Ruby and OOP-design (question of an old "procedural person" ;)

**URL:** <https://rubytalk.org/t/ruby-and-oop-design-question-of-an-old-procedural-person/7323>\
**Category:** ruby-talk\
**Created:** [9 August 2003 07:47 UTC](https://rubytalk.org/t/ruby-and-oop-design-question-of-an-old-procedural-person/7323 "2003-08-09T07:47:10Z")\
**Posts on this page:** 3\
**Page:** 3

<div class="post-metadata">

**Author:** ![Dan\_Doel](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dan\_Doel](https://rubytalk.org/u/Dan_Doel)\
**Post date:** [10 August 2003 23:44 UTC](https://rubytalk.org/t/ruby-and-oop-design-question-of-an-old-procedural-person/7323/41 "2003-08-10T23:44:23Z")

</div>

Mike Campbell wrote:

> > The big reason for & and && is that && is lazy, while & evaluates all  
> > its arguments.
> 
> In what language(s)? I think that’s the case in Java, but not in C/C++ if I  
> recall correctly, which I might not.

I’m pretty sure it’s lazy in C++ too.

I’m not sure about C, but I would assume it is, because there’s no  
booleans in C so it’d be totally useless  
as it would do the exact same thing as &.

> > and | could, theoretically be different if || always gave 1 and |  
> > gave whatever the bitwise answer is, but  
> > I don’t know off hand.

I thought laziness was the big factor in && and ||.

- Dan

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [11 August 2003 00:35 UTC](https://rubytalk.org/t/ruby-and-oop-design-question-of-an-old-procedural-person/7323/42 "2003-08-11T00:35:42Z")

</div>

No, it wouldn’t. C doesn’t have a Boolean _type_, but it does  
have the idea of truth and falsehood (needed for if statements, etc).  
Nonzero is true and zero is false. Which means that this expression:

```
1 & 2

```

is false - bitwise AND of 1 and 2 yields 0. Whereas

```
1 && 2

```

is true, because both 1 and 2 are true (nonzero). The big difference in C  
is that the double-characters are LOGICAL operators, while the single  
ones are BIT operators.

However, it happens that the logical operators in C and derivatives  
also short-circuit, which means you can do things like

```
if (p != NULL && strcmp(p->value, key)==0)
{

```

and not worry about the program blowing up because the strcmp() is called  
when p is NULL.

-Mark

> **···**
>
> On Mon, Aug 11, 2003 at 08:44:23AM +0900, Dan Doel wrote:
> 
> > I’m not sure about C, but I would assume it is, because there’s no  
> > booleans in C so it’d be totally useless  
> > as it would do the exact same thing as &.

---

<div class="post-metadata">

**Author:** ![Dan\_Doel](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dan\_Doel](https://rubytalk.org/u/Dan_Doel)\
**Post date:** [11 August 2003 01:01 UTC](https://rubytalk.org/t/ruby-and-oop-design-question-of-an-old-procedural-person/7323/43 "2003-08-11T01:01:50Z")

</div>

Mark J. Reed wrote:

> > I’m not sure about C, but I would assume it is, because there’s no  
> > booleans in C so it’d be totally useless  
> > as it would do the exact same thing as &.
> 
> No, it wouldn’t. C doesn’t have a Boolean _type_, but it does  
> have the idea of truth and falsehood (needed for if statements, etc).  
> Nonzero is true and zero is false. Which means that this expression:
> 
> 1 & 2
> 
> is false - bitwise AND of 1 and 2 yields 0. Whereas
> 
> 1 && 2
> 
> is true, because both 1 and 2 are true (nonzero). The big difference in C  
> is that the double-characters are LOGICAL operators, while the single  
> ones are BIT operators.
> 
> However, it happens that the logical operators in C and derivatives  
> also short-circuit, which means you can do things like
> 
> if (p != NULL && strcmp(p-\>value, key)==0)  
> {
> 
> and not worry about the program blowing up because the strcmp() is called  
> when p is NULL.

Yes, very true. I think I need to get more sleep or something.

> and || would work equivalently, since you can’t get a | b == 0 unless  
> a == 0 and b == 0.

& and && would be different, you’re right.

And, yes, logical && and || are lazy in C which answers the original  
question.

I’m not sure why C doesn’t have ^^ for logical xor, since 1 ^ 2 would be 3,  
which doesn’t work for boolean logic. In Java, for example, though, it  
doesn’t  
make a difference, because only booleans can be used as booleans, so  
there’s no  
reason to have separate logical and bitwise operators, other than clarity of  
intent.

Perhaps xor is just so seldom used that people don’t care.

- Dan

> **···**
>
> > On Mon, Aug 11, 2003 at 08:44:23AM +0900, Dan Doel wrote:

[Previous page](https://rubytalk.org/t/ruby-and-oop-design-question-of-an-old-procedural-person/7323.md?page=2)
