Busy

Worked up a multi-level core data schema last night for storing and handling data, and wrote a loader to allow importing files into the data store, allowing me to provide default data without hard-coding it into the app itself.

Core Data is an interesting technology. It has some weaknesses compared to SQL databases – there are no “uniqueness” tests built in, and everything is in memory for large data sets which is a bigger issue on memory-limited devices like the iPhone – but it also provides an excellent wrapper for managing data without having to write a lot of code yourself.

There are actually a number of decent tutorials, including Apple’s own documentation, but it’s not a “beginner” topic, and I’ve found that the only way I could get my own head wrapped around it was to actually work with it, so there won’t be tutorials coming soon.

Version Control – git and OSX development

I really can’t do much more than recommend this quick how-to, and point out that I entirely agree. 

I dearly love time machine, and it has its place, but when it comes to tracking changes in anything more complex than a couple dozen lines of code, or more importantly, figuring out what changes you made and why, nothing beats having the convenient log of what you did and why that version control provides. The recommended gitX makes it pretty easy, though I find the branch merging to be a bit lacking (I prefer to use Tower).

Defining and Creating Objective-C Data Types

Learning Objective-C would be straightforward for anyone exposed to other languages, if one only wanted to learn Objective-C. You have your basic “C” data types, you have “objects.”

Further in, you have a library of “Foundation” objects that handle numbers, strings, and arrays. This is part of the standard “GNU” distribution and is not Mac-specific. The Root object is of the type “NSObject”

Even Further in, you have the Application Kit frameworks that handle UI elements and their needed features for programming on the Mac  (pasting, etc.). “Cocoa” includes both the Foundation and the Application Kit frameworks.

What I needed was a simple, clear-cut rule on when to use pointer nomenclature.

In C-based (and other statically typed) languages, you have to tell the program what kind of information is stored by a variable. So:

int myInteger;

myInteger = 1;

tells the computer that there will be a variable named “myInteger” used to access or manipulate data, to set aside room for that variable, that the variable will be an integer, and sets it to “1.” They can easily be combined into one statement:

int myInteger = 1;

Now, let’s look at objects. First of all, Objective-C, like Python and other object-oriented languages, refers to objects instead of accessing them directly, and so when you define a new variable for an object, you define it as a pointer. 

MyClass *myObject;

myObject = [MyClass alloc];

myObject = [myObject init]; 

This block of text creates a variable called “myString” that is a pointer to an object of the “MyClass” class. The second line calls the classes factory method to allocate the space for the class and create the basic structure of the object. The last line tells the object to initialize itself with any starting information needed so it can be used and referenced.

Why the curly braces? That requires a detour. Like all object oriented languages, objects have methods. For example, “car” objects have “drive” methods that do something when the car is driven. In Objective-C, you call these methods with the following syntax:

[object methodname]

so when I type:

myObject = [myObject init];

I am telling the “myObject” object to run its “init” method, and to assign the object it returns back to “myObject”. 

Also – name conventions. In Objective-C, Class, method, and variable names are written out in “camel case,” where instead of using underscores or other characters, descriptive names are put together with first letters capitalized. The very first letter of class names themselves are capitalized, while all other method and variable names, the first letter is lower case. Thus:

myObject = [MyClass alloc];

actually tells the object for the core class to create an instance of itself, and assign it to “myObject,” while:

myObject = [myObject init]; 

tells the actual object “myObject” to initialize itself, and repoint to its new self when done.

Also, messages can be nested. Since “alloc” returns a valid object, we don’t need separate lines. Instead, we can write:

MyClass *myObject = [[MyClass alloc] init];

Since [MyClass alloc] returns a valid instance of MyClass, it can then be passed a message to initialize itself (via “init”) before handing itself over to our new variable. This one-line methodology is the way you will almost universally see a new class object defined and created.

What about arguments? Arguments are delineated by colons. So, an init with one value passed in would look like:

MyClass *myObject = [[MyClass alloc] initWithArgument: argumentOne];

Simple enough for just one argument (I added italics to make it easier to tell which is which). “initWithArgument:” is the method, and “argumentOne” is the first value passed in. When dealing with multiple arguments, it can appear confusing. Each additional argument gets an additional label and colon. So, a message to implement a method with multiple arguments would look like:

