151
submitted 1 day ago* (last edited 3 hours ago) by AcesFullOfKings@feddit.uk to c/programmer_humor@programming.dev

A couple of years ago, my friend wanted to learn programming, so I was giving her a hand with resources and reviewing her code. She got to the part on adding code comments, and wrote the now-infamous line,

i = i + 1 #this increments i

We've all written superflouous comments, especially as beginners. And it's not even really funny, but for whatever reason, somehow we both remember this specific line years later and laugh at it together.

Years later (this week), to poke fun, I started writing sillier and sillier ways to increment i:

Beginner level:

# this increments i:
x = i 
x = x + int(True)
i = x

Beginner++ level:

# this increments i:
def increment(val):
   for i in range(val+1):
      output = i + 1
   return output

Intermediate level:

# this increments i:
class NumIncrementor:
	def __init__(self, initial_num):
		self.internal_num = initial_num

	def increment_number(self):
		incremented_number = 0
		# we add 1 each iteration for indexing reasons
		for i in list(range(self.internal_num)) + [len(range(self.internal_num))]: 
			incremented_number = i + 1 # fix obo error by incrementing i. I won't use recursion, I won't use recursion, I won't use recursion

		self.internal_num = incremented_number

	def get_incremented_number(self):
		return self.internal_num

i = input("Enter a number:")

incrementor = NumIncrementor(i)
incrementor.increment_number()
i = incrementor.get_incremented_number()

print(i)

Since I'm obviously very bored, I thought I'd hear your take on the "best" way to increment an int in your language of choice - I don't think my code is quite expert-level enough. Consider it a sort of advent of code challenge? Any code which does not contain the comment "this increments i:" will produce a compile error and fail to run.

No AI code pls. That's no fun.

you are viewing a single comment's thread
view the rest of the comments
[-] CameronDev@programming.dev 107 points 1 day ago

My favourite one is:

i -=- 1

[-] palordrolap@fedia.io 17 points 1 day ago

This is actually the correct way to do it in JavaScript, especially if the right hand side is more than 1.

If JavaScript thinks i contains a string, and let's say its value is 27, i += 1 will result in i containing 271.

Subtraction doesn't have any weird string-versus-number semantics and neither does unary minus, so i -=- 1 guarantees 28 in this case.

For the increment case, ++ works properly whether JavaScript thinks i is a string or not, but since the joke is to avoid it, here we are.

[-] CameronDev@programming.dev 16 points 22 hours ago

Every day, JS strays further from gods light :D

[-] Feathercrown@lemmy.world 1 points 15 hours ago

The solution is clear: Don't use any strings

[-] GissaMittJobb@lemmy.ml 19 points 1 day ago

The hot dog-operator

[-] DahGangalang@infosec.pub 30 points 1 day ago

The near symmetry, ah, I see weve found the true Vorin solution.

[-] mogranja@lemmy.world 7 points 20 hours ago

Upvote for the stormlight archives reference.

[-] spongebue@lemmy.world 10 points 1 day ago

It looks kinda symmetrical, I can dig it!

this post was submitted on 06 Jan 2025
151 points (96.3% liked)

Programmer Humor

19896 readers
1050 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS