To Engineer: My Design Philosophy

I have a habit of calling myself an engineer. You could say well like an engineer that build my favorite clogged turnpike or that blender that lasted three days? Well yes and no, but rather than argue semantics lets first take a look at the origin of the word.

The word engineers roots go back to the roman word ingeniare(“to contrive, devise”) and ingenium (“cleverness”) per wikipedia. If your Spanish is up to par they should also seem a bit familiar to you. One thing interesting to note is that the English word engineer also shares an origin with the word engenders(“To arise from”). This particular framing of the word gives me particular pause for though as my job as a programmer requires a great deal of attention to that ethos.

Ethos? Yes I believe so. I believe that a more accurate definition of the word engineer, despite the modern context, is the title of someone who’s core mastery / profession is to contrive and create tangible, useful devices that move the world forward. An engineer derives these constructs through cleverness and intellect. Now I use the term devices loosely, this can range from the physical( a phone, turnpike, computer processor) to the metaphysical(Concepts, ideas, thoughts). Now it is important to included thought-space in our definition because physical derivation must first be derived through thought. This is perhaps the biggest departure from the traditional definition.

How its useful

By considering this we can also use the word engineering to define a philosophy. In its simplest form this philosophy is the philosophy of creating things through cleverness and creative use of known / discovered principles. As a software developer I have found that it is important to approach problems by providing myself a label that extends beyond the traditional word software developer of X. This acknowledgement that in fact I’m engineering a solution in a greater sense allows myself to act outside of the normal barriers the we impose on our minds. I instead have now become a problem solver, only limited by my means of my own imagination and astuteness to research and learning.

Further Reading

The Executioner: the JavaScript execution context and how to defeat it

JavaScript as the dynamic and versatile language it is provides us with several non intuitive points to be weary of while constructing our code. But before we dig too deep into the issues and their solutions lets review a few facets of the JavaScript’s interpreter.

Execution Context

[Note: also sometimes referred to as Activation Object, Scope Object, Variable Fairy]

Death

Not as scary as it sounds, I assure you. To understand its purpose we need to merely break down its very name. Execution Context, it is the manager of the context of the code that is currently being executed. Quickly, lets take a look at what the execution contexts properties are.

var executionContextObject = {
variableObject:{},
scopeChain:{},
this:{}
};

  • VariableObject – Simply the list of variables/arguments which may be accessed in the function. More on this in a moment.
  • ScopeChain – All the execution context objects containing of all the parent functions.
  • This – the value associated with the current this

Variable Object Array

Lets take a moment to peel back the covers and understand how the variable object works. Now at the point this is happening the interpreter has reached a new function and is creating the execution context for us. These are the steps it will take while creating it.

  1. Look at the inputs||arguments of the function and make an arguments array out of them.
  2. Find each function and and save the pointer to it, if it finds a matching pointer overwrite it with the newest value. Stick results into the Variable Object array.
  3. Find each variable with a var and initialize it to its line assigned value (var me = ‘Ben’) or default which is the “undefined” value. Stick results into Variable Object array.

This array is what lets us do things like this.

var mySweetObject = {
color: "Red",
magicalAbility: "telekinesis"
};

//Normal way to access a property
var color = mySweetObject.color;
//Using the Variable Object Array
var magicalAbility = mySweetObject["magicalAbility"];

The Execution Stack

Now that we understand a bit of how the execution context looks lets take a look at the execution stack. The execution stacks job is to keep track of whats being executed as well as its associated execution context. We can think of it simply as a stack which items are placed on top as they are encountered or LIFO if you prefer that verbiage.

Created By David Shariff

[Image created by David Shariff]

Hoisting

You hear a lot about hoisting, but armed with the above you can tell all of your friends about how simple it is, properly asserting your intellectual superiority over them. Lets look at an example.

console.log(typeof funkyFunction ); // function pointer

var funkyFunction = function(){
return "awesome";
}

Wha, bit odd that myFunkyFunction seems to have a value as though it was declared before its var. But we know that the variable object array is created before the code is executed and therefore is the any inside the scope of this function are created before execution. (Side Note: this does not apply to variables applied to child function)

Scope Chain

Like mentioned before the scope chain is an array which contains all parent execution contexts. Its purpose is to store the location of all variables that the current function can access , this is to facilitate JavaScript’s external variable storage which is represented as lexical scoping.  Lexical scoping simply means functions are stuck with the variables of their parent functions even if called from another physical location.

