Showing posts with label Nguyen William. Show all posts
Showing posts with label Nguyen William. Show all posts

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.

Monday, January 15, 2018

JavaScript: Functions

In JavaScript, functions are one of the main components of writing code. A function can be anything that "encloses a set of statements" (Crockford 26). For new programmers like me, functions can be very confusing to learn, but are essential in order to create complex code. Over time, however, the learning process pays off, allowing you to employ functions and use them to your advantage.

Since functions have a broad definition, they can be found in nearly any set of code. For example, a simple function literal, which includes four parts. Respectively, the function literal's parts consists of the word function, the functions name, the parameters of the function, and the statements in curly braces (Crockford 27).  Since I am just starting to learn how to create functions, I was only able to make a simple one, a function literal that can retrieve someone's information.
An example of a function literal.
I am still very new to JavaScript, and so far the most confusing thing so far are functions, probably because of my inexperience with style and syntax. In my learning to understand functions, I found that instead of skipping to the big ideas, I should start small. In the case of literal functions, learning what returns are can be very helpful in understanding the larger concept of functions.

A return is a statement that can be used to cause a function to return early, meaning that it instantly executes the function and ignores the remaining statements. To practice return functions, I made a statement that gives you the sum of two numbers when you press a button, and one with the return function:
Sum statement without the return function 
Sum statement with the return function
First, I decided to use HTML for JavaScript so I could see the user interface, or the button which says "get sum of c and b". It took me a while to find out how to format the program the way I wanted it to look, but I eventually was able to make it the way I wanted. As for the return function, I included it in one of the programs and left it out in the other to see how the output would differ.

I pressed "RUN" on each of the consoles, and they both gave me some different results, as I had predicted. Alluding to Crockford's description of the return statement, it made sense that one of the programs gave an output through an alert, or a pop-up notification on the top of the screen.
Run Button
In line 12, the return function was present, and this caused the program to completely ignore the alert, and so when I pressed the button there was no pop-up giving me the sum of C and B, which was 30.

Line 12 with the return function

This was definitely a great learning experience for me. I was able to realize that the individual parts are what make up the sum of the larger parts, and gave me a chance to open my eyes to new learning processes!

Question: How do you complete your tasks? Do you agree that the small portions are as significant as the bigger whole?

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

Sunday, December 31, 2017

JavaScript: Style

One of the most important things to remember when writing code is style. Style, in comparison to the real world is simply making the program better and easier to read. Although it can be a small part in the code and how it executes, it makes a huge difference in how the programmer/computer can read and interpret it. There can be many types of styles depending on the programmer, with no true "correct" form. Learning how to properly style your code can be extremely confusing and difficult, but is necessary for successful coding.

Because I am new to programming, this can be one of the most difficult things for me to master and  learn. I often made minute mistakes that cost me quite a bit of time. "Code that appears to mean one thing but actually means another is likely to cause bugs" (Crockford 96). I practiced on SoloLearn and tried to find and fix some of my errors, and see if any of them had a "hidden function" that would interfere with the function I wanted it to run.


Original Code with error
Error code's real output
While searching for errors, I found one of my errors, as the original code on the right was programmed incorrectly, and resulted in the one on the left. It was because I put the brace symbol "}" in the wrong spot, causing the program to misunderstand what you were writing. This can often be confusing, because in JavaScript, the way you write code is extremely important, and even a seemingly insignificant mishap can detract from the code's function.

Style is nearly everything you write in the program to make it easier to read, which includes comments. Code in the form of comments, or lines with two backslashes in front can give readers more information on the program, such as what the programmer was thinking or leaving important to-do messages (Crockford 96). In my code, I often use comments such as these to help me remember what I did with the code, or its functions. As a new programmer, it can be confusing for me to look back and remember what I was thinking/doing. These have helped me greatly, since I am busy with things other than coding, and I spread my programming time over a large period to prioritize other tasks. I often forget my thoughts during the coding session, especially after being away for long periods of time. Comments, however, refresh my memory, so using them can set me on track to finish my code.
Creating a comment in green

Comments do not affect the code, but they can be part of the backbone of the code, since they are there solely for the purpose of clarification. Making effective and useful comments can greatly improve how easily the program can be understood by programmers, and thus play a large part in the code's style.

Through my trial and error, I have learned more about how to style my code to make it as easy as possible for the machine to understand what I am inputting. It may not seem important at first, but as you start creating more advanced programs, it becomes clear how crucial it is to have proper style and formatting.

Question: How does style play a role in your life? Does the way you perform your tasks or work affect its outcome? 
Works Cited:
Crockford, Douglas. JavaScript: The Good Parts. O'Reilly Media, Inc., 2008.


Tuesday, December 26, 2017

