MacKuba

🍎 Kuba Suder's blog on Mac & iOS development

JavaScript template libraries

Categories: Frontend, JavaScript Comments: 4 comments

For the last 3 months I’ve been working on a new web application at work. It’s quite unique in some regards, from the architecture perspective; the biggest difference from other projects that I’ve worked on is that almost entire page is one huge embeddable “widget”. This requires a completely different approach than I usually use:

  • a lot of the code above the model layer is moved to the client side (i.e. JavaScript); this means that controllers and helpers are rather simple, controllers mainly return JSON, and there’s quite a lot of JavaScript to write
  • since a significant part of the system is written in JavaScript, it needs to be unit-tested too
  • I have to be very careful not to cause any JS, CSS or DOM id conflicts between the embedding site and the “widget“ (which includes such things as keeping all JS code in a single global namespace, and using jQuery in the “noConflict extreme“ mode through an alias)

Another thing, which I’d like to write about today, is the way the views were implemented in this project. Since entire GUI is created dynamically by JavaScript, I had basically two options:

  • render the views in Rails with ERB and send big chunks of HTML via AJAX to JavaScript;
  • or make Rails send only data as JSON, and render the views on the JavaScript side.

Read more »

JavaScript unit testing

Categories: JavaScript, Ruby/Rails Comments: 6 comments

I’ve read a lot about good programming practices recently. I’ve read the “Pragmatic programmer” book (which is awesome, one of the most useful books I have read, seriously); I’ve watched a great presentation “Craftsmanship and ethics” by Robert C. Cooper. And it seems that everyone seems to emphasize that one thing that is extremely important to write good software is writing unit tests (and writing them “all the fucking time”, as that black dude said in that presentation ;). I must admit I still haven’t had the courage to switch to TDD (although I try it for single tasks from time to time), and my test coverage is nowhere near what it should be ― it ranges from 20% to 80% depending on the project and its layer. But I know it’s important and I’m working on it…

Anyway, one day I had a thought: even if I test all models and controllers thoroughly, am I not leaving something out? Didn’t I forget about something that is a quite important part of the application ― about my JavaScript code? After all, it’s code too, right? And sometimes it’s very important code; and not having any tests for it means the code is very fragile, it’s easy to break things, already fixed bugs may reappear again, and so on. Of course, JavaScript may be harder to test, because it’s sometimes very closely coupled to HTML, but at least some part of it could surely be tested.

But how do you unit test JavaScript?… I had no idea how to do this.

So I started googling, and I found that there are plenty of different unit test frameworks for JavaScript. Of course I couldn’t resist and I had to take a look at every single one and compare them to choose the best one :) (I heard that it’s called “maximiser” and that it’s bad…). The result of this looking is the list below.

Read more »

Hello, iPhone!

Categories: Cocoa, iPhone Comments: 0 comments

For the last few months, I’ve been spending a lot of time learning how to create apps for the Mac. Since I’m slowly starting to run out of new tutorials to read, I’ve decided to find myself something new to learn ;) Well, not completely new, actually, as a lot of that knowledge about Cocoa is going to be very useful.

I’m talking about iPhone development, of course. I bought an ebook about iPhone SDK from Pragmatic Programmers some time ago, which looks very promising, but I didn’t really start learning, because I wanted to get the iPhone development certificate first, and that took some time. You see, Apple’s platform is so damn open that to install even a “Hello world” on your own phone, you have to have a set of certificates from Apple, which they give you only after you register for an “iPhone developer program” which costs 100$. Really, guys, this is not how you attract developers to a platform… :\

Anyway, I got the certificates now, so I was finally able to run the “Hello world” on my phone :) Here’s a proof:

Hello iPhone (screenshot of my first application)

Read more »

The Dark Side of Cocoa

Categories: Cocoa Comments: 6 comments

An empty cup of cocoa
Photo by Alexander Staubo (CC)

Some time ago I wrote about all the things that I loved in the Cocoa framework. This time, I’d like to write a bit about the worse side of Cocoa – the things that annoy me, confuse me, and make me wonder who the hell came up with such an idea…

So, the things that dislike in Cocoa are:

  • Long names. I know, some people say that explicit is better than implicit, also, there are no namespaces in ObjC, which may explain some of that. But I’ve seen names which really belong on The Daily WTF. The longest I’ve found so far is “NSManagedObjectContextObjectsDidChangeNotification” (51 characters), and I’m worried that this might not be the longest one yet… Are you telling me a name that takes half a line in my editor is more readable than a short one? Seriously?

Read more »

Interface Builder tips and tricks

Categories: Cocoa, Mac Comments: 1 comment

I read Apple’s tutorial about Interface Builder this week. Most parts of it told about rather basic things and were quite boring, but I’ve learned several useful tricks from it. IB, like most of Apple’s applications, has lots of hidden features which aren’t obvious at first, and can only be found by experimenting, by accident or by reading about them somewhere. Here’s a few things that I’ve learned from the tutorial – some of them are such hidden features, some are just things that weren’t mentioned in any source I’d read before.

Read more »

Code like a hobo

Categories: Ruby/Rails Comments: 6 comments

For the last 2 months, I’ve been using a new Ruby framework at work. A very… interesting framework :) It looked very promising in the beginning, only to become a complete disaster a few weeks later. I thought I could share some of my experiences with it here, although not to recommend it, but rather to warn you not to use it…

Its name is Hobo, which makes most people think of homeless people, and makes it a constant object of jokes in my company (see below :)…

'Hobbo land' sign
A sign hanging in my room – copyright by Wombat

Read more »

Generating scaffold interfaces in Cocoa

Categories: Cocoa, Mac Comments: 0 comments

I discovered an interesting feature in Interface Builder a few days ago. It seems that it can generate a scaffolding UI for you based on a CoreData model. It works like this: first you have to design a data model in XCode model editor (which you have to do anyway if you want to use Core Data; if you don’t, it won’t make sense to draw the model only to get the scaffold UI, it will be faster to make it yourself…). Let’s say you have a model like in this picture:

XCode data model editor

Read more »