Friday, April 10, 2015

Encouraging the Next Generation



Recently, some of the people where I work have started a weekly writing prompt in order to get people to blog more often. This week's topic is “How do we involve the next generation of young minds?” and was inspired by two of my co-workers building quadcopters with a group of students in the northern part of the state.

This is a very open topic and very difficult to cover in a way that is less wordy than a copy or War and Peace (or at least your average work by Stephen King). However, I think it can be approached by breaking it down into a number of subjects which need to be addressed.

That being said, let's begin.


Start Them Early


The truth is that the earlier you start to instill curiosity and creativity in people, the easier it is to get them to continue on that path. There are a number of ways to do this, but if you're looking for ways to get a small child started, Legos, Lincoln Logs, Tinker Toys, etc are really a wonderful way to get the ball rolling. They offer nearly endless ways to combine pieces creatively, are tactile (which helps a lot of people, children or not. Never underestimate the value of physically building and handling something), and are generally fairly affordable.

Realize that not everything has to involve circuitry and code. Even things like basic woodworking projects can help instill creativity and the engineering mindset. I've built everything from birdhouses and toolboxes to actual buildings while I was growing up and I learned something valuable from every project.

As they get a little older, introduce them to things like crystal radio kits, 130-in-1 experiments kits (yes, they still make those), snap circuits and even simple programming languages like Scratch and Logo. A little later still get them involved in writing code on something like a RaspberryPi (it's fairly inexpensive and if you somehow manage to botch the os, just re-flash the SD card and start over. You can even extend it in order to interact with hardware). Tangoes are also a wonderful, inexpensive tool for teaching spatially related problem solving.

You want things that are simple to get a beginner's grasp of yet versatile enough to keep their attention after they master the basics and, most importantly, make them want to learn even more.


Mindset


“A mind all logic is like a knife all blade. It makes the hand bleed that uses it.”
- Rabindranath Tagore

Logic is a very important part of being successful in technology fields. However, there is another part of the equation that is often overlooked – creativity. You really do need both the ability to think your way through a problem as well as the ability to look at a problem from a completely different angle than other people at times.

We need to foster not only logical thought, but also appreciation of and involvement in the arts. I would almost argue that the type of art doesn't matter nearly as much as being involved. Painting, drawing, writing, music, theater, and countless others – all of these things exercise your brain in ways that simple logic based problem solving doesn't.

On the logical side, we need to encourage the next generation of technical people to cultivate the skills to look at situations objectively, come up with a set of possible answers, and then evaluate or work through them (revising their general assumptions as they uncover more information) until they reach a conclusion. This is the time for measured experimentation instead of just wildly poking at a problem until something happens (though, admittedly, sometimes poking at the problem is necessary in order to uncover behavior).

Encourage the next generation to ask questions. That's not to say that you should spoon feed them the answers since guiding them through the discovery process is both an extremely effective way of teaching and often fun for everyone involved. However you do it, you should encourage them to ask the “whys” and “what ifs”.

Continuing in the vein of asking questions, foster the questioning of authority. If someone says “You can't do that” they should ask WHY. If it's because doing that thing is dangerous, that's one thing. If it's just because they don't think it can be done or because of other foolish reasons (“getting above your station”, “that's not something that proper girls/boys do”, etc), they should be encouraged to CHALLENGE IT.

We are, among other things, professional troublemakers. We create and change current reality as a part of our jobs. It's what we do. Innovation is inherently disruptive. Embrace that; don't try to stomp it out of the next generation.

Encourage reading. I can't stress this enough. Fiction, non fiction, philosophy – just read. It exposes you to different voices and approaches from your own. It's literally a different view on the world, and being exposed to that will help you grow.


Quick Feedback, Small Victories


Make initial victories easy to attain. This is especially true for younger children. Defeat is frequently demoralizing. Starting off with a victory encourages people to continue. It doesn't matter if it's just making a ball bounce on the screen. Give them something that provides near instant feedback that they have done something with a real, visible result.

As they progress, keep feedback loops tight even though the difficulty of what they are doing increases with time. Yes, I realize that this sounds a lot like Agile practices.


Make it Safe to Fail


“I have not failed. I've just found 10,000 ways that won't work.”
- Thomas A. Edison

Failure, both professionally and personally, is an occasional fact of life. No matter what we try or how hard we work, there are times when things just won't happen the way we want them to.

The problem is that we have stigmatized failure to such a degree in this society that some people are afraid to even try because they might fail. This needs to stop.