[myObject doSomethingWith: firstArgument andWith: secondArgument andAlso: thirdArgument];

On one hand, this is almost completely different from anything you see in other languages – it reads almost like a normal sentence. That said, it allows you to create very descriptive method names that tell you exacly what it does, and what each value is needed for.

Also, the arguments can be standard “C” datatypes like integers, and they can also be other objects. As a result, if an argument for a method is an object, any method that returns the required type of object can be used as a parameter:

[myObject doSomethingWithObject: [myOtherObject returnAnObjectAsParameter]  ];

So, nesting works both for the object being passed a message, as well as for any objects being used as the parameters of that message.

All of the data types we’ve dealt with are statically assigned, yet, Objective-C is a “dynamically” typed language. This is because of one other data type I haven’t discussed yet: “id”. The “id” data type is a “generic” object pointer that can point to any type of object. It is most commonly seen when receiving actions from UI elements, or in other methods where the type of data is unknown, other than being an object. 

The “id” type also doesn’t use the pointer notation. As a result, unlike the examples we’ve seen above, a variable for an “id” is declared without the asterix:

id myIdObject;

Unlike static typing, any object of the “id” type can receive any message, and as long as the appropriate method exists, will do whatever the method instructs it to do. This is great for flexibility, but the downside is that you also can’t use key-value coded parameters, or any other convenience that relies on knowing the class type. You also don’t get error-checking when compiling the program. For the sake of speed and compiler error-checking, when possible, use statically assigned data types. You can often use a parent “super” class to allow some flexibility without sacrificing all of the error checking.

For some more good information on naming and programming conventions, you can look at part 1 and part 2 of the article “Cocoa Style for Objective-C.” 

Python Variables

As much as I love Python, there are two things about it I’ve seen drive programmers experienced in other languages absolutely nuts. The first is the syntax. Instead of using curly braces to delimit blocks of code, Python uses indentation. The second is how variables are handled.

But wait! Aren’t variables like boxes with names, that we can put data into using the names?

Well, yes, and barring pointer operations in C and its derivative languages, that mental model often works perfectly well. For most languages.

So what makes Python different? First, the fact that everything is an object. Even basic datatypes like integers.

Second, variables in Python, like PHP, are dynamic. The type of data is not predetermined when you first create the variable name, or “identifier”. Because of this, you will never see a variable declaration specifying a type as in Java or C like this:

int car = 3;

As a result, the following commands are perfectly valid:

car = 3

car = “Ferrari”

Third, and most importantly, the variable identifier is just a reference to the data. Any data. Even integers. For example, if I create the variables “car” and “pet”, and set them both to 3, they both point to an integer object with the value of 3. If I set the variable “joe” equal to “car”, it doesn’t make a copy of “joe” – it just points “joe” to the same object as “car”:

8753197-9976584-thumbnail

For immutable data types like numbers and strings, the behavior is effectively the same as you’re used to in any other language, because if you change the value that a variable points to, it just gets redirected to the new value. For example:

8753197-9976599-thumbnail

 

Where things get interesting though, is when dealing with mutable data, like lists.

Try this out:

L1 = [2,3,4]

L2 = L1

L1[0] = 24

In the first two lines, you create a list, and set the second variable, L2, equal to the first. Since we are just passing pointers, then L2 actually ends up pointing to the very same list as L1.

8753197-9976608-thumbnail

So when, in the third line, we set the first item in the list L1 to 24.

8753197-9976617-thumbnail

Since both L1 and L2 point to the same list, the first value in BOTH lists is now 24.

Why was it chosen to make Python behave like this? I haven’t found the specific answer in either the python.org sites, or at Guido Rossum’s blog detailing the history and philosophy behind it, but It’s almost certainly a part of trying to make everything an object, while allowing dynamic variable types, and reducing the memory overhead as much as possible while also keeping you from having to deal with memory management.

One of the most useful consequences of these design choices is the ridiculous amount of flexibility in how you pass around and manipulate data. Since everything is an “object” – you are not restricted to “integers” and ‘floats.” Specifically, I’m talking about “duck typing” – named for the expression “If it walks like a duck, and talks like a duck.”. In the case of Python, (or id-type objects in Objective-C), what this means is that not only can you assign any type of data to a variable as we already discussed, but you can try to run any method on it you wish as long as the object supports the method.

