ProgFu

programming tips & tricks

Jan 24

Enough abstraction, it’s time for Arduino

update: this article was originaly posted on my wordpress blog on 10th November, but since I’m unable to set a publish date on tumblr, and I felt it would be a shame to just leave it there, I’m republishing it here.

Over the past few years, I’ve been moving up in the levels of abstractions in programming languages, from AVR Assembler all the way up to enterprise Java and Ruby on Rails. Right now I’m at the point where I can work with a database without even knowing whether it’s MySQL, Postgre or SQLite.

At first I thought this is great, because it makes a lot of things simpler, but it also has some drawbacks.

It’s been a couple of years since I’ve worked with MS SQL Server and wrote some pretty decent SQL and it looks like I’ve lost it all. Just this month I had to move two not-so-small databases from one database server to another. First one from SQLite to MySQL, and second from MySQL to Postgres.

That made me thinking – if I didn’t have all those ORM tools, would I still be able to program the same things? Did my brain degenerate by using super-high level libraries? Working with Rails is sometimes really dumb easy. Gems can solve so many problems without even having to think for 5 seconds. For example, today I needed to add some static pages to my Rails application.

It took me about 20 seconds to google the amazing Thoughtbot’s High Voltage gem which solves this problem almost instantly. All you have to do is add it to your Gemfile … and yea, nothing more, you’re finished … the hardest part is to put the view file in a correct directory and optionally set up routing to have nicer URLs.

Things like this can really save time, but I feel that they make me really dumb at the same time. I can work much faster than I did 5 years ago when I was doing raw ugly PHP, but it doesn’t feel like programming. All I’m doing is copy & pasting little bits of code.

And that’s why I decided to challenge myself. I’ve always wanted to do some low level programming and since Pragmatic Programmers released a great book on Arduino, I decided to buy one.

Unfortunately I wasn’t able to find a reliable vendor in my country, which means I’m going to have to wait for a delivery from US, which is going to take some time. At least I have time to prepare and get all of the necessary components. The next article about Arduino will be an intro and first impressions.


The story of TextMate, VIM and SublimeText

I always preferred heavyweight IDE to do most of the stuff. I tried many IDEs for Ruby, even purchased two upgrades for RubyMine, but it never felt right. I wanted to do the same things that IntelliJ can do to Java, all those beautiful hardcore refactorings. But those just don’t exist for Ruby.

I used my IDE to do source control and I didn’t really understand the command line tools. I knew how to invoke Rails generators from the IDE, but I wasn’t really confident doing the same from the command line.

When TextMate came, it changed the way I think about programming. Instead of complex GUI, I started using command line much more, I started to prefer lightweight tools that do the job right instead of relying on the IDE. Just being able to cd into a directory and do mate . and instantly work gave me a lot of flexibility. No need to create a project in an IDE, set up directory structure, libraries etc.

This lasted me for about a year. I was very happy with TextMate, even made some custom snippets for the code I write the most. Actually I created a lot of snippets for various things, because I’m a really lazy typer.

About a month ago, I saw the Destroy All Software screencasts by Gary Bernhardt. It struck me that my editor skills are nothing compared to what he does in VIM. Being able to see someone skilled at VIM work is kind of a revelation. It makes you see things that seemed impossible before.

So after the first 10 minutes of the screencast, I decided I absolutely have to learn VIM. There’s just no other way.

It’s not like I never used it before, I’ve been doing some system administration for my virtual servers and used Linux on my desktop occasionaly, which forced me to learn some VIM. But I never used it beyond editing simple shell scripts. I always gave up too soon.

But this time it was different. I could see someone actually use it. Up until the moment I saw the screencast, I didn’t really believe it was possible to use VIM instead of a complex IDE and achieve the same productivity, but I couldn’t have been more wrong.

So I gave VIM another chance, but this time for real. I watched all VimCasts, downloaded every single cheatsheet there is, and spent long night hours on vimgolf.com trying to improve my VIM skillz. The only thing that was holding me back was navigation between files, since I was used to the PeepOpen Application.

There is a perfect alternative for VIM, and that is the CommandT plugin. It does exactly the same thing, provides a fast fuzzy search across files in the whole project. For example if I want to open app/models/user.rb, I can just type amouser.

But this is not the end of the story. A couple of days ago, I got to try SublimeText 2, since many people recommended it to me. First thing that instantly struck me is that SublimeText is fast, and I don’t mean just fast. People say that VIM is fast, but that’s not true for large files, and definitely not for CommandT with large projects. If you have a file with more than 200 columns and try to do anything, it will lag, at least it did for me.

