The Ternarator
In the previous video we have seen, how easily we could replace a 5-6 lines of if..else conditional with a single line of ternary operation. It is very easy and most of you might not understand why most of the experts avoid using this operator and does not recommend its use to anyone else either. This is because the ternary operation gets much complicated when we move to complex conditionals. Lets see how the complexity arises when we write an equation for a complex conditional.
Example for complex ternary operation
In the previous video, we used the example of finding the maximum value out of any two given values. But here we will write a program to obtain the maximum value out of any 3 given values. Once, the number of variables increase, the comparison becomes much harder and thus the ternary operation. The ternary equation for this can be written as a>b? (a>c? a:c) : (b>c?b:c); The logic can be formed using the nested if conditional. In the above equation, a>b? checks whether a is greater than b or not, if its true (a>c? a:c) is executed else (b>c?b:c) is executed. Now, if any one of the two sets is correct, we can find another ternary operation inside the brackets, just like another if statement inside a nested if conditional. So, again ternary operation is performed for these equations. Say a>b is satisfied and (a>c? a:c) is being processed. Now within this, the compiler checks whether a is greater than c or not. If its true, the value of a is traced out as output else the value of b is traced as output. Similarly, if the if the first condition is false then the second set of equation inside the bracket is executed. This is a bit complicated. The theory behind it is very simple yet the syntax for writing the ternary operation is difficult. So, most of the developers do not recommend the use of this technique.Ternary Operation compared to if conditional
But one thing that needs to be remembered here is that, the whole operation is performed within a single line. If we go with if conditional for this, it takes about 10 lines approximately to develop the application to print the maximum value out of 3 numbers. Although it is simple and easy to understand, I think if the same operation can be performed in a single line, its much better. But its up to the individual, how he/she looks at it. If they feel that the code is too big compared to the complexity of the ternary operation, then they can obviously go for the ternary operations. But if you are a beginner, you will be good with if conditional and its good to prefer if rather than going for complex code in the beginning. So such beginners are advised not to use ternary operation when you are comfortable performing the same operation with if conditional. However, irrespective of whether you are learner or developer, it is important to know how these operations are performed. It just doesnt matter whether you use this technique or not but you must have sufficient knowledge on the topic. You should always be in a position to perform ternary operation any time if needed.Video Transcript
00:00This video is kind of to take it to one level deeper with
00:03ternary operations and how you could leverage them
00:06in a much, much more, I would say, complicated way,
00:10which most programmers will tell you, never do it.
00:14It's so not legible that you shouldn't do it.
00:16And I would probably tell you that 99.9% of the time they're
00:21right, but you could, in one line of code,
00:24replace 30 lines of code with ternary operations.
00:28And although it's not very legible,
00:29if you're building out a banner and you're
00:31trying to figure out a way how to cut down
00:33the amount of code that you have to type,
00:35to cut down the amount of, when you're down to the bytes,
00:39sometimes these kind of dirty tricks, which are not that
00:42dirty, they're just really not very human friendly,
00:46are really great to know.
00:47So what I want to show you is one level more of complexity
00:50in ternary operations.
00:51And what I want to do is I want to create three variables this
00:54time around, and we're going to create a deeper leveled ternary
00:57operations and blow everyone's mind away.
01:00So I'm going to create, again, a, which is an integer,
01:04create b, which is an integer, and I'm
01:08going to create c, which is an integer as well.
01:13And what I want to do in this ternary operation
01:16is create a more kind of sophisticated ternary where
01:22it actually will go a little bit deeper than just
01:24if a is bigger than b, but if b is bigger than c.
01:29And what I'm going to do is I'm going
01:31to create a variable, which will go to p,
01:33but it's going to be big, and big will be an integer as well.
01:36I'm going to start off with if a is bigger than b, question mark.
01:41If a is bigger than b, then we're
01:42going to put the value of, we're going
01:45to put here some sort of value,
01:47which we're going to get back to.
01:48And if a is not bigger than b, then we're
01:50going to put a different value, which we're
01:52going to get back into as well.
01:53Now if you notice, I put round brackets,
01:55and the beauty with round brackets and flashes
01:58is that if you put them, they never really hurt.
02:02And it just sometimes helps the eye
02:05when you put these round brackets,
02:07because it's easier to see that everything inside
02:09of these brackets are in focus.
02:12And it's really just about us making things look cleaner.
02:15And if we wanted to make things even more clean or more
02:18easy on the eye, we could have even broken down the line,
02:21which is completely legitimate in flash.
02:23We could break lines whenever we want to.
02:25So I just broke it down so it's easier to see it,
02:28maybe easier for people to consume.
02:30So now we asked if a is bigger than b.
02:32Instead of just giving you a value of outputting b
02:35like we did before, I'm going to ask
02:36if a is greater than c as well.
02:40I'm going to ask that.
02:41Now if a is bigger than b, and it's bigger than c,
02:44then a is the biggest value.
02:45So I'm going to put a, and then I'm going to put the colon.
02:48If a is bigger than b, but it's not bigger than c,
02:51then c is the biggest value.
02:54Let's go to our l statement.
02:56So we know if a is bigger than b, we've got the answer.
02:58And it's going to bubble the way up until it reaches big.
03:05Now in our l statement, in our other side
03:08of our ternary operation, we're going to ask, well,
03:10we know that a is not bigger than b.
03:12So let's ask if is b bigger than c.
03:16OK.
03:17So if b is bigger than c, because we
03:19know that a has no chance of being the top value,
03:21so we don't have to ask it again.
03:23If b is bigger than c, what is the biggest value?
03:25b.
03:26If b is not bigger than c, then probably, not probably,
03:29but for sure, c is the biggest value.
03:32And what we've done right now is we've created an application
03:35which is really complicated.
03:39We created a very, very complex statement here.
03:42And we wrapped it all into one short line, which we broke down
03:46just to make it legible.
03:47But if we wanted it, it didn't have to be legible.
03:49We could have just put everything in one line.
03:51And that's a really short amount of code
03:53to trace out what's the biggest number.
03:56And it worked.
03:57If we would want to trace it in a traditional way of doing it
04:00with if statements, we would have
04:02to type in a very long, let's do it, a very long if statement.
04:07Right?
04:08And then we'd have to have an if here.
04:11a, if a is greater than c, what else?
04:18And what would be inside of this one
04:20would be if b is greater than c.
04:28Not only that, we would have to actually take the big value,
04:33and we would have to say, OK, if a is bigger than b
04:36and bigger than c, then b equals a.
04:39If otherwise, big equals c.
04:45If b is bigger than c, then b is big.
04:49Else, c is big.
04:52And if you've noticed, this is a lot of code.
04:56This is really a lot of code.
04:59And we've replaced all of this code with a simple line.
05:03So yeah, it's not really humanly friendly.
05:05And yeah, I'm going to get a lot of hate mail and people telling
05:08me that I don't know what I'm talking about.
05:10But this is so much cleaner.
05:13And I don't know why people complain about it.
05:15And I know it's not as easy to digest.
05:19And probably, it's much easier to read this out.
05:22But isn't this so much more extra code
05:25that's not really needed?
05:27And don't you think that if you get used to this,
05:29this will become legible as well?
05:32But it's really up to you.
05:33And if you want to listen to the big cheeses out there
05:35that even, I think even Adobe themselves,
05:38which I consider a big cheese in their formal documentation,
05:41say, well, you could do it, but we
05:43don't recommend you do it.
05:44So maybe Adobe is right.
05:45Maybe you shouldn't do it.
05:47And if you ask me personally, I avoid it
05:49unless it saves me time.
05:53And I know what's going on.
05:56And then I probably do do it sometimes.
05:58But it's really rare.
05:59But if I'm trying to recommend saying to a beginner developer
06:02is when I want you to know how to do it,
06:04I want you to understand how to do it,
06:06that you shouldn't practice it, that
06:08doesn't mean you shouldn't be very comfortable with it.
06:12Because all these type of operations
06:14will make you so much better programmers,
06:16even if you never use them.
06:17You need to get used to all this world of the conditions.
06:21Because all the world of programming
06:24is all about conditions.
06:25So the more fluent you are in conditions,
06:28the more in power you are.
06:29And the more power you have, the more things you could do.
06:32So I'm all for you using it and getting used to it.
06:35And maybe when you're working on bigger projects
06:37and working with people, maybe you should avoid it
06:39and use it only sporadically when you have
06:42a very, very classical state.
06:45Because we just deleted maybe 12 lines.
06:48Maybe we deleted 12 lines with one line,
06:50which is unbelievable, I think.