This is fundamental. Unlike, for example, Java – where polymorphism is limited to methods of a class or superclass from which the class inherits – Python doesn’t care what type the class is – it just cares if the method exists. It doesn’t matter whether it’s a duck, a rectangle, or a car, it just matters if it responds to the method “quack()”.

So how do we deal with this?

Well, if you’re just passing the variable into a function to be read, it doesn’t matter. If, on the other hand, you want to actually make a copy, or create a new, modified data set without changing the original, you need to make a copy.

Even with copies we run into issues. Lists are objects just like anything else. What happens if the list you are copying contains lists? A simple copy of the top-level list makes a separate set of references for anything in the list, so now both lists seem to be entirely separate. Yet, if the list we are copying has sublists, our copy still contains shared pointers to those sublists, with all of the problems we saw above.

So first, unless you, the programmer explicitly ask it to, Python only passes references. This is never going to cause trouble when dealing with immutable data types.

Secondly, if you want to make a separate copy of a list or mutable datatype, you have to explicitly copy it by using the “copy” method, or a slice  like this:

aList = [0,1,2,3]

bList = a[:] # shallow copy of a using a slice

or

import copy #import the copy module

aList = [0,1,2,3]

bList = copy.copy(aList) # shallow copy using copy module

Third – if you have sublists or other mutable data types within a mutable data type, you need to make “deep” copies. Deep copies allow you to make separate copies of any sublists, dictionaries, or other mutable data that would cause issues in a shallow copy. They require you to either code your own method, or again, you can import the copy module, and use the deepcopy method.

import copy #import the copy module

aList = [0,1,2,3, [“a”, “b”, “c”]  ] # list with sublist

bList = copy.deepcopy(aList) # deep copy using copy module

Hopefully this helps someone out there trying to get their heads wrapped around Python variable handling.

Accessibility

Recently stumbled into this excellent programming related article on how to make iOS apps handicapped “accessible.” 

If you’re interested in programming for the iOS, you should read it. What’s interesting to note for everyone else though is this point – how easy it is for visually impaired people to use iOS (iPhone, iPad) devices, how much of that support is just simply “there” courtesy of the standard Apple interface toolkit, and how easy it is to make that support complete for many utility apps.

A number of the biggest iPhone/iPad fans I know of are visually impaired.

Different Kinds of Programming

An article at Time:Code on “Life Beyond the Browser.” Interestingly, it echoes some points I made about the different tool/learning paths required for web vs. desktop programming, but also explains part of why they are different, and use different languages. Math. I can state from my own experience, web programming uses relatively little math as opposed to anything even approaching doing desktop data optimization and animation. 

Getting Started With Programming: Tools of the Trade

In programming, the actual “code” programmers create is merely written text, and programs are little more than text files. As a result, the most important tool anyone can have is a solid, reliable editor. Sure, you can use Notepad to whip up a batch file, or TextEdit on the Mac, but most projects require more than Notepad provides, while the rich text features of Wordpad or TextEdit will only get in the way.

The nice thing is that to get started you don’t have to invest a lot of money in fancy IDE’s (integrated development environments) or hours downloading the hundreds of megabytes of XCode or its equivalent. The Eclipse IDE for Java – while fairly large still smaller than Xcode – is free, and most web or Perl/Python/etc. development can be done quite nicely with a broad selection of free editors. Outside of Java and Cocoa (Mac) programming, all of my coding is done in BBEdit.

On the Mac-only side, I recommend getting started with Textwrangler – the “lite” version of BBEdit. Despite missing some advanced features it has the critical ones – syntax color hilights, multiple-file searches, regular expression searches that let you search by pattern definitions, and my favorite file comparison tool for displaying the difference between two files.

For Windows, check out Notepad++.

Finally, for free, ridiculously powerful, and cross-platform (including Linux) there’s Vim and Emacs. Both have all the features you need and then some, come in command-line and GUI variants, and have fanatical adherents who will tell you at the drop of a hat why Vim/Emacs is awesome and the other one sucks. No matter which one you choose, you’ll end up with an editor powerful enough to handle your needs for years (One of the guys handling FX for the movie 2012 uses Emacs for handling 3D render programming…).

