Higher tiered coding?

if this is a beginner course, why are we learning stuff that 3+ year people are doing? example:

direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;

as a beginner i feel it would have been easier for us to use/remember if it was left as the if else statement. it seems like some of this stuff is getting complicated for no reason. or maybe im just too dumb to get it…idk. all i can say is good thing his tests arent graded. cause ive been failing a lot of them recently. stephens a good teacher, but it just feels like hes dabbing into intermediate or pro level styles of coding.

I don’t know if I’d exactly call this intermediate or pro, but it’s important to know what the ternary operator is. Especially when looking at other peoples code. The ternary operator is a shorthand form for an if-else statement.

In this case, direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f; Is the equivalent of doing this with an if-else statements.

if (direction.x < 0.f)
    rightLeft = -1.f;
    rightLeft = 1.f;

I think I’m going to have to defend the instructors here. I’ve never taken a C++ course, but I have taken courses in C, Java, and JavaScript before. In each of them, the ternary operator were taught fairly early on. Typically, they are taught immediately after if and else are taught, and before loops are taught.

You don’t have to use them but they are good to know about in case you encounter them in other code. Programming languages can often express the same in multiple ways. Also, sometimes things can be subtlety different, and the ternary operator is actually one of those cases.

I’m going to share an additional thing about the ternary operator that wasn’t covered in the course, and I hope that this might be something that is helpful and that you can learn from (and not just cause further confusion).

A big difference between the ternary and an if / else block is that if / else are statements and don’t return anything, but the ternary operator does return a value. So, while you can use it in place of if / else, you can also use it directly provide a result for another expression, a return, or an assignment.

An example of this would be the original line:

direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;

That isn’t much difference than using if/else. But you could instead write it like this:

rightLeft = direction.x < 0.f ? -1.f : 1.f;

In that case, the condition of direction.x < 0.f is evaluated, and then a value of -1f or 1f is returned, which then is assigned to rightLeft.

Or hypothetically, if we had a function that determined the facing by passing in the direction.x as “x”, that function could have something like:

return x < 0.f ? -1.f : 1.f

You could write those with if/else blocks too, but I hope this helps show the value of the operator. With practice, ternary can be actually easier to read than if/else blocks.

This is definitely on point :slight_smile: Most intro to ‘c++’ will have ternary operators.
I do feel that this is very dense though - doing this after doing intro to C++ and intermediate C++ lessons I feel I keep move past the C++ lessons and get into the “C++ for gaming” concepts, which is what this course is for.

Would highly recommend aiming for that content prior to this so you can really soak in the ‘game’ aspects.

I would advise the same for other languages before jumping into full-blown projects as well (e.g., make an API/website/etc.).

Not that you “can’t” follow along, but more so you are going to be stuck in the ropes with the density!

1 Like

its also a thing of keeping the work up and keep learning, i eventually got to a point, where i had a mess up, couldnt fix it, and actually messed it up so bad it wouldnt run anymore trying to fix said mistake. so i deleted that restarted from the very beginning and was able to breeze through it, with minimal errors, and made it all the way to the end. but at the time i didnt wanna delete the project and start over, but honestly, i think it helped me in the long run knowing a tiny bit about what he was talking about as he went over it a second time

1 Like

There’s a mantra in the agile-development crowd, fail often fail fast. Now this isn’t a mantra that I feel you should adopt strongly, it’s still important to know about and know that even in other development spaces it’s not only ok to fail, but even expected that you will.

Mistakes happen, failure happens, take that failure and apply what you’ve learned to try again. In the context of our courses; there’s no time limit, there’s no “final exam”, and no one is going to judge you if you need to review a lecture, section, or the entire course again. So don’t be hard on yourself and enjoy the journey, along with all its twists and bumps.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms