Attr_developer, you can do it.

This post is a response to chapter 2 of Sandi Metz’s Practical Object-Oriented Design in Ruby (POODR). I applied the teachings from this chapter to transform data (instance variables) into behavior (methods). The result is a class that is easier to change, and class objects that are easier to read and write.

In the example below, I created a simple Artist class, which demonstrates the magic of using attr_accessor. attr_accessor transforms instance variables into reader & writer methods that you can use to get or set data to each instance/object of your class. For the longhand version of attr_accessor, see lines 14-22 below. attr_accessor obviates the need for writing these methods out. However, it is important to grasp their syntax.

The beauty of transforming data into methods is that you don’t have to use instance variables (@name, @song) throughout your code. More important, by replacing instance variables with their attr_accessor methods, you are able to change a name or song on a whim without changing your code. Metz summarizes this trick: “Implementing this method changes [name] from data (which is referenced all over) to behavior (which is defined once).”

Advertisements

Optimize later alligator

My greatest strength as a developer is also my greatest weakness. I have a tendency to want every detail to be perfect, a habit that can produce great results for a solution to a single problem. But I can get so lost in the details that I lose sight of the greater goal, such as checking off all the problems in an assignment, or realizing that if I don’t get the current problem, the next one is apt to teach me something I can use to refactor it.

In my short two weeks at the Flatiron school, I have made some good progress at softening this myopic tendency. This weekend is a good example of the yin and yang of my perfectionism. Saturday, I managed my study time without stellar expectations, and made a lot of progress on a number of assignments. A flow emerged, and solutions piggy-backed across topics.

Today, however, I got lost in trying to perfect the appearance of my student app. I made minimal progress doing that; as a tradeoff I only completed one of the major back-end challenges thus far. Comparing the two days is a great lesson. I am finding that time used wisely is often time used experimenting with multiple projects and without great expectations.

Optimize later alligator

My greatest strength as a developer is also my greatest weakness. I have a tendency to want every detail to be perfect, a habit that can produce great results for a solution to a single problem. But I can get so lost in the details that I lose sight of the greater goal, such as checking off all the problems in an assignment, or realizing that if I don’t get the current problem, the next one is apt to teach me something I can use to refactor it.

In my short two weeks at the Flatiron school, I have made some good progress at softening this myopic tendency. This weekend is a good example of the yin and yang of my perfectionism. Saturday, I managed my study time without stellar expectations, and made a lot of progress on a number of assignments. A flow emerged, and solutions piggy-backed across topics.

Today, however, I got lost in trying to perfect the appearance of my student app. I made minimal progress doing that; as a tradeoff I only completed one of the major back-end challenges thus far. Comparing the two days is a great lesson. I am finding that time used wisely is often time used experimenting with multiple projects and without great expectations.

For each floorboard, count the number of cracks in it

One of the best books I’ve ever read is Fun Home by Alison Bechdel. Her portrait of growing up gay in a small town with secrets everywhere is heartbreaking and breathtaking and everyone should read it. It transcends LGBT issues and speaks to all of us who’ve ever lived and suffered through lies.

In the book, Alison develops a pretty severe case of Obesessive-Compulsive Disorder to control the bogeyman she has to live with (her father). She creates rituals like counting the cracks in each floor board as she crosses the threshold from one room to another. This pathological performance piece worked very much like an each loop in ruby: With each floorboard, Alison would pass the board to her OCS method (i.e., each) and proceed to sum the number of cracks in the board. We can quantify the disorder as such:

OCD = 0
floorboard.each do |crack|
OCD += crack
end

fun home interior 2

two paths to the same buzz

Let’s talk about how we get to two different solutions for the game FizzBuzz.

Solution 1

Solution 2

The first solution uses a while loop to iterate through numbers 1 to 100. Using if/elsif control flow, we achieve the desired outcome of fizz for numbers divisible by 3, buzz for numbers divisible by 5, and fizzbuzz for numbers divisible by both 5 and 3.

The second solution employs the each method on range 1..100, storing the various instances of divisibility by 3 & 5 in two variables: m3 & m5. Next, the uses a case statement (as the condition of the iterator) to print out the values fizz, buzz, fizzbuzz, or i.

Do you know where you are?

Half the time I encounter a problem on Git, it’s because I’m in the wrong folder. This is the worst kind of git K-hole, because it’s so easy to fix but also so easy to overlook when you’re first getting the hang of (g)it.

Today, I kept trying to add the readme file, commit it, and then push it to Github. In response, git would barf out…

Image

This drove me bonkers because I was certain that I had edited the readme file and fixed the conflict. But I hadn’t looked closely enough at the error message. Is says the file name is “../README.md,” which to the careless and untrained eye can easily be mistaken for README.md. However, the difference between the two is vast. The former “../README.md” file name specifies that the file i needed to fix one was directory above my current directory. I had been fixing the wrong readme file the whole time.

With that discovery, I simply had to follow these steps:

1. cd .. (to move up one directory)

2. subl README.md (to open the file in Sublime and resolve the merge conflict)

3. git add . (to track all the files in the correct directory, including readme.md)

4. git commit -m “Finally! Resolve merge conflict!” 

5. git push origin master

Phew. Here’s to knowing which directory you are in at all times!

 

 

 

Optimism, pessimism and everything in between

People have good days and bad days. In social situations, perceptions are often skewed and cognitive distortions are magnified in sometimes frightening, often hilarious ways. Human nature is a messy affair, and I’ve found that the best way to deal with emotional chaos is to laugh it way, make fun of it in you, make fun of it in me, call us all crazy and just forgive it. I am wary of socially engineered optimism, because I think it denies the full spectrum of human experience. For the same reason, I am wary of socially engineered pessimism (I’m thinking of that South Park episodes about the emo, goth and vamp kids). I try to sit somewhere in the middle thinking of ways to laugh about the extremes.

That said, in the classroom, I think extra optimism serves a very real and necessary function. It teaches us all to treat each other with respect and compassion. It helps motivate me when I’m down, and it helps motivate you when you’re down. I wouldn’t advocate singing Christmas carols all day long, but willingness to help each other along through this process is essential.