Teach people that it's okay to fail on occasion. That's not to say that we should excuse laziness, but re-enforce the idea that failure occasionally happens, treat it as a learning experience, and teach others not to fear making a mistake.


Access to Tools


It may not occur to a lot of us in this field that not everyone is as well off as we are from an economic standpoint. Not everyone has ready access to the equipment needed to learn various technical skills. This is especially true in both poorer urban and rural areas.

Access to programing tools (IDEs, etc) has become considerably cheaper thanks to open source tools and educational/community licenses offered by companies like JetBrains and Microsoft. However, for some people, computers are still an expense that they can't justify (even if they are much cheaper than when I started learning to program).

That's not to mention the expense of things like quadcopter kits. Some of the things that you need access to for some projects are simply out of reach for a number of individuals as well as some school districts without outside help.


Access to Mentors


When I started out, I didn't have any mentors to help me learn how to code. The internet wasn't an option (yes, dinosaurs roamed the Earth and we had to walk to school uphill both ways). I had never even met or spoken to a professional software developer until I was in college. In fact, the only other people I knew who wrote code were a few friends in basically the same situation as myself.

It would have been much easier and a lot less discouraging if I had had access to mentors (even online) instead of having my only resources when I started out be the manual for a TRS-80 color computer and the occasional code sample in magazines borrowed from our very small public library (I told you that dinosaurs roamed the Earth at the time).

If you want to encourage the growth of a new generation of creative and technical people, you have to literally be there to encourage and guide them. Answer questions on the internet, make yourself available to schools/after school clubs and programs, and generally be a good community member.


Parental Involvement


This is the really difficult one. In order to encourage young people to go into technology based careers, their parents have to be positively involved. They can't just treat the computer as a way to babysit their child or, possibly even worse, view everything that their child does, no matter what it is, as simply “playing on the computer” like my parents did (which, I might add, included programming homework in college).

Keeping up momentum when it feels like nobody cares or you are being actively discouraged is extremely difficult. Not everyone is as hard headed as I am.

Parents don't even have to be experts in the field. They just have to be positive influences. Be curious about what your child is doing, encourage them, have them show off what they're doing to you a bit. Be a cheerleader. It's important.


Show Me The Money


Show people that there is a (generally) fun, well paying job doing work in this field and that it doesn't matter if they're a girl or a boy or even what socio-economic background they come from as long as they work at it. After all, it's important to be able to do things like pay your bills, go on vacation, and buy sandwiches.

As I said, this is by no means an easy problem, and this is only the short list of things that can be done to help, but it's a start and even if you can only manage a few of them that's better than the alternative.

Current mood: calm

Current music: Murray Head – One Night in Bangkok

Friday, March 06, 2015

A New Chapter – A Long Overdue Update



First off, I apologize for the radio silence. It's been kind of crazy (for varying reasons) and I haven't devoted a lot of time to writing for a while. I plan to change that (or at least attempt to) going forward.

For the few of you that didn't see the announcement on twitter when it happened, I've taken down my personal shingle and joined the team at Pillar Technology. This move came after about 3 years of discussion between me and the team there. Before this, the timing just wasn't right, but things were different this time, so I joined back in mid-October.

Since then, I've worked on an embedded software project for a Fortune 100 farming equipment manufacturer and a dev-ops(ish) cross compile project that allows the code that I was working on in the first project to be compiled for i386, x86-64, Atom, and ARM architectures.

It was odd writing C++ code after 10 years away from the language for the first project and really cool to be able to have a real reason to learn how to code in Python for the second one. Over the last few months, I feel like I've stuffed my brain to the point of overflow and then looked around and said “more!” That's an amazing feeling.

The funny thing is that, before I started, I joked about wanting to try to use TDD in C++. My first project involved exactly that lol

On top of that, I'm diving into learning Android development, which means I get to remember Java (a language that I haven't used in several years since I was doing mostly .NET work for quite some time).

I work with an amazing group of people that make me feel like an idiot child (which is probably a good thing for me, honestly, because it pushes me to grow more). My line on the matter is that, on a good day, I might outsmart the trash can. Thankfully, most of the people around me seem to have similar mindsets (about themselves, not about me), so there's not a whole lot of ego-driven nonsense going on.

I can honestly say that there is basically nobody here that I've worked with so far that I don't enjoy working with and learning from. That sort of environment is really rare.

