this post was submitted on 06 Jan 2025
158 points (95.9% liked)

Programmer Humor

19932 readers
2159 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
 

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.

(page 2) 32 comments
sorted by: hot top controversial new old
[–] eager_eagle@lemmy.world 9 points 3 days ago

That's a tricky problem, I think you might be able to create a script that increments it recursively.

I'm sure this project that computes Fibonacci recursively spawning several docker containers can be tweaked to do just that.

https://github.com/dgageot/fiboid

I can't think of a more efficient way to do this.

[–] Blue_Morpho@lemmy.world 9 points 3 days ago

Your intermediate increment looks like serious JavaScript code I've seen.

[–] bjoern_tantau@swg-empire.de 7 points 3 days ago* (last edited 3 days ago)

Everything is easier in PHP!

<?php

/**
 * This increments $i
 * 
 * @param int $i The number to increment.
 *
 * @return int The incremented number.
 */
function increment(int $i) {
  $factor = 1;
  $adjustment = 0;
  if ($i < 0) {
    $factor *= -1;
    $adjustment = increment(increment($adjustment));
  }
  $i *= $factor;
  $a = array_fill(1, $i, 'not_i');
  if ($i === 0) {
    array_push($a, 'not_i');
  }
  array_push($a, $i);
  return array_search($i, $a, true) * $factor + $adjustment;
}
[–] j4k3@lemmy.world 7 points 3 days ago

74181


(A + 1)
A0:A3 = (Input Register)
S0:S3 = Low
Mode = Low
CaryN = High
Q1:Q4 = (Output)


https://en.wikipedia.org/wiki/74181

::: spoiler . Funny enough, it is one of the understood operations that I did not integrate on the truth table on-chip. I had some ideas on extra syntax, but the point is to avoid needing to look at reference docs as much as possible and none of my ideas for this one were intuitive enough this satisfy me.

[–] joyjoy@lemm.ee 6 points 3 days ago (1 children)
// this increments i
var i = new AtomicInteger(0);
i.increment(); 
[–] henfredemars@infosec.pub 3 points 3 days ago

The best solution for the concurrent and atomic age.

[–] lugal@lemmy.dbzer0.com 6 points 3 days ago
i = max(sorted(range(val, 0, -1))) + 2
[–] subignition@fedia.io 4 points 3 days ago
// this increments i: 
i = i+i---i;
[–] magic_lobster_party@fedia.io 3 points 3 days ago

Where’s NumIcrementorFactory?

[–] Redjard@lemmy.dbzer0.com 2 points 3 days ago

Isn't beginner++ gonna leave it unchanged?
range(val) iterates from 0 to val-1, so the final i+1 is val

[–] sik0fewl@lemmy.ca 2 points 3 days ago* (last edited 3 days ago)

Java has AtomicInteger, which is probably one of the more complicated, but also robust, ways of setting an integer.

[–] Nougat@fedia.io 1 points 3 days ago

With a magnifying glass.

Oh wait. Not "incinerate an ant." I got nothing then.

load more comments
view more: ‹ prev next ›