The Quickie: Attacking if/switch statements
The Quickie. In this video we will compare between building an app using if/switch statements and building it up using mainly objects. We wil see how we can with a small obect make our code 70% smaller and easier to maintain.
Video Transcript
00:01One of the things I see that are most frustrating when I work with other people's code or even sometimes with my own code old code
00:09there's just too many if statements too many if statements too many switches and
00:14Just to showcase this example. I created a small little application which we could let's just run it up
00:20Let's just run it for a second. And as we run the application
00:25we'll see I created a small little multi-langle application where
00:290 through 6 gives us the days of the week in English Monday Tuesday, Wednesday, Thursday
00:36Ooh, little mistake here, right Thursday. Oh, that's right Saturday Sunday
00:41And we could switch it from English to French and do the same thing and we would see the days of the week in French
00:48Now all this is nice and simple
00:50But we have a few little problems in our application from the get-go one when I click on to the English it should have
00:55Translated my copy into English automatically, right? And when I started up the application
01:01I should have probably had some sort of selection in advance
01:05Let's look at this at the source code for a second and understand. Why is this starting to get really complicated?
01:10So I'm just gonna click on the f4 just to make it nice and big and without digging too deep into the logic here
01:18we'll see that we have your one-click event and
01:20It's capturing all the movie clips and based on what movie clip was selected and as you can see all the
01:28zeros through six movie clips are called mc0 through mc6 we're putting all the logic related to them and
01:35again, we're also putting the logic for our mce and mcf and
01:40If you're one of our subscribers, you could download the source code and check it out for yourself
01:45But really the point of this was is one is even though our application is not doing everything
01:50We wanted to do our code is starting to be really complicated and also very long which makes it very hard
01:57to actually modify our code and
02:00There's once one approach that I love doing and it's one of what's one of my favorites
02:05I don't really know if there's a terminology for it
02:08But I kind of like calling it linking because it makes sense and let me show you what linking means
02:14So I'm just going to go into my properties panel, and I'm just going to change my class to be main short
02:20Which is another class that I've built that does basically the same thing only in much less code and in my perspective much more legible
02:29So if we go into our main short, and I'll just press on our f4
02:33You'll see that first of all our our constructor has a little bit more things going on in it
02:38We're setting up everything in advance. We're creating an object that is holding all the information, and it's holding for e
02:45It's holding all the days in English and array in English for F
02:49It's holding an array of French now most developers that would see this or a lot of developers say
02:55But this is too dynamic and to you know we should work in a structured format
03:00and I agree that most of the times you want to work in a structured format, but there's a lot of cases where you wouldn't and
03:07This is a great example of just because I created these two lines of code
03:11Let's look and see how much less complicated my on click event now is so if I just scroll down to the bottom
03:18You'll see that my on click event now all it really is is this and it's absolutely nothing compared to what it was in our main
03:26If we look into our main we can't even it's 108 lines
03:29It's hard to even see it in one line, and it's not even doing everything right and in this scenario
03:35Not only that it's doing everything it's doing everything and more so if I run my application one more time
03:41We're going to see that my application now
03:44Automatically has the copy on Monday in English and if I click on F
03:48It will transit that into French and I could switch at any point in any given time without any problem
03:55So it's so powerful so powerful and so much control now
03:58I like calling it linking and basically what we're doing is we're using something that
04:03Server-side developers called a call a hash map or in flash a terminology more known as an associative array and
04:11Basically, let's just go dig down here for a second and see what is actually what is the logic inside of our on click?
04:19So the first thing I'm doing in our on click is in line 30 and in line 30
04:23What we're doing is we're fetching the name of the movie clip that was clicked
04:28then we're slicing the two first characters because they're always going to be MC and then our ID and
04:34Then I'm just going to grab what's left and make sure that it's all lowercase so to make sure that we're all consistent always
04:40Now that I've got my ID which now is basically a value between
04:4506 or 6 because our MCs are called MC 0 through MC 6 or it could be E or F
04:52So now that I know my ID the next item that I'm checking is just one if statement and my if statement is checking to see
04:58Is there a link is this ID does this ID exist?
05:02So if our E was clicked on which is our English we would then get our E
05:07Which would then it would return a value in which return a value thus it would mean it's true
05:13So if it found a value then we're going to change that specific movie clip reset the last language one and and
05:20Reset the last language one that's basically what we're doing
05:22We're making sure that our last language that was selected is
05:25Deselected for a second and same is true if it's not one of those digits
05:29That means it's 0 through 6 in our scenario because it could only be a language button or a digit button in our sample
05:37So our if statement here is also very simple so instead of resetting our language
05:41We're resetting our digit and all that's left in line 38 is actually to set our new target. That was just selected
05:49And to set its copy which setting is copy is right down here
05:53okay, and
05:56Because we're short on time
05:57We want to make sure that our quickies are nice and short basically all the way around it want to take out of this
06:02Quickie is try to take advantage of using
06:06Objects as associative arrays you're gonna make your code so much less complicated
06:11And the best tip for me to give you is if you ever have a scenario where you have a switch
06:16there's a 90% chance you don't even need it because you could probably use an object and
06:22It's not necessarily about making it less processor intensive although it is less processor intensive
06:28It makes your code so much easier to read so after you understand the logic there now
06:33You just have to understand what's going on in these 10 lines instead of what's going on in 60 lines
06:40Okay now
06:41I want to show one more thing to show how much powerful this thing is and I've said you're in just two lines
06:46You're and I want to show what would happen if we wanted to add another two languages into our main short
06:50Versus our main long one there our regular main would be really hard to add another language
06:56so what I want to do is I'm just gonna click f4 and I'm just gonna
07:01Let our
07:03Unguide our guide here
07:05I'm gonna save that and I'm gonna go into my main short and all I really have to do now is I'm just gonna
07:10Copy the new language that the movie clips that were added were MCS and MCI
07:17So there we go. We got s for Spanish
07:20and we got I for Italian and
07:24If I run the application one more time
07:26We're gonna see now we have four languages and we could now change the days of the week and change the language
07:32And change it to Spanish if we wanted to and we could change it even to Italian
07:38So we've just created a small translator tool in 40 different languages
07:44And if we wanted to we could have created 20 languages it completely didn't matter and we could have here a hundred different words
07:50It wouldn't really matter. So the powers are insane
07:55so I hope you enjoyed this quick tutorial quick quick quicky and I really hope you try to take advantage of