Here's hoping that our working relationship continues (and continues to improve) for quite some time to come. And, as always, the opinions I express are my own unless explicitly stated otherwise.

Current mood: Contemplative

Current music: Hound Dog - Rocks

Thursday, May 01, 2014

Sometimes Optimization Matters (Recursion vs Iterative OR "A 4 Billion Character String is REALLY Long")



Computers today are really fast. In fact, they’re fast enough that some people don’t bother to optimize a lot of code. Sometimes that’s okay – after all, premature optimization isn’t a positive thing and can see you spending a great deal of time in exchange for little or no gain. Other times, however, you know there’s going to be a difference of an order of magnitude or more on a computation intensive part of a system.

As an example, there is a coding challenge on CodeEval to find the character in a string at a specific location. You aren’t given the string ahead of time, just the first character and the pattern to use in order to build the string (up to a string length of 3,000,000,000).

I’ll summarize here since it’s pretty simple. The character set is 0, 1, and 2. The first character is 0. You build out the string by using the previous string as the first half of the new string and add one to each digit. This means that 0 => 01 => 0112 => 01121220 and so on.

You only want one character in the string (we’ll call it N). The rest, as far as you are concerned, is garbage.

The knee jerk reaction for a lot of people would be to build the string to the length that you need and just access the character directly. That might even be okay if your upper bound for length wasn’t 3 BILLION. Given that, however, you need a better way (I’ll show you why here in a little while).

If you take a minute to look at it, you’ll realize that, by following the pattern they’ve defined for the number, that you will always end up with a string that has a length of a power of 2 (1, 2, 4, 8, 16, and so on). That means that you can determine how long the shortest string of that pattern will be by taking x = Ceiling of log2(N) and then calculating 2^x.

That doesn’t help us much by itself, but with that information, the pattern, and the fact that we only need one character, it makes this problem a prime candidate for recursion.

Method 1 – Recursion

Recursion can be kind of a scary word for some people because initially it can be sort of difficult to wrap your brain around. However, given a little practice, the hardest parts of the exercise are structuring the problem in a way that you can use recursion and making sure that you actually write it properly (in fact, it took me a little while to get rid of a bug because I had a line of code in the wrong place).

I’ll link to the code here so I don’t have to walk through everything, but to put it succinctly, after calculating the needed string length, the recursive approach essentially keeps track of where the target character would be during each string transformation, uses that to calculate how many times the character would have to change (C) and then calculates our N = C % 3 (each character is essentially a one digit base 3 number, so we can get away with that).

It’s probably as close to doing the problem with pure math as you can get without actually having an equation. As a result, it’s quick. REALLY quick. Fast enough, in fact, that I had to measure runtime in ticks (according to MSDN documentation, it takes 10,000 ticks to make up a millisecond).

The chart below shows the runtime (in both ticks and milliseconds) of one calculation for each given string length (2^0 through 2^32) – not counting file I/O or output to screen (which the original project required). This is just the time from being handed the number to getting the answer to what N is.

Power of 2
Recursive Runtime (in Ticks)
Recursive Runtime (in Milliseconds)
0
4
0
1
10
0
2
4
0
3
4
0
4
5
0
5
5
0
6
5
0
7
4
0
8
5
0
9
5
0
10
4
0
11
5
0
12
4
0
13
5
0
14
5
0
15
5
0
16
5
0
17
5
0
18
5
0
19
5
0
20
5
0
21
5
0
22
6
0
23
6
0
24
6
0
25
6
0
26
6
0
27
32
0
28
6
0
29
6
0
30
6
0
31
7
0
32
7
0

Note that the 32 tick runtime for the 2^27 length string is an aberration (probably caused by time slicing in the operating system). Running it multiple times, every test case ran at 20 ticks or below (generally 10 ticks or less). The only reason it looks like a big difference is because ticks are really REALLY tiny.

So, to sum up the pros and cons for the recursive approach to this problem

Pros: Really fast. Takes up less memory because there’s no actual string being built.
Cons: More difficult to write and debug


Method 2 – Iterative (Don’t do it this way. Please)

Again, I’ll give you the code for this method.

This time, we’re actually building the string in order to find the character. There are a few problems with this.

First is that it’s SLOW. I initially tried doing it two different ways. The first way ran for over HALF AN HOUR trying to calculate the 2^32 length string before I stopped it. The way which is in the code linked above runs much faster because it uses String.Replace() but seems to use more memory.