For more suggestions, take a look at this lifehacker page on the best text editors.

Learning to Program

Programming is an art form where we ask a question, and then write out very detailed instructions on how to answer that question. The instructions are written in a very limited subset of words, with a very precisely defined grammar. From this foundation, we can eventually end up at Microsoft Office or the latest XBox game.

Of course, there is a long, long road from writing out your first “hello world” tutorial to the latest word processor or Portal adventure from Valve. On this road, the byways will include a bewildering array of languages, options, tools, and a seemingly insurmountable mountain of functions, libraries, and bizarre vocabulary you’ve never seen before.

It’s very easy to get discouraged if you allow yourself to. The trick is to break the process down into chunks that you can manage, that give you some feedback, that let you go “Yes, I succeeded!” so you don’t feel like you’re churning away without making any progress whatsoever. This comes under the header of “Choosing your projects wisely.”

Actually, the first trick is to remember that trying to program gets you nowhere. You’ve actually got to do it.

What Have I gotten Myself Into?

A challenge. Like all worthwhile challenges, it will not be easy. The good news? You don’t have to invest a lot of time or money to get started. So first choose a goal – a mid-term one. 

What are you interested in creating? An iPhone app? An android app? A web-based app? The next Twitter? That will determine your starting point, your choice of starting languages, and what tools you need to pick up.

The path is not straight.

Keep in mind that programming, like math, depends on you learning concepts and then building on them. Programming as a process though, is not the same as programming as a language. Don’t get hung up on mastering PHP first if you really want to develop in Ruby “on Rails,” for the web. You don’t have to master HTML to start learning scripting languages for web programming, but you do need to know enough to build a webpage using those languages. Learn enough of the current toolset to start being proficient, and advance to the next one. Once you’ve learned the basics of a couple languages, the rest is mostly a matter of syntax and available functions. It’s how you learn to think about the problem, and break it down, that’s important.

For the web

For web-based applications, the baseline is to learn HTML. No, its not a programming language, but it IS a structured language that defines what kind of information you’re putting on the page, and where. It is used in combination with CSS: stylesheets that allow you to separately define how that information looks once it is on the screen. Since any programming for the web will involve manipulating and modifying HTML and CSS, you need to know these. HTML is also a subset of XML – which is used as a document and information storage format by many programs.

The next layer to add once you’ve become comfortable with basic tags would likely be Javascript as it’s easily integrated into any web page and required to handle any interaction after the page is loaded. 

For more advanced programming – the next step is learning how to change and construct what HTML, Javascript, etc. web server delivers to the user dynamically. PHP is a common language that is easily integrated into web pages and allows you to do this, while MySQL is a database language so you can store and recover data for later use. From there, you can pick up Java, Perl, Python, Ruby, or even C for more advanced programming.

Mobile and Desktop

Unless you’re building low-level driver code, this will eventually require you to learn a set of daunting “API’s” that allow you to open windows and otherwise graphically manipulate data. For Macs and iPhones, you need to learn Objective-C. For Windows, check other people’s recommendations, but you’ll end up learning some variant of C, C++, or C# and the .net libraries. For Android based devices, and a number of other applications, you will need to learn Java. 

Again, the real trick is to learn to think like a programmer. All else is syntax, and understanding what will happen when you type out any given command. The choice of languages here determines what types of features are already wrapped up for you, which ones you have to program yourself, and how quickly and flexibly you can write down the concept you’re thinking. 

There are arguments for starting in Java. If you’re planning on making a living outside of the Mac or Windows world, you definitely need to learn it. That said, I personally prefer Python as a starting point. It comes pre-loaded on Macs, it’s easily available for Windows, it has a very clean syntax, and it easily bridges the gap from simpler, “procedural” languages to learning object oriented ones. While relatively slow performance wise, it has been repeatedly used by experienced programmers to prototype out software for later optimization, or even tackle projects they never would have dreamed of in high-performance languages like “C”.

Either way, there are plenty of good sources of information and starting tutorials. Once you get your feet wet, you can start progressing into more complex languages that require more attention to details like memory allocation, but give you better performance in the tradeoff, or start learning the basics of creating Mac, Windows, Android, or iPhone apps.