Text

And on the topic of improving my vocabulary, something I recently witnessed my coworker Brian write:

define_method :attribute_with_foo=
    send(:attribute_without_foo=, arg)
    more_code
end

alias_method_chain :attribute=, :foo

This piece of code was practically gibberish to me, so many foreign concepts wrapped in one small block. Going through it piece by piece:

define_method

This is something you can do instead of using 

def method_name
    &block
end

to define instance methods. In this context I guess it doesn’t make that big of a difference but define_method can be one-lined like

define_method(:method_name) { &block }

so that could possibly be useful for writing small gists. Also it could be used to dynamically define methods, but I haven’t yet encountered a use case for that. 

attribute=

I don’t even know how to Google this one to learn more about it… but I guess there’s not that much to it. Basically it’s just a method definition for the equals sign.  For example,

def foo= (bar)
    @foo = bar
end

where foo is an instance variable of a class. Then I’m able to do

class.foo = 'bar'

More simply put, this is how you explicitly define setters. The easier way of doing it in Ruby is just to declare whether the attribute is an attr_accessor, attr_reader, or attr_writer.

send

The first argument passed is a method of the object to be invoked. All succeeding arguments are parameters to be passed to the method. This method is useful for when you want to dynamically call methods.

alias_method_chain

And finally, this I found the coolest, though apparently it has been around since at least 2006, and is considered an anti-pattern? Basically the first argument you pass it is a method, and the second is… a word? Now whenever you call the method, it’s actually calling method_with_word behind the scenes, which you define. The original method is now aliased to method_without_word, which you can even call inside of method_with_word if you want, as seen in the example, so that method_with_word basically builds on it. 

This was useful to us because we had a method used pretty extensively in the codebase that we realized consistently needed another method call to go along with. It was a predefined method so we couldn’t just add to it, and doing a find and replace all could be dangerous, so this seemed like a good approach.

I can see why this method could be looked down upon since it’s practically made for monkey-patching, and no one likes that, but sometimes you can’t use inheritance..? I’ll have to look more into this later.

Text

I realized today that programming is not that different from any other trade. Every technique/technology I learn is really just an expansion to my repertoire to solve the problems encountered in more concise and optimized ways, to get the job done faster and better.

I can write if statements and for loops, but I still lack the creativity/breadth of tools to try different techniques in my implementations. I may have learned some algorithms in school but I’ve yet to really try them out in real life situations, I’m still relying on my bare fundamentals. Looking at my coworkers’ code versus mine, there really is a difference in the vocabulary. Most of my code is very hacky, making workarounds with very simple vernacular, while theirs is much more clean and terse.

How do I improve on that? I guess after learning new techniques, give them a try or two, figure out when and where they’d be useful, and keep them in the back of my mind for the future. Every new concept learned is another addition to my technological toolset. 

Text

It’s the little things in life

I’m pretty lucky to have been given web dev internships these past two years, considering I had only about 4 months of programming experience and zero web knowledge prior… well that’s a lie. I made a Dragonball Z fansite via Microsoft Frontpage when I was young, but that’s it I swear. I’m very grateful for all the insanely smart people I’ve had the opportunity to interact with, but I think in the process of diving into web and learning pretty much solely from these people who’ve had years of experience, I’ve skipped out on some fundamentals and taken it for granted. 

I used to think that servers were all massive supercomputers that basically contained the Internet. I had seen server rooms (below) and thats how I pictured servers. 

Slowly, after hearing the term used in the field in different contexts, it occurred to me that my definition didn’t seem quite right, and my mind tried to form a better definition. I never really thought to look up the correct definition until now though, pretty naive of me. A server is literally something that serves. More specifically, it’s any program that serves requests of other programs. Everything makes so much more sense now! 

Web servers were simply programs that delivered applications/pages asked of them, that they contained locally, across the web (they really did contain the Internet!). Database servers delivered the information stored inside the databases to the aforementioned applications. Pubsub servers, cache servers, etc etc, you get the story. Servers didn’t have to be these bulky machines, your own computer could have/be a server. In fact it could have multiple servers! The reason there are the bulky machines and rooms full of them is because each computer’s hardware can only handle so much, so having bulky ones with better hardware and lots of them splitting up the serving, crashes aren’t as likely and request response times are sped up. 

Before I had simply followed guides for setting up dev environments and didn’t really understand what I was doing. Now I actually get what’s going on when I install Apache and MySQL: I’m actually setting up servers! Well, I guess I still don’t get what’s going on, but I have a slightly better understanding now. One step at a time.

This makes me think that there are other things that I’ve made assumptions about that I should dissect and better understand. Probably more to come of this stuff later.

