Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Debugging with Logging" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to use logs to debug the code, and answers questions about the Writer monad, a monad that, along with values, returns an error message.

Preview
Close

Transcript from the "Debugging with Logging" Lesson

[00:00:00]
>> See you have some login to string stuff in this file, can you show how to use that for debugging?
>> Yes, check it out. So, to string actually just [LAUGH] calling string around things, so if we do see a console.log(Right(2)) hopefully what happens here, where are we?

[00:00:26]
Wow, that's fine.
>> I think you can just click the console and-
>> That's a much better way to do it thank you, okay where's the?
>> In the bottom.
>> Lower left.
>> Lower left down.
>> All the way.
>> All the way, bottom left corner.
>> There it is, it's hidden from me, cool.

[00:00:46]
Let's tie these tests and bring this up, I have to coerce it into a string by calling a string around it, so there's that. And now you get a Right(2), I can call to string but if I put the string type around it then it does the same thing.

[00:01:04]
It's kind of a fun trick if you have a null, you can't call a null to string week put string around a null.
>> I'm more wondering inside you're chaining.
>> What's that, yes, exactly so that was the first one was easier. So here we are, and we're working with this and we're like, you know what?

[00:01:23]
I don't know what's going on here, I'm just gonna map log, and what's happening here is log, it was LogIt, sorry, but LogIt, if I can find it takes the x logs, it returns it back. So if I'm mapping over it, I can actually just LogIt and there we go, and I can sprinkle these through if I'd like, I can kinda say, well what do I have here and what do I have there, and it doesn't wanna stay up.

[00:01:56]
[LAUGH] That's fine, [SOUND] okay, but that's how you'll do that I mean that's the same thing as doing this. Okay, but you could also do this cuz it returns it back, right? So you can see what I'm dealing with, and it looks like I have a left of that, so cool.

[00:02:18]
So those are useful tools in the middle of your composition to just kind of inspect the inside or outside, cool. Anybody else.
>> Can we take off on the log method?. I've used the similar thing to ship out to like a write only database to keep a perimeter a durable log of functional operations.

[00:02:42]
>> Interesting view.
>> The question is more of a technical one, is there a point at which you're starting to fork your threads. Is there a point in which this becomes too cumbersome or you need to re-architect if you're trying to insert a bunch of logs?
>> Yeah.

[00:02:55]
>> When you need a similar kind of like shutoff and side effect functionality.
>> Right, so there's the writer monad which we're not gonna talk about today but provides that kind of functionality, but it's still almost exactly the same cost. So it doesn't really solve the problem, you could define your own composition, right?

[00:03:17]
So that would be a possibility make some kind of composition that keeps a log on the side, perhaps, maybe some state like monad that threads it through. But I think there's there's probably some ways to do it that is less just getting in there and inserting the stuff.

[00:03:37]
[LAUGH] But those are two that come to mind, I think I'd have to work through a couple cuz neither of them seemed great. [LAUGH] But we could probably get to the solution by trying starting there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now