Programming Principles: Stuff You Need To Learn ASAP
It’s been taking me way too long to come up with new content lately, because I’ve been focusing much more on being accurate in my writing than I should. And conveniently forgetting that the entire point of this thing is to document my own learning process, which is going to be riddled with misinterpretations and mistakes!
That’s okay, because we’re here to find the answers together.
As self-taught web developers, we miss out on some of the fundamental coursework included in standard Computer Science academic tracks. The biggest umbrella concept, and one worthy of its own term-long class, is known in the computer science communities as data structures.
A data structure is a particular way of organizing data in a computer so that it can be used efficiently. I actually did take a data structures course at Wellesley, but it never quite came full circle for me, as I effectively stopped programming after I switched majors.
I am moving through coding languages incredibly quickly. I started with PHP via Treehouse‘s tracks, then moved on to JavaScript and eventually Ruby. There are huge root commonalities in all of these languages, much like with spoken languages. We learn how to conjugate verbs, and structure sentences to communicate. With coding, our “words” are being “understood” by the computer, and we are given a response.
From what I’ve picked up, there are some principles that any new programmer will need to grasp in order to start building up your language toolbox. No matter which language we are writing our code in, you will surely use these data structure principles.
Variables
Variables show up in every language. A variable is like a placeholder for information that can be referenced later. Mostly, we first hear about variables in abstract mathematics, such as algebra.
Variables have become so widely used, that they even pop up in languages like CSS — via the use of preprocessors. I was super excited to learn about Sass and Less, which add those types of fun features to existing languages.
Especially in object-oriented programming, you will use variables to store virtually every type of object — strings, integers, floats, arrays, hashes — the list goes on! Variables are flexible and dynamic, allowing them to be appended to, retracted from, and modified in virtually any other way.
Variables will also use memory, which is a valuable resource. As such, in terms of program scalability, they are best used as temporary placeholders. I’m still learning about the programming relationship between structures like databases, which hold large amounts of data, and the programs that utilize this data. Data can, and should, be big! And I’m learning that programs should only be as big (read: resource-intensive) as they need to be.
Arrays and Hashes
Arrays and hashes are both types of data structures, consisting of an index and a value. In short, arrays and hashes solve our problem of referencing and fetching data easily it when it’s needed.
They can hold any type or types of data, and can be stored in a variable for future reference. In some languages, such as JavaScript or PHP, a hash is just a type of array with named indices rather than numbered.
What do we do with arrays? Virtually everything, because like variables, they can be appended to or removed from. Arrays can also be multi-dimensional, as well, storing other arrays inside of themselves.
Algorithms
An algorithm is an ordered set of instructions for accomplishing a specific task. In programming, we need to recognize that there are always multiple ways to accomplish one task. Understanding algorithms is the heart and soul of programming
Each algorithm is also going to have its own cost. One may be more resource-intensive, or take more time than another way. Given the parameters — perhaps how many records you are dealing with, for instance — you’ll choose one way over another. Being wise enough to know which to choose is one component of many that will determine how resourceful of a programmer you are.
Right now, where I’m learning the options… I’m not very resourceful! I occasionally write redundant code, code that sometimes uses too many resources for what I seek to accomplish. The really cool part is that every time I learn a new way, it clicks. I see exactly where I would have used the new way, rather than the way I used when that was all I knew. It’s an exciting feeling, and it will keep you excited to learn! I promise.
I had mentioned the books Think Like a Programmer and Pragmatic Thinking and Learning in a previous post, and one of the very reasons I picked up these books is because they cover a lot of “common” algorithms we face, masquerading as being more complex than they are. The biggest thing I have picked up is that there are often solutions laying right where we don’t see them.
A common fallacy is that if we are not explicitly told that we can do something a certain way… we can’t. Eliminating this fallacy from our thinking is critical! Until we’re told otherwise, every option is on the table, and we should approach problems with that mindset.
Logic Statements
The secondary heart and soul of programming? Maybe, because they tie in so closely with algorithms! A logical approach to writing computer programs allows us to take into consideration multiple different sets of circumstances. On an elementary level, we’re all familiar with logic to some degree. Maybe your Saturday plans are dictated by the weather. If it’s sunny, go to the park. If it’s rainy, go to the mall. Even then, there are other types of weather to factor in.
One particular joke always makes me laugh. A programmer is going to the grocery store, and her husband gives her a simple instruction (I changed it because I’m a woman, see?): “Buy a gallon of milk, and if there are eggs, buy a dozen”. She goes, then returns from the grocery store with a dozen gallons of milk. Upon questioning, the programmer explains, “there were eggs!” Get it?
Humor aside, the point is that even logic needs to be carefully considered. Obviously, the husband meant for the programmer wife to purchase one gallon of milk. Then, if eggs were available, to purchase a dozen eggs. In programming, this can be the difference between using a not-equals (!) operator, or a method such as exists?. Evaluating the coverage of logic statements is a solid part of evaluating your code, and doing quality assurance (QA) on others’ code.
Loops
Loops are fun, and are a big part of command-line programs. I’ve started using these more and more in Ruby, and they allow us to execute a particular line or lines of code either a certain number of times, or as long as a certain condition or conditions are met.
There are different kinds, like while, for, do…while, foreach — and the list goes on depending on, which language you are using. The syntax will vary, but fortunately, they are fairly semantic:
While a certain condition is true, you can do something, with the code terminating upon the end of that condition being met.
For each item in a particular object (array, hash, etc.), you can do something.
Essentially, we are taking lines of repetitive code, and maximizing our program’s text and resource allocations by using a look to execute code for any number of given circumstances!
Knowing when to use a break or continue statement is also equally important. These are a single-word line of code that will terminate the loop manually upon reaching certain conditions, to avoid never-ending loops.
If you have others that I haven’t mentioned (not by my misgauging their importance, of course!), please feel free to let me know in the comments! As always, you can also keep the discussion going on the La Vie en Code Facebook page, or via Twitter @lavie_encode. Happy coding!