Text
if (typeof foo === "function") {
   foo = foo();
}

My co-worker Jonathan showed me this yesterday. “foo” is a parameter passed into a function to do whatever. Itself may or may not be a function. If it is, run it and set the return value to be the value used for whatever, otherwise just use the value. Took me a sec to wrap my mind around it, pretty sure you can’t do something this crazy in other languages. Javascript sure is interesting with its quirks!

Edit 06/10/12: I stumbled upon a python snippet that implemented the same madness. Looks like Javascript isn’t the sole proprietor here.

It turns out this concept isn’t just badass looking, but is also useful in efficiently trampolining (replace the ‘if’ with a ‘while’), which basically simulates tail recursion. This is valuable in Javascript since Javascript doesn’t have tail call optimization.

Text

Concepts

Bitwise Operators - Scott taught me about these today. ‘|’ (or), ‘&’ (and), ‘«’ (shift), ‘^’ (xor), etc. They’re done at the hardware level, meaning super efficiency. It’s basically the same as the operators in circuits, except codified / in decimal representation. For example, 3|4 = 011|100 = 111 = 7.

Bitmasks - Imagine you’re conducting a survey on people’s diets (this is probably not a great example) - eats chicken vs doesn’t eat chicken, drinks milk vs doesn’t drink milk, eats beef vs doesn’t eat beef, etc. You could record each person’s every answer as a separate string/boolean, or you could record all their answers as one number. With 1 being ‘eats it’ and 0 being ‘doesn’t’, 100 = eats chicken, doesn’t drink milk, doesn’t eat beef. 101 = eats chicken and beef but doesn’t drink milk. Bitmasking is using bitwise operators to test against conditions. In an ‘if’ statement, I can test if the person drinks milk by performing a bitwise ‘&’ on their answer with 010. If the result is greater than 0, then the person drinks milk. Yes this could be done with other conditional tests but from what I gather, bitmasks / the whole binary representation concept in general is more lightweight (1 bit per test), albeit possibly harder to understand.  

& - Another magical Ruby shorthand. This one took me a while to understand, and I have to admit even now I’m a bit shaky on it. Basically if used on a Proc (variable which references a method), it’ll convert it to a block. If used on a block, reverse happens. This article did a good job of explaining it. The only thing I’d add is that in work we use it a lot for mapping. Normally to map you’d do

foos.map { |foo| foo.bar }

which performs ‘bar’ method on every item (foo) in ‘foos’. You can shorthand it to

foos.map &:bar

The & converts the ‘bar’ symbol to a block and calls it on each iteration, doing the same thing as the long(er) version. 

Law of Demeter - A general guideline for loose coupling / making your code more maintainable and safe. Doubt I can explain it better than that article, it’s a pretty easy read.

Technologies

Page Speed - Extension for web developer tools which tells you how to improve page performances

RDebug - Great gem for debugging / stepping through code in Ruby. This article explains pretty well how to use it

A/Bingo - Possibly one of the ugliest sites I’ve ever seen but very neat plugin for Rails A/B testing

Cool Articles

Linux Fundamentals 

Photoshop pushing photo magix further with unblur

Text

I suck at titling

I’ve recently passed the one year mark for programming! Oh how exciting it is to be a noob. A time when if someone asks “How does that work?!?”, your only response is “magic”. It seems many web entrepreneurs are already super successful at my age, with years of programming and other experience under their belt. That just means I’ve got catching up to do. Lucky for me I’m surrounded by many bright minds and everyday I get to learn a myriad of cool, new things. 

Because I have terrible memory and would like to remember all of said things, I’ve decided to write it all down (in blog-form). Well, not everything. Just the programming-related things that I may forget about in the future and that probably will come in handy (sorry non-programmers, see my Twitter if you actually want to hear about other aspects of my life). 

In this blog I’m going to talk about programming tricks/concepts I recently learned/found cool or difficult and technologies/news I’ve recently discovered. ”Why would I read this rudimentary stuff” you ask? Well, your guess is as good as mine. This mostly serves personal purposes so if you’re reading it, you must be really bored, a good friend of mine, or a novice like me looking to have something pretty basic explained. I’ll try to explain concepts in layman’s terms, or link to articles that do so if you’re actually here for knowledge, hope I am of help.

Note: I’m just going to blog about things as I learn them so there won’t be much flow or categorization to my posts. Also, I’m very much an amateur writer and programmer so feel free to call me out if anything I say is audaciously flawed or flat out wrong.

Link

We’re both on co-op. We both have a lot of free time. This is the result. 

Video