Making Code Stick: Day 3

Had to get some work done, take my car in for maintenance, and go to a meetup and a meeting, so I only got one lesson in, but also partially because it took a while.

Lesson 6: Make it Modular

One of the reasons this took so long is just stupid little mistakes. Three that stand out:

1: I forgot to assign my imported modules (fs, path, and the one I made) to variables so I could use them. D’oh. This is a bad habit I need to break.

2: I assigned the path module to a variable named “path”… Duh. The problem was that when feeding the filepath to my module, I called it “path” in the incoming parameter names for my function. Yay. Naming collisions. I was overwriting my module with a string. So when I’d try to use the module’s extname method, I’d get an error that there was no such method.

3: I put my call to the callback outside the function that handled the asynchronous fs.readdir call, so the module’s method would run and invoke the callback BEFORE the asynchronous call to get and filter the data finished.

There weren’t a lot of handwritten notes except to remind me of my mistakes, which I have listed above. They made this particular lesson take about 40 minutes to debug and get right.

Here’s My Code

My module:

Main Script:

Now remember, I have been paid to code. My last three jobs have had “developer” or “programmer” in the title. So making rookie mistakes like some of these is embarrassing, but if I don’t cop to them, what good is this exercise? Knowing the rules of good code doesn’t mean you always follow them, especially when it’s a quickie exercise you’re trying to rush through. Writing code insanely fast usually makes debugging it insanely slow.

Leave a Reply

Your email address will not be published. Required fields are marked *

*