Second, it takes up A LOT of memory because it’s actually building the string and keeping it in memory. That’s not a big deal when the string is small, but according to the formula here, the size of just the string at its maximum length is 512MB.

Below is the benchmark data for this approach using the same constraints (no file or screen I/O) and the same numbers as the recursive approach.

Power of 2
Iterative Runtime (in Ticks)
Iterative Runtime (in Milliseconds)
0
4
0
1
5
0
2
4
0
3
6
0
4
7
0
5
9
0
6
21
0
7
17
0
8
35
0
9
56
0
10
111
0
11
281
0
12
386
0
13
2011
0
14
6360
3
15
12657
6
16
12528
6
17
125246
61
18
57912
28
19
53481
26
20
101440
49
21
204253
99
22
400705
195
23
1250113
611
24
1778822
869
25
3158791
1544
26
6980949
3412
27
OME
OME
28
OME
OME
29
OME
OME
30
OME
OME
31
OME
OME
32
NA
NA

OME stands for Out Of Memory Exception. These test cases literally caused the program to exceed its allocated memory (it was compiled for “any processor” since NUnit’s test runner didn’t want to play nicely with forced x64 solutions).

The NA result at 2^32 reveals a special problem. In C#, a character at a specific index of a string is accessed by using stringName[int]. The catch is that 2^32 is larger than an Int (you need at least a Long if you want a number that big), so you’d need to compensate by doing a substring to chop off the first Int32.MaxValue and reducing the index by the same amount.

I did that in the first attempt at this problem, but since I found out it was going to throw memory exceptions, I decided that I’d refrain from re-implementing it for the sake of simplicity and more readable code.

As you can see, this approach is MUCH slower than the recursive one. In fact, the general trend is to roughly double the runtime for each larger string. This means that a string of length 2^32 would take roughly 384,000,000 ticks (38,400,000 times the length of time for the recursive solution assuming the recursive solution took 10 ticks).

This is, of course, assuming you made the string from scratch for every test case. You could get around that to an extent by storing the string and just building it out longer as needed. However, you’d still be eating up a lot of memory to store the string. At its longest, the string would be over 4 billion characters long.

That’s longer than War And Peace. This is why, sometimes, optimization really matters.

I’m not sure I really need to sum up the pros and cons for this approach, but

Pros: Stupidly easy to write
Cons: Slow and memory intensive.

That’s not to say that recursion is the right answer to every problem, because it isn’t. There are times, however, when the extra work in designing a more efficient algorithm in order to tackle a problem can really pay off.

Just some food for thought.

Current mood: thoughtful
Current music: Shawn Mullins – Lullaby

Wednesday, April 23, 2014

The Evils of Perl (or Logic Should Make Sense)


The people who know me professionally (even a little) have probably picked up on the fact that I hate Perl. I spent several years as a sysadmin, so I come by my Perl aversion honestly.

Don’t get me wrong, it’s a useful language and I found it to be a very handy tool, but it’s so very easy to make code that even the person who wrote it isn’t sure what it does. The next day.

Or it could be that I’m a crappy developer. Either’s possible. Maybe a little from Column A and a little from Column B.

It’s the only language that I’ve seen where your cat can walk across the keyboard and come up with a valid program. What does it do? We may never know. It may parse a file for a list of addresses, trigger an ICBM launch, or create animated gifs of dancing cats, but by gum it’ll run.

Personally, I’m hoping for the dancing cat gifs. Everyone loves dancing kitties.

My standard quip for why I finally stopped coding Perl is that in any sane language, to get the length of an array, you do something along the lines of myArray.length. That may not be exactly how it’s done, but it will be something similar.

In Perl, you do the following: $myArray = @myArray

You assign the array variable to a scalar variable (single piece of information. Int, char, etc.). This to me makes zero sense from a logical standpoint.

I made that comment about a week or so ago, and the person I was talking to said that it makes perfect sense because it’s an implicit type conversion. (I’m not sure if he was joking or not. I hope, and think, that he was).

He had a sort of point about implicit type conversion, but that sort of thing is supposed to follow a logical convention of some kind. For example, an implicit conversion from int to long makes sense (it’s basically just shoving existing data into a bigger container).

You could even make the case for implicit conversion between string and character array data types. I wouldn’t do it, because it’s probably better done explicitly so you don’t shoot yourself in the foot, but the argument could be made.

However, using implicit conversion to cast an array to (effectively) an integer in order to get the length of the array doesn’t make any logical sense. It’s like buying a chicken because you want a renewable source of pineapples.

