User Tools

Site Tools


ch03-places-to-put-things

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ch03-places-to-put-things [2017/08/24 01:56]
mithat [Processing's primitive data types]
ch03-places-to-put-things [2017/08/24 02:03] (current)
mithat [Potential pitfalls]
Line 349: Line 349:
 Processing is an example of a **statically typed** language. In statically typed languages, the type of data that can be stored in a variable cannot change once the variable has been created. The most common way to indicate the data type that can be stored in a variable is through declaration. Other examples of statically types languages are C, C++, C#, and Java. Processing is an example of a **statically typed** language. In statically typed languages, the type of data that can be stored in a variable cannot change once the variable has been created. The most common way to indicate the data type that can be stored in a variable is through declaration. Other examples of statically types languages are C, C++, C#, and Java.
  
-Contrasting with this are **dynamically typed** languages. In dynamically typed languages the type of data that can be stored in the variable is free to change at any time. It can store a number in one statement and a string of characters ​in the next. Since the type of data stored in a variable can change, there'​s no need to explicitly state what type of data you plan to shove into the box. Because of this, many dynamically typed languages will automatically create a variable for you the first time you try to store something in it, and they will automatically change the type associated with the variable as needed. Many programmers are attracted to the simpler syntax that dynamic typing permits. Popular dynamically typed languages include ​Javascript ​and Python.+Contrasting with this are **dynamically typed** languages. In dynamically typed languages the type of data that can be stored in the variable is free to change at any time. It can store a number in one statement and a character ​in the next. Since the type of data stored in a variable can change, there'​s no need to explicitly state what type of data you plan to shove into the box. Because of this, many dynamically typed languages will automatically create a variable for you the first time you try to store something in it, and they will automatically change the type associated with the variable as needed. Many programmers are attracted to the simpler syntax that dynamic typing permits. Popular dynamically typed languages include ​JavaScript ​and Python.
  
-So, dynamically typed languages seem pretty cool. Why don't all languages work that way? One disadvantage of dynamically typed languages is that all the automation associated with them consumes computing resources while the program is running. That means slower execution and/or larger memory requirements. Another, and possibly more important, disadvantage ​is that dynamic typing can be dangerous. Static typing provides an extra safety net by ensuring that new variables are created only when you really wantrather than because you missspeelled a variable that you already created. They also make sure that only code that is designed for a specific type operates on that type. For example, it might make perfect sense to divide two numbers, but it doesn'​t really make sense to divide one string by another string.+So, dynamically typed languages seem pretty cool. Why don't all languages work that way? One disadvantage of dynamically typed languages is that all the automation associated with them consumes computing resources while the program is running. That means slower execution and/or larger memory requirements. Another ​reason ​is that dynamic typing can be dangerous. Static typing provides an extra safety net by ensuring that new variables are created only when you really want rather than because you missspeelled a variable that you already created. They also make sure that only code that is designed for a specific type operates on that type. For example, it might make perfect sense to divide two numbers, but it doesn'​t really make sense to divide one string by another string.
  
 These might seem like small issues when you are just starting programming,​ but you quickly learn to appreciate them when your programs become longer and more complex. These might seem like small issues when you are just starting programming,​ but you quickly learn to appreciate them when your programs become longer and more complex.
Line 359: Line 359:
 ===== Syntactic sugar ===== ===== Syntactic sugar =====
  
-**Syntactic sugar** is any syntax rule that doesn'​t introduce new power or concepts into the language but rather ​makes using existing concepts easier, more compact, or more semantically obvious. Two examples of syntactic sugar in Processing are variable initialization and multiple declarations.+**Syntactic sugar** is any syntax rule that doesn'​t introduce new power or concepts into the language but instead just makes using existing concepts easier, more compact, or more semantically obvious. Two examples of syntactic sugar in Processing are variable initialization and multiple declarations.
  
 ==== Variable initialization ==== ==== Variable initialization ====
Line 368: Line 368:
 int foo = 66; int foo = 66;
 </​code>​ </​code>​
-The above statement declares an integer variable named ''​%%foo%%''​ and gives it an initial value of 66 in one statement. You can initialize any of Processing'​s primitive types this way---as long as the value on the right of the equals sign is compatible with the variable'​s type.+The above statement declares an integer variable named ''​%%foo%%''​ and gives it an initial value of 66 in one statement. You can initialize any of Processing'​s primitive types this wayas long as the value on the right of the equals sign is compatible with the variable'​s type.
  
-If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the "​default"​ value given to a variable you declare might be a random and/or arbitrary value.((The language on which Processing is built, Java, [[http://​docs.oracle.com/​javase/​tutorial/​java/​nutsandbolts/​datatypes.html|//does//]] specify default values for certain kinds of variables. However, the kinds of variables we have been using up to now and will use for most of this text do not fit into that category.+If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the "​default"​ value given to a variable you declare might be a random and/or arbitrary value.((The language on which Processing is built, Java, //[[http://​docs.oracle.com/​javase/​tutorial/​java/​nutsandbolts/​datatypes.html|does]]// specify default values for certain kinds of variables. However, the kinds of variables we have been using up to now and will use for most of this text do not fit into that category.
 )) ))
  
Line 380: Line 380:
 char firstInitial,​ secondInitial;​ char firstInitial,​ secondInitial;​
 </​code>​ </​code>​
-declare two variables---''​%%firstInitial%%''​ and ''​%%secondInitial%%''​---which are both character types. Notice the comma between the two variable names.+declare two variables''​%%firstInitial%%''​ and ''​%%secondInitial%%''​which are both character types. Notice the comma between the two variable names.
  
 You can even combine variable initialization and multiple declaration in the same statement: You can even combine variable initialization and multiple declaration in the same statement:
Line 389: Line 389:
 ==== Potential pitfalls ==== ==== Potential pitfalls ====
  
-Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read--it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read--because the reader has to know all the intricacies of all the different possible ways of doing things.+Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read---it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read because the reader has to know all the intricacies of all the different possible ways of doing things.
  
  
ch03-places-to-put-things.1503539810.txt.gz · Last modified: 2017/08/24 01:56 by mithat