How hip hop can teach you to code

Avoiding the hip-hop comics?

My favorite part is defining a verse(number) function that takes a parameter which completely changes the output, pointlessly bundling all the verses into one big logical blob and necessitating a bunch of conditional testing to figure out what to do when called. Instead, the verses could be saved off into discrete verse1(), verse2(), and verse3() functions that allow verses to be moved around willy-nilly, as described in the article.

Maybe the programmer is trying to suggest that the lyrical content of each verse is somehow procedurally related, and that there are optimizations to be had by providing a single entry point for generating them… nah. That’s not what’s happening. My guess is that, since lyrics tend to NOT be procedurally generated, this is just a bad design.

Worse, using an ordinal to identify which verse is which is contrary to the goal of reordering them. Why would anyone think adding the ability to do this:

verse(2)
verse(1)
verse(3)

…is remotely sensible? The ‘first’ verse is now the ‘second’ verse, the second is the first, and only the third reflects it’s actual location in the song, by ordinal index. All we’ve done here is add an unnecessary layer of indirection that we can use to confuse ourselves about what the code is actually accomplishing.

You don’t design good code to put cool coding techniques into practice, you design it to reflect your intent. The implied intent of this design is “supporting the rearrangement of verses” which in the practical world of linear narratives is pretty darn useless. By providing that capability, you’re basically asking someone to use your code to do bad things. A good framework will not include these capabilities, thereby steering programmers into to using it correctly.

1 Like

You’re overcomplicating the issue. The point is to realize that you can define functions in a program in one place, and then call then in the main function whenever you want.

I didn’t include an option with verses 2 before 1 and 3 so not sure where you got that from, but it can make sense in terms of music ! There are hip hop songs that are originally written one way, and then the verses are switched around later.

All programmers do it differently.

2 Likes

Um … Thanks for the feedback?

3 Likes

Let me elaborate on my earlier, snarky comic reply.

You’ve mistaken this article for a programming tutorial. It is not actually trying to show you the best way to write a program that prints the lyrics to a single song. (Why would you even want to do that?) Shareef Jackson is a STEM diversity activist; he’s drawing a parallel between coding and hip hop (something many black kids will be familiar with) in order to show black kids that coding is something they can understand and do, and hopefully get them interested in the technical fields that are so dominated by white dudes. Using Verse(1) allows him to explain how functions (and specifically function parameters) work in a way that wouldn’t have been possible if he only used functions without parameters.

This topic was automatically closed after 5 days. New replies are no longer allowed.