In what non-Euclidian universe does this make sense?

“Well, I just need to figure out how long this array is, so I’ll assign it to a scalar and HOLY CRAP! THERE’S CTHULHU!”

Ia! Ia! ArrayLength! Fhtagn!

Any language that will summon Cthulhu by simply using it is probably a bad choice…

Now, if you don’t mind, I’ve got to Great Old One proof my office.

Current mood: Amused

Current music: Charlie Daniels Band – The Devil Went Down to Georgia

Tuesday, April 08, 2014

In The Beginning



In the beginning, there was a television. To the television was attached a Tandy TRS-80 CoCo. Then he said, let there be code. And there was code, and it was in BASIC. He was a child and found the fact that he could make a computer do things to be really cool.

As a teenager, the internet was laid open to him. HTML was discovered and there were many horrible mistakes made, including the dreaded animated gifs of flaming skulls and spinning things. The land was known as GeoCities, and it was eventually cast into perdition for its sins.

Then was Java made known unto him, and, though he tried, he found it confusing. This was the first age of Java, so he was not alone in this.

Lo then did he graduate high school and make his way to college in an effort to learn to do this shit properly.

There did he learn of many things – Pascal and Prolog, C and C++, Scheme and Assembly, Socket programming, the creation of data packets, and stack traces among many other skills and pieces of arcane lore.

He paid the campus ferryman with his toils as a sysadmin where he applied logic to a place of chaos known as a non-profit. All the while, using his position as an excuse to do things programmatically so as not to have to run around the hilltop known as The Ridges as often.

Then did he return to Java, and he found it…okay. After all, with a background in object oriented programming, data structures, and the like, Java now made much more sense.

Upon his path, however, he decided that Java was not the right tool for him, so he endeavored to learn C#. Additionally, in order to wipe away, or at least atone for, his past sins of using Perl, he also learned Ruby to an extent that would allow himself to get into (and hopefully out of) trouble on occasion.

He walked the paths of desktop development and eventually began to work in order to master the arts of web development as well using the MVC framework. So, then, did his past of using HTML and CSS come back to him (though, thankfully, the flaming skulls and spinning things did not), and that is where he stands as of this day.

Current Mood - Tired
Current Music - Shades Apart - Stranger By The Day

Wednesday, February 26, 2014

TDD Saves Me From Myself



I’ve gotten into TDD at least a bit over the last few months. It has a learning curve, but I’m finding that it sometimes saves me from myself (and saves me time in debugging).

Part of my problem, like a lot of people, is that I sometimes over think things or fail to see something as being multi-purpose.

A perfect example was a code kata I started yesterday. It’s a fairly simple exercise – given a file with a list of numbers, take each number, reverse it, add it to the original number, and check to see if the result is a palindrome. Repeat until the number is a palindrome.

Simple stuff, really, but it’s a nice thought exercise.

It started off well. Decided to tackle the number reversal first. Since we’re dealing with data being snagged from a file, I decide to do the reversal with strings (since the data is already in string format, I can do most of the things required by the problem without having to typecast between string and int).

I name it ReverseNumberAsString since that name says what it does fairly well (it’s not the greatest name, but it’s the first function I’ve written in the solution, and we can refactor later).

Fast forward a bit to my writing a function to test if a number is a palindrome. This is where I start to over think things (and where TDD tells me I’m an idiot).

It’s been a long day and I’m a little fried, so the first solution that comes to mind is to set pointers at the front and end of the string and just walk them toward the center, comparing, until they reach the middle or find something they don’t match.

(Hey, I spent years doing C and C++ code. Give me a break)

I test a negative case first, and the test fails. I’d set the tail pointer to string.Lengtrh instead of string.Length-1 (it happens. I was fried.) I fix it, run the test, and it passes. Yay

Try to test for a positive case and the test fails. I go to look at the code to figure out why and, after a minute or two, I realize that I’m stupid, because I’ve already written the answer somewhere else in the code.

ReverseNumberAsString reverses a number that’s in the form of a string. No it doesn’t. It reverses a string. Period. I’m a dummy.

Rename the function to ReverseString like it should have been from the beginning and use that. Compare forward to backward. If it’s a palindrome, they should be the same thing.

Look at that. It works. Who would have guessed.

Thank you, Nunit, for making me stop and see that I’m being dumb.

Current mood - calm
Current music - Dirty Vegas – A Million Ways