Thinking about Thinking

What amazes me is how long it has taken us to accept intellectual standards based on reason, and it’s heartening to see that the forms of good thinking espoused by Galileo and others during the renaissance that were unduly oppressed for thousands of years in human history is now a prerequisite for being taken seriously in the professional world. But even with this growing recognition of the importance of good thinking, the workings of the mind, however mysterious, is still being treated in schools-more so in Asia than Europe or the United States- as second to skill acquisition, rote memory, and other exercises of the brain that cause students to take the most fundamental cognitive functions for granted. They are lectured on how to use the brain- and like a tool, the brain has many useful features, but rarely is there an examination of the tool itself.

In the United States, good thinking is usually what most educators refer to to as “critical thinking”, yet critical thinking is subject to the same flaws of the human mind. The often irrational aversion towards risk, its subconscious influence, and not to mention the negative self talk that discourages many students from pursuing a goal because somebody told them they were not good enough. It may be that I am misunderstanding the definition of critical thinking- perhaps critical thinking already implies a form of higher level thinking. But even so, from my experience, I feel that my education on thought has been superficial at best. I was taught how to think “critically” about the arts, and read with a “critical” mind, which helped me get by academically and gave me a sense of my ability through standardized testing, but didn’t teach me to be critical about my own thoughts in respect to standards such as fairness and accuracy, or, more importantly, to examine the quality of my thinking through an awareness of its shortcomings.

If we can grow an awareness on the importance of quality thinking outside of just the intellectual community, through educating children on psycho-analytics and encouraging citizens to think meta-cognitively,  we’ll have less people who vote blindly along party lines and fall victim to political rhetoric, less volatile traders, and less attention paid to just plain bad thinking.

30. November 2011 by admin
Categories: ramblings | Tags: , , , | Leave a comment

SQL Operator Precedence

Unlike normal code statements in a programming language, SQL code is not executed from left to right. SQL statements are a bit weird and are processed in a non-sequitur manner that may seem counter intuitive to beginners. SQL is read by the database based on the order of precedence- and the operator with the highest priority in any statement is the FROM clause, which shouldn’t be a surprise since you can’t manipulate data without having first specified where the data resides. But trying to figure out why GROUP BY occurs after the WHERE has been evaluated is less clear, so understanding operator precedence before reading or writing SQL queries is vital to writing cleaner queries , and can save hours of head-scratching.

The order of precedence from highest to lowest :

FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY

Lets take a look at an sample problem and take a look at the right and wrong way of writing a query for it.

Lets say we want to know the classes where the sum of the students ages greater than 30 in a table that contains the student name, age, and the class he/she belongs to.

Beginners Fail Attempt:

SELECT class FROM table WHERE sum(age) > 30 GROUP BY class

Why this is incorrect: Which comes first, WHERE or GROUP BY? According to operator precedence, WHERE is evaluated first. So the database returns the sum of ALL the ages, which is not what we want. What we want is the sum of EACH class, and only the classes where the sum is greater than 30. It then goes on to GROUP BY the class- and instead of giving us the answer we want, it assigns the total sum to each class.

Correction


SELECT class FROM table GROUP BY class HAVING sum(age) > 30

Why this works: HAVING is the group equivalent of WHERE. It works with aggregate data. How? Lets walk through this query using us new understanding of operator precedence. First, the data is pulled using FROM. Now, which holds the higher priority, GROUP BY or HAVING? GROUP BY does, and so it groups the data based on our specified column. Finally, it evaluates the aggregated data with HAVING, and the SELECT returns the correct answer.
Closing

The general rule of thumb when writing SQL statements is to keep the order of execution in mind. What do I want? I want the total # of orders for each customer. But instead of jumping directly to the columns for the select, think top down in terms of operator precedence. What table? Should I set a predicate? Do I need to group? Now that they are grouped, what do I want? The problem then becomes dead simple.

Here’s a question for you:

Does the following statement comply with operator precedence? Why or why not?

SELECT a.name FROM table AS a

28. August 2011 by admin
Categories: sql, t-sql | Tags: , , , | Leave a comment

JavaScript: HTML DOM Flow

The view from the top

I had a lot of difficulty understanding how all the different methods and properties in the DOM API worked together when I first began learning JavaScript. For example, how are methods such as similar to methods such as setAttributes? And  how are they related to property specifications like nodeType? It was all very confusing. The details are very important, and key to building a solid understanding of both JavaScript and DOM fundamentals, but I’ve found that it’s equally important to understand how each piece in the DOM work in concert to make DHTML possible; I put together a simple flow diagram using google docs that abstracts that process. If you want more information about the actual DOM API, check out Mozilla or W3C schools.

14. August 2011 by admin
Categories: javascript | Tags: , | Leave a comment

Traversing the DOM

Below is a script that I wrote in JavaScript to find all the tags used within a page recursively. This might come in handy when building a dynamically generated table of contents…


function get_tags(n){

 var tag_string = "";

 if(n.nodeType == 1){
 tag_string += n.nodeName + "\n";
 }

 var children = n.childNodes;
 var total_children = children.length;
 for(var i = 0; i < total_children; i++){
 tag_string += get_tags(children[i]);
 }

 return tag_string;

}

06. August 2011 by admin
Categories: javascript, recursion | Tags: , | Leave a comment

Learning JavaScript: Understand the DOM!

First, a quick review about the JavaScript Definitive Guide aka “Rhino Book”.

I purchased the definitive guide a year ago and just wiped the dust off the cover  last week. It’s a great reference book for developers who already have a basic understanding of the DOM, but it doesn’t contain many exercises for a total novice to apply the lessons. So if you’re looking for a how-to book that not only provides the conceptual explanations of javascript, but also includes enough exercises for you to apply the in-book lessons, this is not it. Also, W3c schools is still the best resource for gaining a solid basic understanding of any language before trying to reap the benefits of an actual book.


I’m beginning to feel much more comfortable with element manipulation now, and there are several exercises that I think were key to helping me understand client side javascript. One of them-and I recommend this to anyone hoping to gaining a complete understanding about javascript- was researching the DOM thoroughly. Understanding the concept of the model is fairly straight forward, but manipulating it using DOM methods and properties is a different story- In the past I tried to do the latter before the former, only to be frustrated and confused before turning to the more intuitive structure of jQuery and the abundance of free copy-and-paste scripts available on the web to mask my ignorance. So now I’m back to step 1.

Trying to program with client side javascript without having an intimate understanding about the DOM API is worst than trying to build a house without a blueprint. It’s really like trying to build a house without a single clue about what construction tools and materials you actually need. Just like any other API, you need to read the documentation. And client side javascript is no different. Once you can explain the difference between properties like innerHTML and nodeValue as well as how they fit into the DOM tree, it’s just a matter of picking up the syntax and learning the quirks particular to the language. Oh, and practice.

Some sources on the DOM that I highly recommend!

DOM Analogy

Mozilla DOM Documentation

Quirksmode JavaScript Guide

06. August 2011 by admin
Categories: javascript | Tags: , | Leave a comment

← Older posts