Peter Cai

Technology and Software. Maybe cooking.

(Mis)Adventures in Ruby

Think fast! Why is this code wrong?

class User < ActiveRecord::Base

    before_save :update_precheck

    def update_precheck
        check_email_changed
    end

    # if the user changed their email then make sure we ask them to verify email again
    def check_email_changed
        if self.email_changed?
            self.is_verified = false
        end
    end

end

If you said “the model will never save any changes if you edit your email address because update_precheck will return false, which cancels the ActiveRecord save” – well congrats! You’re an awesome and experienced Ruby programmer!

For the rest of us, this sucks and is completely unintended behavior because of Ruby’s “I’m so cool I’m going to take the last statement in your function and make it an implicit return value” stunt. There’s a few things at play here: unfortunately this code will work some of the time, because before_save callbacks work if you return nothing OR true, and is only cancelled if you return false. So the lack of strictness in this regard actually hides how the function operates. Just for reference, here is the correct code:

class User < ActiveRecord::Base

    before_save :update_precheck

    def update_precheck
        check_email_changed
    end

    # if the user changed their email then make sure we ask them to verify email again
    def check_email_changed
        if self.email_changed?
            self.is_verified = false
        end
        # make sure we dont accidentally cancel saving if this is the last function called by update_precheck
        return true
    end

end

I try to avoid writing code that accidentally works, but unfortunately Ruby sometimes makes it too easy, apparently.

Edit:

This features reminds me of a good line by one of my heroes, Raymond Chen: Cleaner, More Elegant, and Wrong

How to Write a Software Resume

One of the fun things I did in college was screen resumes and interview candidates for various reasons: as a student programming lead, then as a technical adviser to nonprofits at my university (the trust conferred on me was rather novel at the time). The good candidates have stayed in my mind as vividly as the terrible ones. I later got to see hundreds of resumes while recruiting for Microsoft, and there are patterns to the best and worst resumes. Here are some of them:

  • Use keywords: C++, Java, Ajax, ATL, pthreads, MongoDB. Preferably in the context of stuff you worked on. I’m trying to judge how hard the projects were, and it doesn’t help if you just say “built an e-commerce system.”
  • Show some expertise. Don’t just spam keywords but rather clusters of related ones to show you’ve actually explored something in depth rather than just dabbling in 30 languages. “Java 6, Ant, JBoss, Spring & Hibernate” says a lot more than “Ruby on Rails, Python, PHP, Java, Lisp.” As a corollary, make sure you list where you worked with these languages under your experience. If you list “Java” a single time on your resume, I can pretty much guarantee you do not know Java.
  • Don’t give mundane details. If it takes you more than about a sentence to describe your role, you’re probably doing it wrong. I want to see accomplishments and specific projects and technologies you worked with. As a general rule, ruthlessly remove every word that can’t conceivably change my mind about you. “Filed reports and assisted customers with inquiries?” Shut up.
  • Don’t lie or exaggerate. Don’t even stretch the truth. “Assistant to the Manager” is not an “Assistant Manager.” A “4.0/5.0″ GPA is not “a 4.0″. Reviewers pick up on these tricks.
  • DO NOT PAD your resume. If you’re a college hire or looking for an internship, we understand your experience is light. This doesn’t mean you need to go on endlessly about your volunteer work or have 5 bullet points about your cashier position at CVS. I don’t need 4 lines of crap about how you worked at a dog shelter. Adding drivel de-emphasizes your relevant experience and drowns out the signal with noise.

This last point is particularly sensitive. Many people don’t feel comfortable filling out 3/5ths of page, or even 4/5ths. They must fill the whole page, with anything. I have seen college sophomores who insisted on 2 full pages, size 10. The perpetual fear among this population appears to be: “I am so young, how do I prove I am mature, responsible, and capable without bucketloads of meaningless accomplishments?” To that I answer:

  • Your grades. Especially in computer science, but also peripheral classes. It’s important that you do well even in courses you don’t like, because that’s the sign of a responsible adult (doing things you don’t want to do: story of a grownup’s life).
  • Length of employment and/or extracurricular involvement, rather than quantity (or needless details). Being able to stick through things for medium and long term commitments is the sign of a mature person.
  • Projects: open source contributions, bootstrapped web-apps, whatever. This is the next best thing to work experience, displays initiative, and is the best time for you to experiment before you get beaten down by the everyday stresses of life after college. True story.

Thoughts on Process

You_heard_me_crab_cig

Process sucks.

One of the things I’ve learned about process is that it tends to grow exponentially with the size of an organization. I’ve also learned that beyond its optimal point, process directly hinders productivity. Thus I have noticed that productivity tends to only increase logarithmically as a function of group size.

This is not a new observation. It is probably most famously studied in The Mythical Man-Month. Increasing manpower produces diminishing returns because work cannot be perfectly parallelized; and even when it can, communication lines increase and introduce overhead. What really drove it home for me was working the full gamut of companies – from the 3 man startup to the largest software company in the world. Read more »

The Teamwork Myth

IMG_2113

During my college years, I was constantly being bombarded by the message that teamwork skills were going to be essential in the Real World. This is true, but this idea was uniformly misapplied in the classroom. Many courses that were superficially about learning technical things like data structures or operating systems turned into an exercise in herding cats and dealing with people who couldn’t tell an algorithm from their ass. I’m sure that many other competent programmers before me and after me will have to deal with the same. This is sad because this teamwork “curriculum” is based on a lie. Read more »

There’s no Magic Setting to Increase Adsense Earnings

closer

Browsing the various SEO forums you will come across the wackiest and most incorrect advice you’ll find anywhere. Most of these are pure theories or oft-repeated “common knowledge” that turns out to be baseless conjecture. Still more have an air of pseudo-scientific evidence, often based on a single sample and with no controls for the hundreds of variables that can affect earnings from day to day. Some of these include: The day of week, the day of month, advertising budgets, the economy, the news, the time of year, the price of gas, the weather, inbound or outbound links… Read more »

IBM Software Engineer Interview Questions

4567783689

Here is a dump of some questions from an IBM software engineer interview.

Note, these questions are mostly behavioral, and are geared towards a student who will or has recently graduated from school. Copious notes are taken during this process.

Whether these should be questions of import in a software engineer interview, I will leave for another time.
Read more »