|
| 1 | +Title: Types: Let's Rant/Ramble |
| 2 | +Date: 2025-01-30 18:53 |
| 3 | +Modified: 2025-01-30 18:53 |
| 4 | +Category: Posts |
| 5 | +tags: |
| 6 | +cover: |
| 7 | +summary: "Types" are an important concept in programming, let's collect some thoughts around this topic |
| 8 | + |
| 9 | +Hi, I'm Adam. You might remember me from years back as someone who called |
| 10 | +himself the codependentcodr. Then I disappeared for a few years (!!!) as well, |
| 11 | +life just changed. But recently my therapist encouraged me to find a creative |
| 12 | +outlet outside of work. A place where I could find that ever elusive *flow |
| 13 | +state*. Hence, I thought it time to revisit this blog as an outlet for |
| 14 | +expressing my creative self. We'll see where this goes, if I get back into it |
| 15 | +like I did years ago, but at least for now it's just a chance to write. Also, |
| 16 | +is writing about stuff from work really an outlet outside of work? 🤔 But I |
| 17 | +digress, let's start writing again. |
| 18 | + |
| 19 | +So, the first question becomes: "Hey Adam, it's been years, what are you going |
| 20 | +to write about?" |
| 21 | + |
| 22 | +Hmm, good question. Traditionally this blog was always "hey I learned something |
| 23 | +(usually for work) and now I'll recap what I learned to share the knowledge". There was a time when |
| 24 | +that was not altruistic, like I had this vision of becoming the next |
| 25 | +[Wes Bos](https://wesbos.com/) |
| 26 | + or |
| 27 | +[Scott Tolinksi](https://tolin.ski/) |
| 28 | +, starting my own "Teaching Devs" empire. But now, I think this |
| 29 | +really is just a chance for me to scream (ok, maybe not *scream*, but speak |
| 30 | +aloud) into the void and if anyone other than my current self gets something out |
| 31 | +of it, hey that's some sweet, sweet icing on that proverbial cake. |
| 32 | + |
| 33 | +So, recently I've been having a few conversations at work with colleagues about |
| 34 | +*types*. You know those little categories we put on variables to indicate what |
| 35 | +*range of values* are valid for them. In ye old crusty languages like Java, |
| 36 | +types are *strict*. You can't just write: |
| 37 | + |
| 38 | +```java |
| 39 | +myvariable = 42; |
| 40 | +``` |
| 41 | + |
| 42 | +You gotta tell the dumb compiler what *type* of variable `myvariable` is, |
| 43 | +something like so: |
| 44 | + |
| 45 | +```java |
| 46 | +int myvariable = 42; |
| 47 | +``` |
| 48 | + |
| 49 | +There was a time long ago when that seemed like the obvious and completely |
| 50 | +necessary thing to do when programming. Like, computers are dumb, so of course |
| 51 | +you have to tell it that this `myvariable` thing is a number, so that if later |
| 52 | +some idiot tries to do: |
| 53 | + |
| 54 | +```java |
| 55 | +myvariable = "hahaha, this isn't a number at all!"; |
| 56 | +``` |
| 57 | + |
| 58 | +then the *intelligent* compiler will catch that, and tell the idiot who tries to |
| 59 | +do that "hey, you're doing it wrong". This is nice too, because it eliminates a |
| 60 | +whole class of possible bugs from your programs, those being *type errors*. |
| 61 | + |
| 62 | +People in ye old crusty languages** will often put on their snooty "I'm better |
| 63 | +than you" hat and tell you this is why *strongly typed languages are inherently |
| 64 | +superior*. There was a time I bought that argument, and maybe for some |
| 65 | +dimension of "inherently superior" I still do, but mostly I see typing like this |
| 66 | +as just one of many things that make some languages more or less useful in some |
| 67 | +contexts. |
| 68 | + |
| 69 | +Ok, about now you're probably like "shut up Adam and get to the point". |
| 70 | + |
| 71 | +Most of my day nowadays is spent in Python. I *love* Python. It's without a |
| 72 | +doubt my favourite language for any one of a million reasons (maybe an |
| 73 | +exaggeration, but probably not by much). |
| 74 | + |
| 75 | +As you may know, Python is... *not strongly typed*. I had a colleague (who's |
| 76 | +admittedly not a Pythonista) recently say to me that they felt that Python felt |
| 77 | +like the "wild west" when it came to types. And honestly, that's a fair |
| 78 | +assessment. Historically, or rather famously, Python back in the day was famous |
| 79 | +for having a philosophy of "we're all consenting adults", ie if you mess up |
| 80 | +types in your program, that's on you. Not in a mean or off-putting way, but rather in a "lets favour programmer flexibility" way. There was no real notion of variable |
| 81 | +declaration like you'd see in traditional strongly typed languages like (let's |
| 82 | +keep picking on) Java. So in Python you not only can, but *have* to do things |
| 83 | +like: |
| 84 | + |
| 85 | +```python |
| 86 | +myvariable = 42 |
| 87 | +``` |
| 88 | + |
| 89 | +Look Ma! No types! And what's really wild you can do absolute madness like |
| 90 | +this: |
| 91 | + |
| 92 | +```python |
| 93 | +myvariable = 42 |
| 94 | +if some_function(): |
| 95 | + myvariable = "now it's a string!" |
| 96 | +``` |
| 97 | + |
| 98 | +In here `myvariable` isn't an obvious type. A Java coder would probably look at |
| 99 | +this and go "but how can that possibly work, there's no way the compiler can |
| 100 | +know what type of variable `myvariable` is?" And it can't. And that's fine. |
| 101 | +This *dynamism* is (arguably) Python's hallmark, and a huge secret to its appeal |
| 102 | +and success. |
| 103 | + |
| 104 | +These are all trivial dumb examples, but try writing code to deserialize an |
| 105 | +arbitrary JSON blob received in a REST API endpoint in Python, and then try it |
| 106 | +again in ye old strictly typed language. In Python you just start slingin' |
| 107 | +dicts like a wild madman and *it all just works*. In ye made up strawman |
| 108 | +strictly typed language you're probably going to have to write boilerplate |
| 109 | +classes that define the *structure* of those JSON blobs and what fields they |
| 110 | +contain, and on and on until you have a rigid *schema* that the compiler can use |
| 111 | +to make sense of things. I've been there, and it's about the least fun kind of |
| 112 | +code you can write (maybe though in the world of Copilot, ChatGPT, and `<insert |
| 113 | +your favourite AI code generation tool here>`) it's not so onerous. I dunno as |
| 114 | +again, I live in fun happy Python land. |
| 115 | + |
| 116 | +Or rather, fun happy Python land until circa 2014-ish when |
| 117 | +[PEP-484](https://peps.python.org/pep-0484/) came along and introduced type |
| 118 | +*hints* to the language. |
| 119 | + |
| 120 | +Before I go further I do want to be clear: I think Type Hinting in Python |
| 121 | +absolutely has its place in the language. Any snide commentary that follows |
| 122 | +doesn't mean I think we should revert back to the "golden years" when types and |
| 123 | +static type checkers like `mypy` weren't a thing, as I don't feel that way at all. |
| 124 | + |
| 125 | +But, let's look at that snippet again, this time with a type hint added: |
| 126 | + |
| 127 | +```python |
| 128 | +myvariable: int = 42 |
| 129 | +``` |
| 130 | + |
| 131 | +Woah woah woah, what is that `int` keyword there? And a colon? SYNTAX? WHAT IS |
| 132 | +THIS, JAVA? 😠 |
| 133 | + |
| 134 | +FIXME: some segway here |
| 135 | + |
| 136 | +But, I guarantee you, if you work on a large Python codebase that's worked on by |
| 137 | +multiple developers over time you will reach a point where having some type |
| 138 | +annotations like this will be *invaluable*. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +** - ok, before I get hate mail saying there are modern languages that are |
| 143 | +strict too, of course there is, and I realize that, but give me some poetic |
| 144 | +liberty here), |
0 commit comments