SublimeText brings speed to a next level, at least for browsing the project. With its fuzzy search, you’re able to do the same exact things as with PeepOpen, and even more. It allows you to jump directly to a method in a file using the # syntax.

For example if I want to go to a logged_in? method in app/controllers/application_controller.rb, I can just type aconappcon#login into the search field.

As a result, I bought the license for SublimeText after a couple of hours of using it because it just feels right.

There are tons of other features in SublimeText that are just amazing, but I’ll cover those in a followup article, and I will do the same for VIM.


Oct 3

WordPress query_posts with pagination

This is probably the first issue I stumbled upon ever since I started working with WordPress; which is surprising because I really expected hacking in PHP templates to be much harder and error prone (but more on that in following articles).

The issue comes up when you want to display posts based on some specific criteria, for example listing the most recent posts from all but one category. There’s an amazing query_posts().

When I first realized that I need to display only something and not everything on the main page, I got a little angry, because I expected I will have to code all the stuff myself, but WordPress surprisingly offered a very easy solution.

Specifically, I needed to exclude a category #5, and all that needs to be done is call query_posts() right before the main loop.

 <?php query_posts('cat=-5&posts_per_page=3') ?>

 <?php if (have_posts()) : ?>
 <?php while (have_posts()) : the_post(); ?>
    // blah blah usual stuff

Note the cat=-5, which says I want everything EXCEPT for the category #5.

Now there are two downsides of doing this. First, the query will persist even if you have multiple loops, so you need to reset it back by calling wp_reset_query() after the main loop, which isn’t such a big deal.

Another thing that stops working though is the use of next_posts_link() and previous_posts_link(), because once you call query_posts() WordPress won’t automatically apply the page parameter; you will have to pass it manually.

 <?php query_posts('cat=-5&posts_per_page=3&paged=' . get_query_var('paged') ); ?>

Yes it is very simple, but also very easy to forget. The bottom line is every time you call query_posts(), be sure to check your pagination.


Sep 18

Back from the Dead

It’s been a while since my last article. I’ve been neglecting this blog, but that’s about to change now. I’m going to try to write regularly and at least post some progress on my current projects and learning new languages along the way.

Specifically, I’m trying to learn Scala, which is a language that I always considered interesting, but never really got into it because I thought Ruby is the only thing I need. It turned out that you should never be 100% sure of anything, because there will always be times when it’s not true, and that’s the case of Ruby. I’m going to get more into this in the following articles.

Another thing that I’m getting into a little bit is ActionScript 3. The dreaded Flash has finally caught my attention, so I’m probably going to write something on the ugly and messy ActionScript too.

Last but not least, I’ve finally got to work with WordPress. Not just as a user, but theme development in particular. I’ve been afraid of this for a long time, but since everyone on the web is using it, I should at least have some knowledge to be able to manage my pet projects a little bit better.

I might even get a little bit into plugin development, but I’m not sure about that, since PHP is something I’d like to avoid in the future.

So much for an introduction, see you at the next article!


Jun 15

Join the community on smallZOOM

It’s been a while since my last article, but here I am with something new. I’d like to show you one of my not so recent projects, that never got much exposure.

The idea behind this project is, that when you want to create a service or just a simple website, it’s important to get some feedback, so you know what to improve.

That’s where smallZOOM comes in. By posting your project, you allow other users to comment and vote, which builds up your karma. You can also vote and comment on the feedback and ideas you recieve.

Join the community on smallZOOM.com


Feb 20

Deferred Objects in jQuery 1.5 - AJAX

The latest version of our beloved jQuery introduced a couple of new features, one of which is called Deffered Objects. Now what is that exactly?

We often need to introduce some sort of a callback mechanism in JavaScript, where a function is invoked once a task is done. The most popular usecase would be AJAX.

Read More


Feb 3

Stash it with Git!

How often do you change something and one minute later it breaks and you want to go back to the working version to debug it or just run it again to see how it works.

But what do you do with all those changes you already made? Since something isn’t working, you probably haven’t commited anything to the repository yet.

Read More


Jan 9

Your Code is Not Self Documenting

There is this movement that people should try to write a self documenting code. Such code doesn’t require any comments, because it is obvious what it does. While that might be true, it’s not the whole truth.

Read More


Jan 5

Dec 23

Sinatra and SQLite on 64MB RAM

This article is a direct followup of Ruby on 64MB RAM. If you didn’t read it, please do it now.

No MySQL

Since we’re running on very low resources, we need to think carefuly about every process we want to run on our server.

Read More


Page 1 of 2
Bloggers - Meet Millions of Bloggers
Web Analytics