Autoscaling is a simple concept. Automatically add resources when needed, and automatically shut them down to avoid paying for excess capacity. How hard could that be? If you've set up Autoscaling yourself, you know it's not that easy, especially using the native Autoscalers on platforms like AWS and Heroku. That's why you need JudoScale.
JudoScale is Autoscaling as a service, and they make Autoscaling simple and easy, as it should be. You can use JudoScale on AWS, Heroku, render, fly.io, and more. It's free for low traffic apps, and unlimited plans start at $25 per month. Autoscale on easy mode at judoscale.com. Hello, and welcome to another episode of The Bike Shed, a weekly podcast from your friends at ThoughtBot about developing great software.
I'm Sally Hall. And I'm Joel Kinville, and together we're here to share a bit of what we've learned along the way. So Joel, what's new in your world? So it's really exciting this week is about your early Ralpha Palooza, which is our sort of, in the year hackathon. And the way we do it is that in the lead up to it, people will pitch project ideas, something that they think they can complete within two days, and you need to have at least a couple other people to join on your idea for it to be a go, because this is meant to be something that's collaborative.
And then once you have that, and attract enough people to join your idea, then you have two days to go do a thing. And then at the end of the second day, typically you'll present it to the rest of the company. It's a pretty exciting way to map at the year. And I think the types of projects can be really broad. This is not necessarily just build an app.
It could be doing a particular type of write up, doing features on an existing project, some internal software. I believe we have a team that's working on conference talk ideas for CFPs next year. So it's room for a lot of different types of creativity. I've even participated in some years in things where we did like a mostly designed project and came up with a visual representation of the work we've done that year, or when you're even collaborated on some volunteer work, which was pretty exciting. Yeah, so people can take it in a lot of different directions.
The idea is that you have to do it with other people, so it has to be collaborative. And there has to be some kind of artifact at the end that you can share. So you don't want to just be like, I don't know, I did some things. There has to be something tangible that you can share with others. So what are you doing this year?
I'm on a group that's looking to do some more, I'm going to say rigorous experiments on capabilities of AI in our development workflow. Myself in particular, I'm looking at AI as a code analysis tool. There are a lot of things that I'll do by hand that can be really tedious. So if I'm doing some debugging or if I'm just trying to get familiar with a piece of code that's complex, oftentimes I will sort of manually trace it and do things like build out function call graph diagram, either upstream or downstream of an arbitrary point, just to understand, like, okay, so here's sort of the entry point where I get into this and like what all happens downstream from here.
And you cut it off at some arbitrary depth, right, three or five or something like that. Instead of like going through and like figuring all that by hand, I would love it if my ID just had a button to do that, but given that I don't have that magic button in my ID, can I just throw a like prompt snippet at an LMM and have it do that for me? Hmm. Could the LMM be your magic button? Right, right.
Are you testing different, what's the word? Models, I think is what you're asking? Yeah, different models, yeah. I'm trying to keep the scope of the experiment relatively small. So this is where it's interesting, right?
Cause I'm in the experiment design phase right now, where I sort of decide the parameters of this experiment and then decide what other things I'm going to vary, what are the things that I'm going to keep constant. So yeah, different models can be really interesting. I'm going to try not to do that. I think the question I want to answer is, is it possible, yes or no, rather than like exactly dialing in how to get the best results? Okay.
I'm really interested to see what you end up with. Yeah, yeah. I'm also restricting myself for this particular experiment to function call graphs, but I think there's a lot of other similar types of visual, almost like lenses you could have on a code base that could be really helpful. So I can imagine a situation where you say, hey, there's a complex workflow that I'm dealing with, a lot of callbacks or things like that, generate a state machine diagram that represents this. And that could be really helpful to sort of get at a glance, sort of road map of the code so I can then dive deeper myself.
But for the purpose of this experiment, I'm again, cutting off, pruning a lot of these branches because otherwise I have to test too many things. Nice manageable scope for two days. Yeah. Anyway, if I get good results pretty early on, then I can do some follow up experiments and say, okay, well, I get pretty good results with, you know, cloud code, you know, up to a depth of five. And now I can say, okay, do I want to try a different type of graph and keep going with cloud code, or maybe I want to like play with some models or play with some prompting to see if I can get better results and a deeper depth.
Are you going to generate things yourself too to have sort of like a human control? Yes. So the idea is that the control is that I will do what I would do currently, which is manually trace out that code. And that's sort of a hundred percent accuracy, right? So the LLM will be graded against that, how close it gets to my diagram.
Okay. And it's not never going to surpass it, but the value there is that, you know, my diagram takes a lot of my time and effort to do it, and it's kind of tedious. Yeah. So if the LLM can get pretty close, I'm not quite sure what is the level of accuracy that I really want here. Yeah.
You know what I'm talking like 90%, but it's instant or, you know, near instant, maybe that's good enough. Yeah. I'm, yeah, I'm pretty excited to hear these results. So you're also doing Raffle Glooza? Indeed.
What is your project? Yeah. So I've joined with a couple of other thought letters to work on. So one of our teammates, FridaCossis, has started this non-profit called the Cosses-O-Loh Foundation. I am going to do a bad job of describing it.
So I'm going to read it from their website. Coss-O-Loh will be a safe haven, not only for not only animals, but the BIPOC community. We're bringing folks together through their love of animals, nature, culture, and heritage. Our rescue will not only be about saving animals, but reconnecting with our roots. So that's a really cool mission.
And she reached out and asked anybody who wanted to put together an MVP website for the non-profit for this hackathon. So we're going to be doing that. And I'm excited because collaborating with cool people and supporting a good mission. And there will certainly be at least a few pictures of cute animals involved, which is always a bonus. So you mentioned reading from a website.
Is that sort of the mocks that you're reading from? Or is this a website that's already up? There is a website that's already up. I think it's probably Squared Space or something, you know, one of the sort of basic, build your own quick and dirty things. And so I'm excited to sort of jump in actually right after we've finished trying about this.
We're going to kick off our project and sort of see what the goals for the new website for this foundation are and how much we can get done. I'm curious if you had to build a website for yourself for something, would you build it from scratch? Or would you use something like Squared Space to just sort of get it out there? That's a good question. I think if we're like a marketing website where I just needed like, you know, a landing page with some basic information and ways to contact, I would probably use something like Squared Space.
I don't find the most joy in front-end code. And I'm not great at design. I don't know, I could probably get there eventually. But yeah, I feel like I wouldn't want to spend a lot of time reproducing things that somebody else has already done well and is available for me. But I think that if I was doing a website that had any sort of like application like functionality, I would be excited to build it from scratch.
I do remember when I first finished a coding boot camp like a decade ago and we all made our portfolios to look for job afterwards. And we're strongly discouraged from using any sort of website builder. And what I ended up with was not great. It did its job. It wasn't beautiful.
I think if I had to do that again, I would probably use a tool to do the parts that I either don't enjoy or are not very good at and then spend the rest of time focusing on what I do enjoy or what I am good at or what I want to learn more about. Yeah. Yeah. Like if I was excited about digging into something that I didn't understand about CSS, I would absolutely be building that from scratch because it's always a good learning opportunity. But if you just felt like you wanted to build a portfolio site right now or...
Yeah. Yeah. I don't know. Maybe you wanted to showcase some of your crafting build a website for that. You'd probably just go for an existing tool.
Yeah, absolutely. And I've done that with... I have some family members who have their own little projects or small businesses that I've helped out with their websites and I'm always going to recommend starting with the tool largely because then they can maintain it themselves. I mean, honestly, like at a different scale, that's the same reason why we often recommend Heroku for startups that we work with. Oh, yeah, absolutely.
You know, if you can outsource something that's not the core of the value that you're offering, then yeah, go for it and then focus on the things that you are offering that are valuable. Have you ever tried to debug an issue in production while switching between your error tracker, log viewer and APM tool? Maybe you know how painful it is when you can see the error but can't find the relevant logs or when you see slow performance, but it can't connect to the root cause. Scout monitoring is all in one platform, built to make your life easier with log management, performance tracking and error monitoring coming soon. All of your data will be together at last.
You'll go from bug to fix in one place. The result, less context switching, faster debugging and monitoring that actually helps you ship code instead of distracting from it. No DevOps team, no problem. Scout works with your existing stack and gives you insights in five minutes, not hours, from bug to fix in one place at ScoutAPM.com. So I'm really interested in the topic for this episode because you recently recorded an episode with Ajhi talking about active model and how that sort of shows up everywhere.
For those of our listeners who weren't there for that conversation, we'll link it in the show notes. But do you want to maybe like summarize in a couple of sentences the gist of that conversation? The gist of it was, well, what is called which is active model everywhere, which was the idea that like many classes that we work with that are not active model could really benefit from becoming an active model class, which definitely we put more eloquently in the other episode. But the idea is that there's so much built into active model that you often end up trying to re-implement yourself that more often than not, it's gonna be more useful to just go ahead and pull that in and then get the use of all the initializers and validations and all the other things that you sort of take for granted until they're not there.
Yeah, yeah. One of those features that I wanted to dig into today is active models custom attributes. So there's some built in attribute types and active model for things like strings and integers and dates and things like that. But you can have them customized for pretty much anything that you want. And because active record includes active model, you can have this for your active record objects as well.
And so part of what these custom attributes will do is they'll define ways that things get sort of serialized and deserialized. So you can have an active record object that has a column with some sort of primitive value, a string, an integer or something like that, but then gets turned into a richer object when you access the attribute. So like you store it as a string, but then when you access it, it is maybe like a formatted phone number. Is that what you're talking about? Yeah, something like that.
Or even not just like a formatted phone number. It could just be like a nicer version of a string, but it could be a rich object. OK. For example, I think it's the PhoneLib library that's the one that a lot of people use for handling phone numbers. You just give it a string and phone numbers people form out them all sorts of different ways.
But it knows all the different ways to parse them. And so you just get a nice object that has all different pieces inside of it and can handle international numbers and all the fun edge cases you forget to think about. Yeah. So it's stored in the database as a string. But then using this tool, you've got a rich object that you can do more with.
Right. Well, it's stored in the database as a string. And also, you can pass a string when you're doing object instantiation or object assignment. So you can say user.new phone number passing a string the way you would like form data using mass assignment in Rails. You could also say, you have a user object and say user.phone number equals, give it a string.
But then when you read the attribute from the user, you say user.phone number, you don't just get back that random string you put in. What you get back is a phone number object. Right. And then with that phone number object, you can choose to format it or normalize it or whatever it is that you need to do your phone number. Right.
And as it's constructing that, you can also, or sort of de-structing that. So let's say saving it to the database, you could have the custom attribute sort of know how to serialize itself. You could have it just take whatever the raw value is that got passed in, whatever the user typed, and that's what you store in the database. Or you could have this custom attribute always normalize things to, I think, is it E164, whatever the phone number format RFC is, because there is a standard format for phone numbers, including international. Yeah.
So that's a great example. The phone lip of a gen that you can pull in and use to do that. Do you also write your own custom attributes? Yes, the phone lip does not provide a custom attribute for rails. Oh, OK.
It just provides an object that can parse phone numbers, handles all the RFC stuff, all the edge cases, and then is also able to emit strings in a bunch of different formats, or can give you the parts. Or it's a little bit like having the time object in Ruby. Where you're like, oh, we can take a string, we can take numbers, it's also it's a way to construct time. There's all sorts of ways of then sort of getting some other value out of it. And it handles all the math and all that for you.
So you could use the phone lip to create the custom attribute. Yes. That takes advantage of that object that the Gem or Library has created for you, and uses that to manage the customized attribute, I guess. Right. Right.
I think an example of one where it ships with a gem is the Money Rails gem. Yes, I have used that one. So and that's one where what you probably want as your serialization format in the database is the number of cents, an integer number of cents. Or whatever your currency is, but probably cents. But then when you're interacting with it, like that's knowing you've got to do the math, all this stuff.
And so by having this sort of richer object, you can have sort of more domain operations you can do on it. And you don't have to worry about the fact that it's internally stored as cents. Yeah, I've been using those without realizing that that's what I was doing. How have you been using them lately? Have you been using custom attributes lately?
So I'm not a project where we use the Money Rails for the first custom attribute. We're also looking at the phone custom attribute. We've been just doing phone lip manually for some validations. Yeah. And now we're getting to the point where we're realizing, oh, you know what, this might be actually nice as a custom attribute.
So when it's a custom attribute, you've built it into the attribute in the model. And so you're not having to remember to use that library every time you interact with that. Correct attribute because it's already doing it. What you do need to do is on each model, mark that attribute as that type. So you have to say phone number is active model, phone number, whatever the attribute type is.
But you don't need to then add a normalized call or any of this other stuff. So it will just sort of work out of the box, which is nice. Depending on how you're using it, it also allows you to interact with the active record query system using the rich objects. So for example, you could pass a phone lip object into a where clause. With that then, I'm now imagining all kinds of exciting.
If you can get excited about what you do, exciting ways to query a database for phone numbers with this. This would then mean you don't have to worry about, is the phone number querying the same in the same format as the way it's sort of the database. Correct. Because that will do, that will get converted automatically. That is beautiful.
Now, the important thing if you're doing that, is you do want to have some form of normalization that happens in your custom attribute to the database. Because it is possible, you could write the attribute such that it just forwards the raw string to the database, and that's what gets saved. And then if it's always just forwarding the raw string, it's possible that two strings might not match. It might not match the same format or the same format. So if I'm doing a where clause, but when I initially created the user, I pass, I use parentheses and dashes and things like that.
And then when I'm querying it, I just give numbers with none of that extra stuff. If the database doesn't normalize those numbers, they will not match. OK. Yeah. And sometimes you want to keep the raw user stuff so that you can then show back, let's say, on an edit page, exactly what they typed in.
Not a normalized version, but exactly what they typed in. The downside of that is that then it becomes really annoying to query. Yeah. And difficult to index, all these other things. So it's a little bit of like, what are your usage patterns going to be?
Yeah. I could see, for a phone number, I'm probably not going to care much about whether or not it's displayed back to me exactly as I typed it in. But for something more personalized, or I guess a name, for example, we could be choosing to, in some sort of object, to normalize those in some way. And that's, names are interesting, right? Because there's a lot of stuff that's personal when you say, the way I put it in is the way I want it.
But I think there's some things we sort of accept it are fine normalizations. So maybe we shouldn't. But I'm thinking of things like down casing names for storage. Capitalization can be an important part of a name. Yeah.
But then that makes, again, things like querying and indexing much harder if all of a sudden things are case sensitive. So how could we use then a custom attribute? Let's stick with the case of names for a second. And how would we use create a custom attribute that could help us with that with wanting to, or is this not where we're headed? So I think if you're just doing normalization, you might be better off with just, Rails now has a normalized macro that you can use.
I think where it really shines is where you might have some normalization. But what you really want is some sort of richer object in your actual Ruby program to interact with things. This is especially true if you might have sort of multiple input formats. So data might come in a few different shapes. You might also want to normalize it.
But also the normalized version is not the way you would also want to see it. So maybe there's a formatting step at the output layer. You might have multiple different output formats, in addition to multiple input formats. And maybe there are multiple sort of custom domain operations you might want to do on it as part of your Rails program. So maybe it's, can be decomposed in parts?
Or maybe it can be, those are questions you want to ask about, is it this? Is this a number from such and such a country or things like that? Which sort of you don't want to be writing ad hoc logics or a spread throughout your code base, or please not in your views. So sometimes it's nice to just be able to say the phone number attribute returns a phone number object. And so then I can just say, if Sally.phoneumber.us.
I'm imagining ways that I might want to use something like this. And my brain is going to like a mailing address, which can be normalized. But they can come in so many different shapes, even within one country, like even within the US. Mailing addresses can look very different from place to place. And I'm wondering, would this be a place where we would use a custom attribute to maybe take a mailing address string and be able to do different things with it?
Like pull out the state? That is a maybe thing that you do. I think most commonly for something like that, what I've seen is having a separate, like a full active record model server table that handles address. Yeah, yeah, that makes sense. So you still have a rich object that represents an address, but it is its own table.
It's an active record model. I'm trying to, I don't know. I love thinking examples. Can you give me one? Of something that might be in multiple parts?
Something more than a phone number, I guess, where you might really benefit from using a custom attribute like this. I think that anytime you have something where you're introducing a richer object and you're having to serialize it to something at the database, but you might still want to query it. There's some built-in things like time, which is typically stored as a Unix timestamp. But as a programmer, you never want to be doing raw operations on a Unix timestamp. So you have a nice time object that will do all that for you.
Yeah. Act to support durations or another thing. Like I know I have a span of time, but is it internally stored as a number of milliseconds or seconds or something like that? And then I want to be able to add them, but I have a duration that's maybe in milliseconds and one that's in seconds. And now I've got to do all the math.
No, I want an object that just handles all that and say, hey, duration one plus duration two. If I need to store those in the database, I might want to have a custom attribute type that can handle durations correctly. OK. I feel like I'm excited about this idea, and I'm having trouble finding ways to use it in my head. I feel like I'm a little bit the same way, honestly.
It's a really cool concept. And in practice, it is not a tool I reach for very often. Yeah. It kind of feels like Rails has already handled a lot of the situations where I would use this. Right.
And a lot of cases where you can have an object or something that has multiple fields on it, it might very well qualify for its own separate table, like addresses do. Yeah. I think really, again, where this really benefits are these sort of more complex values where you might have some logic that wants to sort of lob onto it. So things like phone numbers or money amounts, things like that that you want to serialize the database, and they're maybe represented by just a number. But the raw underlying number is not really a thing you want to interact with directly.
Yeah. I imagine a lot of units of measure, depending on your app, if you're not just taking the raw number out of the database and rendering it in a view, could qualify for something like that. I could imagine a world where maybe you're doing some more kind of physics-y things and you're storing physical properties in a database. Assuming you're not doing the math in the database, but you're doing it in your application, then maybe what you want to do is to have velocities and forces and things like that be their own object types that can interact with each other. Or something like latitudes and longitudes.
Yeah. You might also start with something where you have a Ruby library that's really good at that and provide some rich objects, and then realize, oh, I want this in a Rails app, and now I want to, or I need some way to persist these values. But again, I don't want to deal with the raw underlying sort of internals of them. So I'm thinking of things like the Astronomy library that Phil Thott are, Jaime, and Kei does. So he does a lot of math and stuff for calculating the positions of celestial bodies.
It's a really cool library. And let's say I wanted to, it's just a gem. So you run on the command line to do stuff, but let's say I wanted a web UI for it. And maybe I want to save my favorite celestial objects and do calculations on them or something. But you could imagine a world where something like that sort of needs to be rehydrated from the database.
Yes. And then I'm going to add that off to the custom attribute to handle it and not me having to think in detail about the raw representation of these objects. Every time I query your right one, it would be super helpful. I feel like that idea of the raw representation is the thing that I've had to sort of learn the hard way. The idea that the way that I want to think about something as a human is not necessarily the best way for a computer to work with something.
And that a lot of ways that we've sort of learned to interact with things in class example is time. So you're talking about time of day, you often think of it as, okay, it's a number of, it's an hour and a minute amount, maybe seconds, depending on your precision level. If you're building a Ruby class to handle a time of day value, you probably don't want separate hour and minute values because the moment you start doing math to them, now you've got to handle all these weird overflows and it just gets really complicated. What you want is whatever your smallest unit of measure is, so let's say seconds is going to be a single counter and that's all of your time of day. So your time of day is a number of seconds since midnight and it maxes out at whatever 12 times 60 times 60 is, or sorry, 24 times 60 times 60 with perhaps some extra edge cases for those days that actually have 25 hours.
Or it's a 23, right? Days always are 24 hours long, except whether or not. So the internals and then all the math works on a integer counter seconds. As a human, I can't look at a number that says 8,000 seconds and be like, oh yeah, that's mid afternoon. Like I don't have that intuition, that's not human readable.
But that's how a Ruby object would best interact with this. And so that's the thing that I want to hide. And if I were packaging this as a library and sharing it around, users of this gem should never have to care about what the internal representation is. Do I save it as seconds or milliseconds or whatever? I mean, they might care that what is the precision that I can give them?
But the internal, the fact that I'm doing it as a single counter should be entirely hidden. And I think that's what the custom attribute thing in Rails tries to do. It's the same idea of say, hey, we have maybe some sort of awkward value like an integer counter of seconds or a number of cents or things like that. You don't want to deal with directly. You want to have a rich object around it.
You want to be able to ask it more domain questions. So not just what is your raw value, but here's some operations I'd like to do with you. Here's a question I have about you and sort of package that nicely and have it read and write transparently from the database and not have to care. And to be able to have it behave the same way, if you want to use the same type of custom attribute across different models or across different tables and not having to be implemented every time. Yes, that consistency is really nice.
I'm thinking of a project I've been working on where we're storing a lot of percentages. Oh, that's an interesting one. And there's not a lot of consistency. If I want to represent 25.6% of something, do I store that as 25.6 or do I store it as 0.256? And knowing which one it's stored as and how to display it correctly and things like that gets messy.
And if we had a custom attribute where we could just easily take our human understanding of it, give it to this attribute and the attribute of deals with the querying and the storing and all of that. Right. That could be helpful. I can know just imagine like a world where you are looking at a column in the database and you see 0.26%. Does that mean 26% or does that mean a quarter of a percent?
Exactly. Both are valid interpretations, right? Like you can't just by looking at the data, know which format the author intended it to be used as. Especially if it's like a domain that you're not super familiar with. Like, you know, if we're talking about interest rates, I don't know which one makes more sense.
If we're talking about fiber content of yarn, I do. But you might not. It might be the complete opposite for you, where it's like, I don't know what percentage would make sense for fiber content of yarn. I love that as a, I thought it's almost like a DX, a developer experience layer where you say, hey, we're going to not accidentally have any of these mistakes because the custom object that backs the attribute is a little bit more foolproof. Yeah.
You say, hey, I've got a percentage here. And maybe I want to display it. And I've got a helper function that just takes that object and is, regardless of what it's backed by or whatever, it will display it correctly. Yeah. Or I want to do some math against it.
Maybe there are some domains where you implemented a time dysfunction or something on it so you can apply it to an integer. And then hopefully avoid having a code base littered with divided by 100 and times. Yes. And it's the kind of thing that I think is a small change in the way that you build your code base. You wouldn't necessarily think of doing, but it removes so many little paper cuts.
And then when you see it, it's like, oh, all of a sudden, this little bit of code that was in 10 different places lives in one place. And it all just feels so natural to use. Yeah. That's the best moment when you can take something that you were having to think about or forgetting to think about and take it off the table entirely and let you focus on the more interesting or complicated or important problems. I think that's the power of object-oriented programming.
It's the power of abstraction done right. The cohesive power. When your objects are representative thing, that's an actual concept in your app. That used to be like implicitly a concept in your app, it sort of scattered all over. Or just maybe a bunch of primitive values that you already had named, but you had the idea of a percentage.
But now you're having a bunch of helpers scattered throughout the app that have to act on these and maybe some normalization and things like that. And now once you name it as an object, it all sort of becomes one place. You have a list of like, these are the domain operations you can do on a percentage. We have even maybe some unit tests that cover it. That just becomes really nice, as opposed to like, very in-imixed sort of data.
Yeah. I find those moments when you can identify something like that and unify it in that way to be so satisfying. It really is. That's one of my favorite performance. I feel like we could do a whole separate episode on value objects and domain objects in general, sort of the code smells around them and how to identify when to use them.
Because I think it's one of those little things that you wouldn't always think to do. But once you get an habit of doing them, really improves the code you work on. Yeah. And I feel like that's the kind of thing that can make joining a new codebase welcoming. Yes.
Which isn't really a word I've applied to a codebase before. But I think we're thinking about sort of not only what is maintainable, but what is welcoming for a new member of the team, what will help someone feel comfortable here quickly and encapsulating things like that in logical ways that remove some of that mess from the developer's daily life, I think, is a big one. Yeah, just sort of maybe lowering the cognitive load they have to pick up on day one, just to interact with the code. Yeah, absolutely. Oh, now I want to talk for hours about what can make welcoming codebase.
Look forward to that in the future. New episode idea. Yeah. Yeah. Sometimes though, I think it's easy to have that as a goal and then it goes horribly wrong.
Right? We've all seen terrible abstractions that just make your life harder than easier. Oh, yes. Oh, yes. Oh, I once worked in an application where they had this foundational agenda they used that basically abstracted out all of the basic controller actions.
Oh, yep. I've seen multiple projects want to do that. Yeah, so confusing. You don't need to magic the rails harder. Just, you know, apply that enthusiasm to problems that are not already solved.
But we're now getting on so many other trails. I think this is probably the right place to wrap up this conversation. I agree. I agree. We will save those for another time.