Sunday, January 21, 2018

JavaScript: "Bad Parts"

Just like we have imperfections, JavaScript has some "bad parts", which are highlighted by Douglas Crockford in JavaScript: The Good Parts. Some of these features are difficult to understand without extensive experience in JavaScript, making it extremely easy to misuse a function or statement.

I have sometimes misused these functions before when I was writing code. At first it was really confusing why the code was not running, and I was really confused. At this time, I was just starting to learn about JavaScript's shorthand and its "with" statement. I initially thought it was a shortcut and a faster way to compile and write code, but I was proven wrong, as Crockford describes the with function "the with statement significantly slows down JavaScript processors because it frustrates the lexical binding of variable names" (Crockford 110). This means that it confuses JavaScript when you use it, thus leading to an unpredictable result.

Using the "with" statement is highly discouraged
While I was trying to use the with function, the code did not allow me to run, showing me a warning that I should never use 'with' (right). As a concept with good intentions, 'with' was never successful. It was meant to be a function that provides a shorthand when accessing the properties of an object (Crockford 110). It was originally meant to be a shortcut, but instead it was just a nuisance to writing code.

Next, after learning about how the 'with' function messes up the clarity of the code, I tried to find a surefire way to write what I wanted without confusing JavaScript. What I wanted to do was simply state that the variable a equaled variable "b".








The result was exactly how I expected it to turn out, and involved a significantly lower of risk. It was able to properly execute the function with ease. What I learned was that there was always a better and safer way to do something, as shown below where I used more keystrokes than when I used the 'with' function.
Function without using 'with'

Another confusing feature of JavaScript that I found was 'eval'. Eval, defined by Crockford, is a function that gives a string to the JavaScript compiler to execute a certain command (Crockford 110).  Simply, eval is something used to evaluate a string as a JavaScript expression. It is often the most misused feature of JavaScript because of how much experience is required to correctly execute it. Even if one could employ the 'eval' function, it is still highly discouraged, simply for the clarity of the code, which 'eval' fogs up. For example:
Eval statement reducing the clarity of the code
Learning about the bad parts of JavaScript really helped to show me what not to do or use when writing code. It was a great learning experience, and overall greatly improved my understanding of code clarity, and how some of the weaker concepts can affect it. Without the assistance of Crockford, my work would be much different and ineffective, and I would have ended up using some of the lesser preferred functions. My advice? Always stick to using the preferred methods, and avoid the ones that are discouraged.

Question: What is your opinion on the way to solve something? Do you believe there should only be one way to do something, or should there be a diversity of solutions?

Works Cited:
Crockford, Douglas. JavaScript: The Good Parts. O'Reilly Media, Inc., 2008.

2 comments:

  1. Hi William!
    I think there should be many different ways of solving something! It's important to be able to do it in different ways, because without variation, we wouldn't be unique and our community would be completely homogeneous. By having the choice of doing it in different ways, variety is introduced. However, in the world of computer programming, there does tend to be a right and wrong way. Often times, even when there is a specific right and wrong way, there are still a variety of other ways of doing things, and maybe one of those ways is better, but nobody has discovered it yet. So I believe it's still important to do things in many ways, even when programming!

    ReplyDelete
    Replies
    1. Hi Josh, Thanks for reading my post and taking the time to comment! Programming can encompass a wide variety of peoples and personalities, which makes it a unique experience.I definitely agree with your statement that there should be multiple ways to do something, especially in the programming world, where there are nearly infinite possibilities!

      Delete