When One Liners Make Sense

When One Liners Make Sense

Have you ever written a little one or two line snippet of code you were super proud because it was oh so short and sneaky and clever? But then, like a month (or 20 mins) later, you have to go back to it and you have no idea what that stinker’s actually doing? Yeah, me neither.

A close up of a long haired tabby cat staring directly at the viewer
Keep Calvin happy! Write clean code!

I have some thoughts on little clever bits of code. First of all, they’re cool. Yay for figuring out how to compress say 15 lines into two! Second of all, they can drive me nuts when having to go back to review old code. Especially if it contains regex (shakes fist at regex). I also think that there are sometimes places that are more suited for the clever bits than others. Let me explain . . .

I think that if you’re deliberately trying to condense your work down into fewer lines for the sole sake of fewer lines, you’re in danger of losing the overall clarity of you code. And in the long run, that’s going to make your code more difficult to maintain by you and others.

Now when do I think that a cute one-liner is appropriate? I like to use them when doing so doesn’t distract from the overall idea of the function or method I’m writing. It’s like a mini subroutine (but not) that I don’t have to do any extra work for. For example:

# x is a datetime field
sorted(my_list, key=lambda x:x[1], reverse=True)

I could have written a helper function that went through my list and sorted them by date then reversed it, but it turned out to be much more concise to use a lambda function within the built-in sorted() function. It also made way more sense to just use a built in function rather than writing my own.

Another example:

# _ can be used in python editor to reference whatever 
# was on the previous line. Fun, right?
>>> [1, 23, 1, 45, 5, 9]
[1, 23, 1, 45, 5, 9]
>>> [s for s in _ if s > 10]
[23, 45]

# The following is equivalent to above 
# but in more lines.
foo = [1, 23, 1, 45, 5, 9]
foo2 = []
>>> for each in foo:
...     if each > 10:
...         foo2.append(each)
...
>>> foo2
[23, 45]

But if that logic was more complex and my one liner started getting long and I had to start line wrapping it to fit? Nope, that’s going to be way more difficult to read and understand in the long run. (That also goes against the PEP 8 Style Guide. I like to stay within the 72 character line limit, thank you.) Anyone coming after you that has to do anything with that code (including you) is just going to want to whack you over the head with a boot. Just write out some loops, man.

Have fun finding clever ways of coding!

-Rachel

Tinker with Your Thinker

Tinker with Your Thinker

Howdy howdy howdy! It’s still January and it’s getting ready to be all snowy and icy here. Whee. . . It’s just my favorite. . . You know I’d much rather be scraping snow off my car in the extreme wind than say, sitting on the dock at the lake with a book and iced coffee eagerly awaiting a boat ride later in the afternoon.

Anyhoo, so recently I’ve been thinking a lot about knowledge and how much I know and how much I don’t know and such and such. I tend to go down the rabbit hole of thinking about how many topics and concepts and etc that I don’t know about or don’t know enough about and then just start wallowing in my thoughts. Which got me thinking. . .

Bart Simpson writing, "I will not wallow in my thoughts" on a chalkboard in a classroom.
Look, Ma! I found a

It really isn’t about how much you know, but what you do with the knowledge you do have. I have been trying to focus on what I do know and what I can accomplish with that knowledge, rather than getting discouraged by thinking about all the things I don’t know enough about. With the knowledge I do have, I can do a ton of stuff. And the more I do with what I have, the more potential I have to learn new things. For example, I know how to knit. I’d like to knit something new sometime soon (I want to make some cute mittens or maybe a sweater). I’d really like to have it be in a fair aisle pattern, but I’ve never attempted such a thing. Instead of me getting discouraged, I’m going to just pick a project and start it.

I’m not going to become an expert overnight in some subject, but I do know that I can learn new things every day. And all these new little tidbits of learning add up over time.

-Rachel

Debugging: Your Secret Learning Tool

Debugging: Your Secret Learning Tool

I really like going though tutorials and walkthroughs when I’m learning new things. I think that the best way for me to learn something is by doing, but generally I get overwhelmed as to know where to start. So I generally start off getting my feet wet with tutorials.

Stackblitz has been very convenient to learn with.

Right now I’m going through Angular’s app tutorial (see what I’ve done so far on GitHub) and I’ve enjoyed it. It is nice to have a guide and not beat your head against something at the beginning. However, I don’t think that tutorials are the best way for really grasping concepts. What I do think is good for that is debugging error messages.

One of the errors I encountered during this walkthrough was that when I added the different phones to the shopping cart, the cart would update, not with the correct item, but just the first item in the array. I then had to go through and figure out why that wasn’t working. It turns out that I just wasn’t referencing the route parameter correctly. Curse you, case sensitivity!

While debugging this incorrect behavior, I learned that the + operator returns a numeric representation of the object in JavaScript. I would not have researched and learned that little tidbit of information if I hadn’t been debugging an error.

So if you’re ever frustrated debugging, slogging through error messages or unexpected code behavior, remember that you’re learning!

-Rachel

Upgrade: My Word of the Year

Upgrade: My Word of the Year

Last year was the year of dealing with unexpected events. I’m not a huge fan of unexpected events and behaviors, myself. It took a lot of energy to handle everything last year, and I’m pooped out.

I’ve been thinking about resolutions, goals, and themes for this new year, and I think I’ve decided on Upgrade as my theme. I want the 2020 upgrade to encompass many things this year.

I want professional upgrades for me to look like learning new skills and applying them. It also means more meaningful networking with other people in town. A lot of the time (most of the time) I seriously doubt every skill and ability I have. I want to lessen those doubts this year. My big goal this year is to actually put a project into my GitHub repo. I have a ton of stuff in private repos and with work, but nothing out and about in public land.

Personal upgrades include upgrades to the house, upgrades in the amount of effort and energy I exert on getting did up on a daily basis, and upgrades in the amount of energy I spend socializing with family and friends. I generally exert the least amount of effort getting ready in the mornings if I get overwhelmed. If you’re into the enneagram at all, I’m a 5w4. It’s been a pretty handy tool in recognizing good and bad tendencies in myself and helping out with some personal growth.

Here’s to 2020 and some high hopes.

-Rachel