Watching:

Practical Guidelines for Working with Complex Coding


show more Conditions can get complex and unwieldy very quickly, as JavaScript can handle a thousand levels of If statements if needed. However, such code would be difficult to read and manage. In this Lynda.com tutorial, Simon shares some practical guidelines for working with complex conditions for more readable, easier-to-maintain code. show less
please wait ...

Working with complex conditions

So I am going to create a condition and then add a little more thought to it. In this JavaScript file, I am starting off by creating a variable called balance, setting it equal to 5000, just to give us something to work with. Then I'm going to write my if statement. The condition, I am going to write here will be if balance is greater than 0, Yes, we know it is, but we are just writing some sample code. Then I need my code block. Now, it's very common that you'll see programmers when they hit the opening curly brace to also then jump a couple of lines, and do the closing curly brace.

And this just verifies that I have both of them in place and I'll just come back up and start typing the code that's indented inside my code block. So we are just trying to prove a point here. So I'll do something like alert, the balance is positive, and we have a very straightforward condition. But the question is well, what if it's not? What if that was false? I could do another if statement and check balance again, but what I could also do instead is if that's not the case, I am going to add the word else.

Then I'm going to do the opening curly brace for the code block again because I wrote that, do the closing one, and what we have going on is if the condition is true, we'll do whatever is in the first code block. Otherwise or else, we'll do whatever is in the second code block. So I'm going to write a line of code here, closing that with a semicolon. Now, I have introduced a very common error here and it's an error in my thinking, not in the code.

We ask if balance is greater than 0, we're going to output a message saying the balance is positive. Otherwise, we'll output a message saying the balance is negative. Well, what happens if it's 0? In this case, we're going to hit the if statement. If balance is greater than 0, well that would be false. So if our balance is 0, it will give us the balance is negative message. really not what we want, but it's quite a common error to get hit with, particularly when you're dealing with things like positive and negative numbers. So be a bit too exclusive about what you're checking against.

Thinking a bit deeper about it, what I want to ask here is if the balance is greater than or equal to 0. And of course what we are asking there is as far as our program is concerned, what do we care about? Let's say we are dealing with bank balances, where 0 would not count as a negative balance. Now, there is an operator for that. It's just the greater than or equals sign. This is regarded as a single operator. So it's just the two characters next to each other. What we can also do is take this a little bit further.

Inside this first code block that will be executed if the balance is greater than or equal to 0, I could add another if statement. If balance is greater than 10,000, do my opening curly brace for the code block, couple of lines, and then do the closing one. And notice that what I am doing here is trying to make this apparent. I am lining up my closing curly brace with its corresponding if statement, the same way that I would line up the first closing curly brace with its corresponding if statement, and then I would indent any code inside of that.

If I were to save this and then jump over to my container HTML page that will run it, we should get The balance is positive. Click OK. But we do not get this internal if statement because balance is not greater than 10,000. That would be false. So you can create multiple levels of complex conditions inside your code. Now, do be aware, don't nest too deep. JavaScript can handle a thousand levels of If statements if you started to type them, but lots of nested Ifs, say beyond three levels, can get very difficult to read and make sense of just for a human being.

If we do have to have more complex logic, there are other techniques we can use to deal with it, such as working with functions which we're going to get to soon.

Working with complex conditions
Video duration: 4m 9s 4h 47m Beginner

Viewers:

Conditions can get complex and unwieldy very quickly, as JavaScript can handle a thousand levels of If statements if needed. However, such code would be difficult to read and manage. In this Lynda.com tutorial, Simon shares some practical guidelines for working with complex conditions for more readable, easier-to-maintain code.

Subjects:
Developer Web
Author:
please wait ...