blog podcast

The Hidden Constraints

Most programming languages do a wonderful job of hiding complexity. Often the compiler can do a good job of figuring out what you want to do, and wires things to your likings. Sometimes though it’s not possible. That’s when you start interacting with the hidden constraints of the language.

I became quite conscious of these as I moved from developing in C# to work with Scala. All of a sudden things like mapping json needed translators that were created using Macros. I couldn’t understand why at first, I mean Java and C# are quite similar, why this difference? I believe the reason is because of the type information in generics being a compile time feature in the JVM world, but not in C#. It’s a small detail, but it makes you encounter hidden constraints.

This also becomes conscious to me when I’m working in Rust. It’s kind of an exciting journey, because suddenly things that you can casually do in Javascript for example becomes a lot more complicated. With great power comes great amount of extra work.

Don’t confuse this with complaining. I actually like that fact that you come closer to what’s happening at the lower levels. It enables you to understand and to be able to choose better solutions.

Code of the Day

Just for the sake of it I’ll add some Rust code. I solved a leetcode quiz today, and I needed more than a 100 lines of code to do it. Next iteration will be to try to make the solution half as big. This is how you can learn a language, one problem at a time.

impl Iterator for ButtonsIterator {
    type Item = String;
    fn next(&mut self) -> Option<Self::Item> {
        if self.done || self.buttons.len() == 0 {
            return None;
        }
        let mut response = String::with_capacity(self.buttons.len());
        for it in &self.buttons {
            response.push(*it.letter());
        }

        self.increment_iterators();

        Some(response)
    }
}

impl ButtonsIterator {
    fn increment_iterators(&mut self) {
        if self.buttons.len() == 0 {
            return;
        }

        let mut i = self.buttons.len();
        let mut incremented = false;
        while i > 0 {
            i = i - 1;
            if self.buttons[i].can_increment() {
                self.buttons[i].increment();
                incremented = true;
                break;
            }
            self.buttons[i].reset();
        }

        self.done = !incremented;
    }
}