Use local variables

Now considering the impact of all our variables being stored in physical order, also it is then given they must each be resolved every time a non local variable is utilized. This is a very good reason to be weary of the global scope since it must always be contained in the scope chain. So lighten up on those global declarations, it can save you on bigger JavaScript projects. I have seen scope variables easily reach over 100,000 in big projects, this meaning a serious impact on scan times when the interpreter must resolve variables. But of course use of local variables avoids this entirely, allowing the interpreter to remain in the variable object array.

P.S.

Don’t forget that lack of a var keyword when declaring a variable always lands that variable into the global scope, this includes functions.
Thanks for reading!

Further Reading

SQL Voodoo Query: Merging a column in multiple rows into just one

[This is an old but regularly requested  post of mine from BIDN in 2011, updated due to my renowned grammar]

Today I wanted to quickly mention a technique to pivot a one to many data structure into a one column list of items. I know it has been mentioned before a bit differently in previous blogs before on BIDN.

The problem?

We have three tables

InstructorsInstructorClassesClasses

In the instructors table we have the instructors name, ID, and yada yada. InstructorClasses is our junction table and links all of the instructors to their list of classes and finally classes is our simple list of classes.  Now assuming we have a web page that needs to show them as below?

Instructor: Jim Bean

Classes: Rowing; Yoga; Bartending;

Now assuming you are unable to format in code or perhaps you are archiving data and needing to convert it during ETL. To get the expected results we can utilize the XML Path command in SQL Server to provide us a pivoted view of classes as demonstrated in the example below.

SELECT Left(Main.Categories,Len(Main.Categories)-1) as Classes, Instructors.FirstName + ' ' + Instructors.LastName as InstructorsName
FROM Instructors
left join (Select distinct ST2.InstructorID ,
(Select distinct Classes.ClassTitle + '; ' AS [text()]
From InstructorsClasses ST1
join dbo.Classes
on ST1.ClassID = Classes.ClassID
Where ST1.InstructorID = ST2.InstructorID
For XML PATH ('')
) [Categories]
From InstructorsClasses ST2) [Main]
on InstructorID = main.InstructorID

Skill Growth: Or how I learned to love the blog

So this being the first formal blog in some time as well as the very first for the site so I figured that my first topic should be on blogging itself. It has occurred to me on numerous occasions, in both recalling technical issues I have faced as well as clarifying my own thoughts, how really useful blogging is. Though it doesn’t help to have poor memory and terrible grammatical skills. But if trial by fire can’t cure early onset Alzheimer’s then I don’t know what can. So let’s get this ramble going.

typing

To blog or not to blog?

If you are here then I’m pretty sure you have a solid handle on the concept of blogging, so I’m going to skip being a Webster dictionary knock off. That said, the Internet has provided an enormous soapbox for us to stand on so why not use it. On top of the fact that there’s a vast number of content hungry users which have been provided to us by the glorious proliferation of the internet. This has thus created a huge churning pool of knowledge consuming, LOL cats viewing (God only knows what else) group of individuals seeking quality content. All of which may find your ideas to be helpful to them. This gives us the unique opportunity to dive in and see what ideas you have that actually stick and refine them accordingly.

That being said blogging’s greatest potentials lay not only in the fact there are others out there that may find your content useful, but that you yourself may find the act of writing useful. The act of writing requires us form ideas which can easily be conveyed to others. A great deal of mental house cleaning is needed to preform this feat and as we solidify our ideas it allows them to become clearer. Ideas are then illuminated to our-self’s as well giving clearer authority and understanding of them.

What is your blogging tool, and what does it do?

Now hopefully you read the section header with an Austrian accent, if not I’m disappointed in you dear reader. But to the relevant conundrum. Although the majority of my previous blogs have been generally technical, I’m going to forgo replicating the majority of the Internet and provide you with one key tip. Though I will list out several useful links towards the end of the blog for expanded reading. Now as a blogger there really is only one tool that you need and its name is WordPress. Now I know a large segment of the Internet will disagree with me and that’s OK. But in reality it’s such an easy tool with many features that unless its your sole source of income and you have excessive spare time why distract yourself from the most important element, content content content.

But I do promise more to come soon! In the meantime I will be working to get the rest of the site ship-shape and bristol fashion.