Stop doing math in your config files

Welcome to No Compromises. A peek into the mind of two old web devs who have seen some things. This is Joel.

And this is Aaron. I might go on a little bit of a rant here, Joel. But I want to talk about something that bothers me, but I think that there's an actual good thing behind that we can pull out and make our projects better.

Okay.

And it is that... well, let me phrase it a different way. How many seconds is 5 minutes?

What? I didn't know there'd be a test here. 300 seconds in 5 minutes. I'm a math genius.

Awesome. So, let's just say you wanted to set a timeout for 300 seconds inside of a config in Laravel, or something like that. You want to set it 300. And it accepts its values in seconds, and you want it to be 5 minutes, and that's what the business has said.

Like, "We want a 5-minute timeout," or whatever. So, you need to put number of seconds in there. How would you do it? Would you put 300 seconds, or would you put-

Oh, okay.

... 5 times 60.

Oh. I would definitely do 300, but I see where you're going with this. I might change my answer if it was something more bizarre, like the number of seconds in a year.

Excellent. Cool. I started out really simple, just because it should seem pretty obvious when we're doing 5 times 60. It should be just 300. Just put 300 there.

But if you're talking about the number of seconds in a year, again, why would I need to calculate that if I've already done it once? So, let me get to the basis of my point here. I hate math inside of config values.

Oh.

Inside of config files, inside of config values, all that stuff. There is absolutely no reason to do that if that value never changes. Just because it's easier for you to understand when you write it doesn't mean that we necessarily have to have this logic happen every single time that we run it.

So, when I look at something like that, even the number of seconds in a year, no idea how many there is. But I know one way I can figure it out, I can use my calculator and figure it out. It'd be like, I don't know, 60 seconds times, I don't know, how many minutes, how many days, years, whatever.

Yeah.

Whatever, I would do that math. But then I got a value, and that is the amount of seconds. Why would I do that math again every single time I ran every single request?

Well, okay, I see where you're going. What if your config is cached? Do you not care then?

Well, still though, I had to run it one for the cache. It's literally a number, it doesn't change.

Yeah, okay.

So, why do math each time to get that number?

If I'm reading between the lines, in your mind, it's not really about performance. It's just like, why are you doing this extra step? Like, just put the end result in there, right?

Oh, it's a little bit about... Performance is kind of one of the smells of why this is weird, right? Okay, so you said what if it's cached, but what if it's not? What if you're in a place where there is no cache? I'm talking config files, you went to Laravel caching, whatever.

But let's just say it's calling a third-party service or something, we have to pass that to a service parameter or whatever.

Okay.

Like, why do that math there?

So, it's not exclusive to the config folder then. You're just saying in general, if there's a constant of some sort, not PHP constant, but if there's a constant value, don't get cute and put math there to make it more human-understandable.

Versus 86,400. Like just... You know what I'm saying? That's people's inclination for doing it, is like, "Oh, it'll make more sense. They'll know what this magic number means."

Right. So there's also this really, really cool feature built into almost every single language.

Here we go.

Super awesome. It's called a comment.

Yeah.

And so, if you have a value that is a same value that we don't use anywhere else, but it's been pre-calculated, put a comment and explain how you got that number. It's super simple, and we don't have to do the math anymore. Matter of fact, you don't even have to say it's the number of seconds, you could say that number of seconds in a year.

But you can also put the math that you used to calculate that right after that, so that someone wanting to verify your work, they can look at them and see, "Oh, yeah, it's 60 times 24 times 365, times whatever."

And they'd be like, "Oh, yeah, that does make sense. If we did that math, that is that value." But our code doesn't need to run it every single time.

Okay. And I don't feel as strongly as you, but I do tend to agree with you. And just like if anybody is shouting at the podcast player and like, "Well, what if it drifts?" Like, then your comments aren't accurate.

Well, you're doing code review, right? And the person's going to catch that. You know what I'm saying?

Right.

And if it drifts there's other ways of fixing that problem.

Well, and also, yeah... I mean, that's a whole different conversation, but to me, a comment is code in a way. Like, if it's a comment describing code, it's the same level of value as the code itself to me.

Yeah. I don't think this is a real big tangent, but the other thing too that I have found in config values like this is even knowing, like, what's the unit here? Because every once in a while it's seconds, but then it's milliseconds or something.

You know what I'm saying? So that's another perfect... you'd have to put that in a comment, right? Like, if there's no unit types built into PHP as a language, so you'd have to say //these are seconds, or whatever.

So it's kind of the same principle there. Like, you're explaining it with comments, do the same thing with a number.

So it's all little things. And I don't like the focus on premature optimization, so it's not about just the performance. But it's about just thinking it through, like, why would I need to do this thing?

So, when you start thinking about coding that way, you start thinking about other things you're doing. Like, why do I need to do this thing this way? Is there a better way to do it, you know? When I saw that math all the time, and honestly, I've done it once or twice.

I have too.

And after the second or third time I did it, I'm just like, "What am I doing? I know what this value is."

Right. Yeah, it's always good to be thoughtful about your code, right? And I know where people are coming from wanting to show their work, but I think I'm with you on this one. One last off topic question.

What are your thoughts? So the number's 86,400, what do you think about putting an underscore in there? Like, where you would write a comma as a human? You know, that's a thing you can do in PHP.

You can underscore as a separator to make it more readable. Would that make Aaron reject something? Or be like, "Joel, what are you doing?"

Sure. Sure, you can do that if you need to.

Give me that one thing.

If you really need to do that, I'll let you do that.

I'm not going to, but I was just curious.

Yeah, I'm not going to do that either. So, this last weekend we had this residence event at our apartment complex. So, it was for all the people who live here.

You can go there and get some free food, meet your neighbors, cool stuff like that. So, I go down there, and you could bring a guest, or I just went by myself. So, I get there, and there's maybe 10 tables and they're just full of people except there's 2 tables that aren't full.

So there's 8 full tables, and then there's these 2 that one has a couple, and I think one has three people at them. And so, I grab my food, and I go and sit down with the couple and start talking with them.

You know, just normal neighbor stuff. About 5 minutes into me sitting down, they're like, "Oh, we got to head out. We got to leave."

Oh boy.

So they leave, and I'm just at this table. So, now there's eight full tables of people having great conversations. Me by myself at a table where people just left me, and then there's one other table. So I'm just like, "Okay, I'm going to go to the other table."

I go over to the other table, sit down there, and chat with them a little bit better of a conversation. But they have a bunch of work that they have to get to later tonight, actually. They have second shift work and third shift work, so they're all leaving in about 10 minutes.

Oh boy.

So I sit at the second table, and that whole table group leaves. By that time, I just didn't know what to do, but my night came to an end. Did I do the right thing? Is there something wrong with me, Joel? Help me out. What's going on here?

You sure you're not leaving anything out of the story? Like, you came, you sat down, and started ranting about something? So, it's just bad timing. It's just bad timing.

Yeah. I mean, I was ranting, but just how everyone else rants about work and their business partner named Joel.

Oh. You know, when you started telling the story, I'm like, "You had a plus one, and I'm over here."

Yeah, there was free food.

Right.

Sorry. When doing a code review, sometimes there's opinionated things like this, maybe, but other times there's real problems.

If you'd a second set of eyes, expert opinion to review your code and find those things that are really worth fixing, head over to masteringlaravel.io/codereview.

No Compromises, LLC