text
stringlengths
0
2.35k
**Ian Lopshire:** Yeah, happy to be here again.
**Kris Brandow:** Fantastic. So I'm sure that we're going to get straight into the meta, since both Johnny and I are in this, or on this podcast today. But I guess we can start with something kind of high-level. So I guess I can read the actual suggestion, and then I have something I want to prompt us to start with.
Basically, Thomas wrote in and asked, "There are a lot of utility functions that I end up writing myself in Go; these are the kinds of functions that get asked about on Stack Overflow, and met with the response of, "This is trivial to implement, do it yourself." For example, contains slice, string of slice, value slice...
\[04:19\] So the question I want to start off - basically, have you guys run into this before? Have you had the struggle of "I have this code that's somewhat generic, somewhat reused; where should I put it? I guess, Johnny, do you want to start off?
**Johnny Boursiquot:** Yeah, I can start off. I think we've had various conversations that touch on this topic on the podcast before. Oftentimes, I think those of us that are sort of experienced with the language sort of typically settle on this rule of three, kind of thing.
For me personally, I don't consider something reusable, unless I've actually reused it; meaning that I don't try to sort of preempt the process of prematurely trying to make something reusable, even if I have a hunch that it is going to be reusable.
So to piggyback off of the suggester's explanation of utility functions and things like that, like contains, or whatever it is - these things on their face seems like, "Yes, that should be a reusable thing." But for me, until I've seen something at least three times, I don't have enough data to understand all the edge ...
**Kris Brandow:** Ian, what are your thoughts on that?
**Ian Lopshire:** Yes, I mean, I definitely agree with that kind of rule three idea. I would much rather copy and paste code multiple times, than try to make an abstract version that is bad. It's so much easier to add an abstraction later than it is to take one away. So I’d much rather just copy and paste.
**Kris Brandow:** Okay. Yeah, I definitely agree with that, too. This is something we're gonna get into a little bit later, but I think sometimes people try and overdry their code, where it's like, "Oh, no, I've written this once. I have to figure out how to never write these exact same lines ever again, and put them s...
So I guess like on that as well, let's say that you do figure out that you have something that you've used three, four or five times, the code is very similar... What other heuristics do you use to figure out like, "Okay, should I just be copy-pasting this around?" Kind of as the Go Proverb says, "A little copy is bett...
**Ian Lopshire:** I have a fun heuristic for this... If I can think of a good package name for it, that makes sense, I think that means it's reusable enough that I should probably break it into a package. But if it's going to be like this package that's four words long, I'm just not going to. I'm going to just put it w...
**Johnny Boursiquot:** Mine is more of a sort of, why do I think I need to make this reusable? So there's a reuse - I think you hinted at this, Kris... There's reuse basically within the same code base. There's also reuse within multiple projects. So I could extract away a package and make that reusable. But interestin...
\[08:28\] So I'd rather keep reusable chunks of stuff in my own project, because I know the moment I make it reusable by other teams, the responsibility scope for that code has just grown for me. And honestly, perhaps this is an unpopular opinion, or perhaps I'm just getting to that point in my career where I just am n...
**Ian Lopshire:** I'll mirror that. I'm a fan of the internal folder; like, I'm not even allowing you to use this.
**Kris Brandow:** I think in this case, using the internal folder to be like, "No, no. I know you might want to use this thing, but please don't. Please don't touch this thing." But I've also seen the other extreme, where it's just like, "Okay, we're just going to assume that by default, and everything's going to go un...
**Ian Lopshire:** I don't have a habit of doing that. What I do have a habit of is just not exporting anything I don't have to, so they're not going to be able to get to it anyways. But I'm known to put kind of very general packages and internal just because they look appetizing to use externally.
**Johnny Boursiquot:** Perhaps my other dirty little secret - I don't use Internal a ton. Even then, I've heard some folks basically approach projects with basically saying, "You start with Internal, and then if you need to, take things out." I don't take that approach. I tend to sort of reason about the stuff I'm work...
I will literally have everything into a single -- sort of at the root level of the project before I even start to create folders, creating packages and things. Because I don't know what I don't know yet. So I have everything in the same top-level package, and if I know I'm creating executables, one thing I absolutely d...
Back in the very early days, I started following this pattern of putting things in a pkg folder, and all that jazz... Honestly, I think that's an anti-pattern at this point. Like, you don't need a pkg folder. If you happen to work on a project you've inherited that has one - fine, so be it. But you don't need these thi...
So I really rely on sort of a complete understanding of the problem I'm solving. I'll even go as far as to actually have in my cmd slash whatever name folder I need, I'll even have my main.go in there, I'll have other Go files in there. Literally, I'll have everything that I need to make this binary -- especially at th...
\[12:23\] It's all incremental. Even if I've built this kind of project before, I know, I kind of have a sense of where the seams are, I know what I'm going to do. I have the discipline or I've developed the discipline to not jump the gun on that. I really need to start seeing the edges of the service, and sometimes I ...
Again, basically, you don't know what you don't know yet. And sometimes, you build these projects, and they might be proof of concepts, and they might be things that don't end up going into production... Why am I going to expend all this brain energy trying to make all these reusable things and abstractions and things,...
**Kris Brandow:** Right. So the way you follow things is like start with just one package, maybe it is even just like the main package, and then as you have reasons to start pulling things out, that's when you start pulling things out... Which I would also say is a very good way to avoid having people import your code;...
**Johnny Boursiquot:** \[laughs\] You can't import main. Right.
**Kris Brandow:** Like, "Stay off my lawn, you can't touch any of my stuff." So I wonder, does that change when you go from building an application that's going to run as like a microservice or something, when you shift from that to writing a library that is going to be consumed by other people? Do you still take the s...
**Break**: \[14:12\]
**Johnny Boursiquot:** I pull a page from Ian's book, and basically saying, "Hey, I don't export anything I don't want you to use." So even then I might not have an internal folder inside, even if it's basically a package that’s designed to be used as a library... Because the moment you make something a library - again...
So I will never expose anything prematurely. Everything will be unexported until I have a reason to export it. I used to have a bad habit of actually exporting interfaces out of my libraries... That says, "Oh, yeah, like you can use this public interface, this export interface, so you can know what to pass in, when you...
Right now, I rarely, if ever, export an interface from a library. Because you can actually, in your own code, you can create single-method interfaces, whatever you need. You can create local to your code interfaces for whatever you need. I don't need to give you exported interfaces for you to know how to use my library...
There's a way using GoDoc where you can actually show unexported documentation for functions, and stuff. So when I need to actually, on the command line, see what unexported functionality documentation is, I can actually do that. So things are documented as if I'm going to make them available for people to use. But I d...
**Kris Brandow:** Yeah. Ian, what do you think about that?
**Ian Lopshire:** I mean, it's hard to disagree with anything that was just said there. I think that pattern of just starting with one bit, like one package, one thing, even if you're just adding a feature - like, I like to start in one place, and do that proof of concept; and it should be that iterative approach, wher...
**Kris Brandow:** Yes, it sounds like a key thing to building maintainable software as well.
**Johnny Boursiquot:** Wouldn't you know...
**Kris Brandow:** I mean, I don't think I declared this as one of our maintenance series episodes; our mini, but now max series... I wonder if you can just follow like Apple’s naming instances, where’s it’s like Pro, and then we can have Max, and then we can have Ultra when the series gets longer and longer. So I guess...
But yeah, from what you're saying Johnny, and what you're saying, Ian, it sounds like these are also like good strategies to kind of build more maintainable software for us in the future as well. Because once you do export something, you can never really take that back, at the end of the day. It's kind of there forever...
But let's say that we have figured out that like, okay, here is some code that really is generic, at the end of the day; it really is just like, okay, I’m just like copying it and pasting it all over the place, and I'm changing... Like, the changes I make, I can just run a Copy and Replace, or a Find and Replace in my ...
\[20:21\] At what point, or I guess in what way do you then take that code and make it so that it's not so much copying and pasting? Or do you just say, "Well, this code is small enough, and I don't want to make it into a library I have to support" so I do just continue copying and pasting it forever. Or do you do some...
**Ian Lopshire:** I think a good example here is the aws.string, where we’re taking a string and returning a pointer to it. I have that sprinkled throughout the codebase. It might be a struct that I'm trying to do... And before generics, I think the place where those were just where you needed them. But I think that's ...
**Kris Brandow:** I'm kind of surprised there isn't a package that just does that, that someone maintains out there. Or maybe there is, I just don't know. I mean, I think it'd be hard to come up with a name for it, but...
**Ian Lopshire:** There's one just called Pointer, ptr.
**Kris Brandow:** Oh. Well, that's a good name. So following your rule, Ian, that makes sense.
**Ian Lopshire:** Exactly.
**Johnny Boursiquot:** You see, for me, I consider things like that to be extraneous. \[laughs\] Again, I'm going to be that old man pointing at the cloud. I'm like, "Okay, if I need a pointer to something, I will use the language proper to make a pointer to something." Yeah, it might be a little more verbose, it might...
So if I'm really going to be aggressively using a package to do things, in a specialized kind of work - yes, I will find a suitable third-party library out there. And of those, I will evaluate them to see which one is documented well enough, which one is maintained actively, which one is referenced by other things, lik...
To me, it's a matter of -- like, I've been around the block long enough to understand the cost of abstractions, to understand the cost of bringing in third parties to take away perhaps some labor you might have had to do on your own, to perhaps write things the long way, or the more verbose way. So everything is a trad...
I've seen very large projects that basically have some abstractions that no longer hold water today. Perhaps back then, when they were introduced, they made total sense. But today, you have little notes, little side notes on these things, "Well, I don't use that anymore," or "Deprecated. Use this thing instead." Every ...
**Kris Brandow:** \[24:23\] And part of me wonders if this is like a shift from the pendulum swinging... Like, I know when I first started my career, the kind of ethos for everything - I think partially because it was so difficult to get dependencies - it was like, "Build stuff yourself." And I think I joined the indus...
I kind of wonder if what you're saying, Johnny, is the kind of evolution that's happened from us swinging a little too far to the other side of "probably found elsewhere", where we have these problems where doing this type of evaluation on external dependencies to do it properly takes a lot of time and effort and energ...