JavaScript: Punctuation

Before starting to write more difficult lines of code, I have to know the grammar rules of JavaScript. Grammar, as defined by Douglas Crockford is how the computer language is built and structured (Crockford 5). Because grammar is a large part in the language's structure, I will have to practice in order for me to code efficiently. Additionally, the strength and accuracy of the grammar will directly correspond to the integrity of the code and how it will perform. Having flawless grammar will be challenging to master, but it will become necessary in the future to successfully code.

One of the many forms of punctuation introduced by Crockford is whitespacing. Whitespacing, simply, is having a blank space to separate characters. Crockford mentions that "whitespace is usually insignificant, but is it occasionally necessary to use whitespace..." (Crockford 5). Although Crockford states that whitespace can be more or less unimportant, I find that it makes a significant difference in writing and editing code, since the spaces make it much easier to read. However, I was still hesitant in putting a small space between words.
Code with whitespacing

Code without whitespacing

Making your code easier to read can be extremely beneficial, not only for you, but peers who investigate your code. When something is easy to read and understand, the process for revision and editing becomes significantly less difficult. Again, I was hesitant to go the extra mile for readability, but after having my dad, a software engineer, explain to me how important readability was, I realized how important it could be, and made it into a habit to whitespace consistently. With my newfound knowledge of punctuation, I was able to more code with much more efficiency and clarity, allowing for easy revision and readability.

After a few hours of experimenting with a multitude of grammar and conventions using Crockford's guide, I created a simple visual reference chart for the future as I start learning how to create advanced and complex code. The testing process helped me better understand what to do with some of the conventions. Just as writing helps to reform memory, continuously writing code reinforces my understanding of computer language.


When I finished my attempts to practice and learn the grammar of JavaScript, I had gained more understanding of how and when to use some commands, and what I should type. Additionally, I had learned that the learning process can be extremely slow and difficult, and possibly boring, but it can pay off in the end when you have mastered the contents.

After reading this, would you like to try to learn something difficult? If you have already done so, when have you devoted time and effort into learning something?
Works Cited:
Crockford, Douglas. JavaScript: The Good Parts. O'Reilly Media, Inc., 2008.

Thursday, November 30, 2017

JavaScript: Introduction

William Nguyen

JavaScript: The Good Parts, Douglas Crockford

Programming can often be seen as extremely difficult and time consuming to learn. This is not always the case, as it can easily be learned by anyone who simply devotes their time and effort. Learning a computer language can be extremely beneficial for those who are interested in a path down the quickly-growing technology field, as the pay and demand for experienced programmers are extremely high.

There are a vast amount of programming languages that are available to learners, each with their own purpose, such as C++, C#, Python, etc. However, I will be focusing on JavaScript, one of the most demanded languages in the job industry.

What exactly is JavaScript? JavaScript is the primary language that web browsers use, making it one of the most popular programming languages in the world (Crockford 2). This means it can be used to create web interactive elements such as text, or create complex applications and programs. In my practice to eventually master the language, I have chosen to create a simple program that responds with text based on whether a given statement is true or false. To create this program, I first need to obtain a console, which is a text-only interface that runs computer languages. Luckily, there are a multitude of consoles readily available on the internet, some with helpful guides, which I found out about when I started to learn how to program.
SoloLearn, the console I am using


After I found my console, I started to play with some of the code and test some different functions, seeing how JavaScript worked. As it turned out, many of my statements had mistakes, making it impossible for them to execute properly.  I found that it is necessary and extremely important to have good syntax when programming if you want your code to function. From this, I realized that I was essentially "speaking to the computer", and similarly to real life situations, if you do not communicate clearly, the person will not be able to do the job properly.


Syntax errors in red

 
The correct output

I made a significant amount of mistakes, but even the smallest error can make a huge impact on the how the code runs. In addition, errors can cost us large amounts of time to fix. "The sooner we can detect and repair errors, the less they cost us. JavaScript is a loosely typed language..." (Crockford 3). This means that JavaScript's software is unforgiving when it comes to mistakes, and this shows that in order to not waste precious time finding/fixing mistakes, we need to construct code accurately the first time. I noticed that I was constantly making mistakes by overlooking the minuscule details. After realizing what I was doing wrong, I was able to change the way I wrote code. Instead of typing and moving onto the next line without looking back, I made sure to double-check my work for syntax errors. This was extremely effective, as it became a helpful habit.


Code working properly!
Close-up of edited code

Practicing to master my new technique took quite a bit of trial and error, but as a result, my code was working much more often!




Question: Have you ever tried or thought about starting to learn how to code? If so, what are your thoughts on the learning process, and if not, what is your opinion on  starting to learn something                                                                  completely new?  


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