Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Patrick Burke
Patrick Burke
5,885 Points

Why if !(car1_speed == car2_speed) rather than just writing "!="

Is this good practice? Does my method of if (car1_speed != car2_speed) make a difference in odd cases?

Patrick Burke
Patrick Burke
5,885 Points

I received this question Quiz Question 2 of 3 Would the following code return true or false? !(1 == 2) The answer is of course true but the syntax is clearly confusing. (1 != 2) would be much less confusing.

Hi Patrick,

Is this a video or a challenge?

Like you, I'm struggling to see why your code isn't clearer but that depends on context and usage. If you can link to where this code is presented, that would help.

Steve.

Patrick Burke
Patrick Burke
5,885 Points

My apologies, here is the link to the not operator lesson https://teamtreehouse.com/library/ruby-operators-and-control-structures/logical-operators/the-not-operator I am wondering why anyone would write The not operator is written by surrounding a condition with parentheses and an exclamation point. !(1 == 1) rather than (1 != 1)

1 Answer

Hi Patrick,

I think this is just Jason demonstrating the ! operator alone, rather than getting into the realms of != at the same time. It isn't supposed to be a demonstration of great coding practice, although that scenario could be a good solution in some situations, but rather showing how negating something using just the ! works.

The key thing here is that you understand both methods/approaches so your learning is complete in this area. Jason is keeping it very simple to highlight how ! works on its own.

Steve.

P.S. R.I.P. Jason - a great teacher.