Ricardo Delfín
Ricardo Delfín

Week 1: 30/8 - 3/9

The UT Tower as seen from Martin Luther King Boulevard.

It's been an interesting first week of class. It began with a canceled flight and having to wait until Wednesday evening to catch the next flight back. Either way, I'm now back in Austin and ready to go through my last semester at UT Austin. Since there's less to talk about for this first post, I'll jump in to answering a couple questions.

What are your expectations of the class?

In this class, I hope to improve the quality of the software I write and just hone in my software engineering skills overall. I've noticed in my past internships and the personal projects I've worked on, that my biggest enemy has been code quality. I can usually come up with an algorithm or a piece of code that will solve a given issue quickly. However, I'm generally not very good at testing it, documenting it, or keeping it generally readable. My hope is I will be able to improve those skills in this class.

What did you do this past week?

I dedicated most of the last week to settling back in Austin and my new class schedule, interviews, and interview prep. The interviews and interview prep were particularly useful.

Something I realized how easy it was to quickly loose practice if you're not programming on a daily basis as much. I got a very strong contrast with my interview season from last year where I had just finished a software engineering internship (as opposed to the product management internship I did this summer). My performance difference was measurable. This pushed me to try to study more.

What's in your way?

The biggest blockers I currently have are some of my classes. I'm still getting back on track with school schedule and it's very easy to fall behind early on. I've already fallen a bit behind in a world literature class. While I've enjoyed the readings, I am not quite reading them at the pace I should. This could impact my other classes if I'm not careful.

What will you do next week?

Now that my schedule has fully settled down, I'll dedicate next week to organizing my life by finding a good way to keep track of everything I have to do and when everything is due, as well as my progress. A favourite of mine on this is the android app timetable. I'll also start working on all the projects that are coming up in all my CS classes, including Software Engineering.

What's my experience of the class?

The first class I went to on Friday was quite interesting and different from a lot of CS classes I've taken in the past. It felt very applicable. An interesting dynamic I've seen teachers rarely use, but that I saw being very useful in this class was the teacher asking random students questions. This made sure that everyone was engaged in the class. I quite enjoyed learning the basics of how docker works, which I had never used.

What's my pick-of-the-week or tip-of-the-week?

While I learned this a while ago, it's been a topic that's been going around my head a lot lately. That's the concept of Type Erasure in C++. I felt like it was an extremely clever trick for anyone writing libraries in C++.

It works like this. Suppose you have two classes with a method called sound() with the exact same signature. They essentially do the same thing. However, this is only due to a convention and nobody created a class that implements both. You can see these below:


class Dog {
public:
    std::string sound() { return "Woof!"; }
};

class Cat {
    std::string sound() { return "Meow!"; }
};
                

This, by the way, happens over and over in the C++ standard library, with methods like push_back() showing up in multiple places (both in the deque and vector classes). What if you want to encapsulate these and any other class with this method signature in a class, when you can't change these two classes?* You could encapsulate each class in a parent class and have those extend another class with a virtual sound() method, like so:


class Animal {
public:
    virtual std::string sound() = 0;
};

class DogCapsule : public Animal {
public:
    DogCapsule(Dog d) : d(d);
    virtual std::string sound() { return d.sound(); }
private:
    Dog d;
};
                

And do the same with the cat class. This is nice and lets you create a method that takes in an Animal. The problem is this will not scale well. You need a capsule class for every new class. Instead, you can make use of templates to automatically create this encapsulating classes:


class Animal {
public:
    virtual std::string sound() = 0;
};

template <typename T>
class AnimalCapsule : public Animal {
public:
    const T &animal;
    AnimalCapsule(const T& animal) : animal(animal) { }
    std::string sound() { return animal.sound(); }
};
                

And now, you can use the AnimalCapsule as a parameter into a function, and any class that contains a .sound() method with an appropriate signature will be converted into an AnimalCapsule() by the implicit copy constructor (or you can wrap it in the class by calling it explicitly). There's more to this pattern but this is the basics. This is very similar to how std::function works in C++ and how it converts disparate functors into instances of std::function, with the only requirement that they have operator() defined.

I'd like to thank Google Engineer Ivan Serna for introducing this in his C++11 class, as well as this blog post that helped me understand and write about this topic I found extremely interesting.

* There's a lot of reasons you would do this. You might want some common interface to be able to pass as arguments into methods, without caring what the underlying class is. This takes the burden of defining what methods you need in a function of the person designing the classes over to the person using them.