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

Programmer Humor

19911 readers
2778 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.

top 50 comments
sorted by: hot top controversial new old
[–] fool@programming.dev 9 points 1 day ago

First, imagine a number in JavaScript. (Bit of a nail biter here, huh?)

let i = 5

Then, we will construct an incrementor. This is really simple: here is the method.

  1. Make a bracket-string-centric version of eval().
[]["filter"]["constructor"]("return i+1")()
  1. Reconstruct stringy eval() by using +[] as 0, +!+[] as 1, and implicit conversions as ways to create strings. For example, 'false' is (![]+[]), so 'f' is (![]+[])[+[]].
[][
  (![] + [])[+[]] + // f
  ([![]] + [][[]])[+!+[] + [+[]]] + // i
  (![] + [])[!+[] + !+[]] + // l
  (!![] + [])[+[]] + // t
  (!![] + [])[!+[] + !+[] + !+[]] + // e
  (!![] + [])[+!+[]] // r
][
  ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
  (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
  ([][[]]+[])[+!+[]]+ // n
  (![]+[])[!+[]+!+[]+!+[]]+ // s
  (!![]+[])[+[]]+ // t
  (!![]+[])[+!+[]]+ // r
  ([][[]]+[])[+[]]+ // u
  ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
  (!![]+[])[+[]]+ // t
  (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
  (!![]+[])[+!+[]] // r
]("return i+1")()
  1. Draw the rest of the fucking owl. Final code:
let i = 5; // haha yay

[][
  (![] + [])[+[]] + // f
  ([![]] + [][[]])[+!+[] + [+[]]] + // i
  (![] + [])[!+[] + !+[]] + // l
  (!![] + [])[+[]] + // t
  (!![] + [])[!+[] + !+[] + !+[]] + // e
  (!![] + [])[+!+[]] // r
][
  ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
  (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
  ([][[]]+[])[+!+[]]+ // n
  (![]+[])[!+[]+!+[]+!+[]]+ // s
  (!![]+[])[+[]]+ // t
  (!![]+[])[+!+[]]+ // r
  ([][[]]+[])[+[]]+ // u
  ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c
  (!![]+[])[+[]]+ // t
  (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o
  (!![]+[])[+!+[]] // r
](
  (!![]+[])[+!+[]]+ // r
  (!![]+[])[!+[]+!+[]+!+[]]+ // e
  (!![]+[])[+[]]+ // t
  ([][[]]+[])[+[]]+ // u
  (!![]+[])[+!+[]]+ // r
  ([][[]]+[])[+!+[]]+ // n
  (+[![]]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+ // ' '
  ([![]]+[][[]])[+!+[]+[+[]]]+ // i
  (+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[!+[]+!+[]]+ // +
  +!+[] // 1
)()
// no virus i swear. execute arbitrary code in your browser console.

Anyway, that's just everyday JS work. It's like step 5 after resizing the button, but a bit before centering the div.

based on this. ~some~ ~translation~ ~methods~ ~done~ ~differently.~

[–] CameronDev@programming.dev 112 points 3 days ago (4 children)

My favourite one is:

i -=- 1

[–] palordrolap@fedia.io 25 points 2 days ago (2 children)

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 20 points 2 days ago

Every day, JS strays further from gods light :D

load more comments (1 replies)
[–] DahGangalang@infosec.pub 33 points 3 days ago (1 children)

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

[–] mogranja@lemmy.world 8 points 2 days ago

Upvote for the stormlight archives reference.

[–] GissaMittJobb@lemmy.ml 19 points 3 days ago

The hot dog-operator

load more comments (1 replies)
[–] PlexSheep@infosec.pub 16 points 2 days ago (2 children)

Conditional adder:

if x==1:
    return 2
else if x==2:
    return 3
...
[–] driving_crooner@lemmy.eco.br 4 points 2 days ago

Can't increment negative numbers or zero smh.

Fix:

if x==0:
    return 1
else x==1:
    return 2
else if x==-1:
    return 0
else if x==2
    return 3
else if x==-2
    return -1
...
[–] Dyskolos@lemmy.zip 3 points 2 days ago* (last edited 1 day ago) (2 children)

Hard to beat this. If you ever manage to finish the code that is...

[–] SomethingBurger@jlai.lu 7 points 2 days ago* (last edited 2 days ago) (1 children)

There is a finite number of integers in this context. For 32-bit it, it's 4 billion or so (2^32).

[–] Sas@beehaw.org 5 points 2 days ago

And you could write a script to write the code for you

[–] freohr@lemmy.world 6 points 2 days ago (1 children)

But that's what TDD is for. If the test fails for 55, you just add a return 56, and then all is well.

load more comments (1 replies)
[–] letsgo@lemm.ee 10 points 2 days ago (1 children)

C++:

int i = 5;
i ^= printf("The initial value of i is %d\n", i)^
printf("i=i+1; // this increments i\n")^
printf("Trigger very obscure FPU bug %c",(int)((float)8.5953287712*(double)8.5953287713-'?'))/10;
printf("i has now been incremented by 1 : %d\n", i);

Output:

The initial value of i is 5  
i=i+1; // this increments i  
Trigger very obscure FPU bug  
i has now been incremented by 1 : 6

I didn't test other values but they're probably OK.

[–] Eranziel@lemmy.world 3 points 1 day ago* (last edited 1 day ago)

I didn't test other values but they're probably OK.

Excellent work, thanks for the laugh.

[–] notabot@lemm.ee 23 points 2 days ago

Trying to avoid using any arithmetic operators, and sticking just to binary (extending beyond 16 bit unsigned ints is left as an exercise for the interested reader):

#!/usr/bin/perl

# This increments $i

my $i=1;
print "Start: $i ";

if (($i & 0b1111111111111111) == 0b1111111111111111) {die "Overflow";}
if (($i & 0b0000000000000001) == 0b0000000000000000) {$i=(($i & 0b1111111111111110) | 0b0000000000000001);}
else
{
        if (($i & 0b0111111111111111) == 0b0111111111111111) {$i=(($i & 0b0000000000000000) | 0b1000000000000000);}
        if (($i & 0b0011111111111111) == 0b0011111111111111) {$i=(($i & 0b1000000000000000) | 0b0100000000000000);}
        if (($i & 0b0001111111111111) == 0b0001111111111111) {$i=(($i & 0b1100000000000000) | 0b0010000000000000);}
        if (($i & 0b0000111111111111) == 0b0000111111111111) {$i=(($i & 0b1110000000000000) | 0b0001000000000000);}
        if (($i & 0b0000011111111111) == 0b0000011111111111) {$i=(($i & 0b1111000000000000) | 0b0000100000000000);}
        if (($i & 0b0000001111111111) == 0b0000001111111111) {$i=(($i & 0b1111100000000000) | 0b0000010000000000);}
        if (($i & 0b0000000111111111) == 0b0000000111111111) {$i=(($i & 0b1111110000000000) | 0b0000001000000000);}
        if (($i & 0b0000000011111111) == 0b0000000011111111) {$i=(($i & 0b1111111000000000) | 0b0000000100000000);}
        if (($i & 0b0000000001111111) == 0b0000000001111111) {$i=(($i & 0b1111111100000000) | 0b0000000010000000);}
        if (($i & 0b0000000000111111) == 0b0000000000111111) {$i=(($i & 0b1111111110000000) | 0b0000000001000000);}
        if (($i & 0b0000000000011111) == 0b0000000000011111) {$i=(($i & 0b1111111111000000) | 0b0000000000100000);}
        if (($i & 0b0000000000001111) == 0b0000000000001111) {$i=(($i & 0b1111111111100000) | 0b0000000000010000);}
        if (($i & 0b0000000000000111) == 0b0000000000000111) {$i=(($i & 0b1111111111110000) | 0b0000000000001000);}
        if (($i & 0b0000000000000011) == 0b0000000000000011) {$i=(($i & 0b1111111111111000) | 0b0000000000000100);}
        if (($i & 0b0000000000000001) == 0b0000000000000001) {$i=(($i & 0b1111111111111100) | 0b0000000000000010);}
}
print "End: $i\n";
[–] henfredemars@infosec.pub 64 points 3 days ago* (last edited 3 days ago) (3 children)

Typing on mobile please excuse.

i = 0
while i != 1:
  pass

# i is now 1

Reminds me of miracle sort.

[–] Scoopta@programming.dev 64 points 3 days ago (1 children)

Ah yes, the wait for a random bit flip to magically increment your counter method. Takes a very long time

[–] henfredemars@infosec.pub 41 points 3 days ago (1 children)

The time it takes for the counter to increment due to cosmic rays or background radiation is approximately constant, therefore same order as adding one. Same time complexity.

Constant time solution. Highly efficient.

[–] frezik@midwest.social 21 points 3 days ago* (last edited 3 days ago) (1 children)

If you do it on a quantum computer, it goes faster because the random errors pile up quicker.

[–] Birch@sh.itjust.works 15 points 3 days ago

Finally, a useful real world application for quantum computing!

load more comments (1 replies)
[–] kayzeekayzee@lemmy.blahaj.zone 37 points 3 days ago (2 children)

I like to shake the bytes around a little

i = ( i << 1 + 2 ) >> 1
[–] rhpp@programming.dev 5 points 2 days ago

Wait, why does it multiply by 4? (apparently addition takes precedence over bitwise operations)

load more comments (1 replies)
[–] tiefling@lemmy.blahaj.zone 19 points 3 days ago* (last edited 3 days ago) (1 children)

Use bitwise operations to simulate an adder. Bonus points if you only use NOR

[–] 18107@aussie.zone 6 points 2 days ago* (last edited 2 days ago)

I decided to use NAND instead of NOR, but it's effectively the same thing.

Scala:

//main
@main
def main(): Unit =
  var i = 15 //Choose any number here
  i = add(i, 1) //this increments i
  println(i)

//Adds 2 numbers in the most intuitive way
def add(a: Int, b: Int): Int =
  val pairs = split(a).zip(split(b))
  val sumCarry = pairs.scanLeft(false, false)((last, current) => fullAdder(current._1, current._2, last._2))
  return join(sumCarry.map(_._1).tail.reverse)

//Converts an integer to a list of booleans
def join(list: Seq[Boolean]): Int = BigInt(list.map(if (_) '1' else '0').mkString, 2).toInt

//Converts a list of booleans to an integer
def split(num: Int): Seq[Boolean] = num.toBinaryString.reverse.padTo(32, '0').map(_ == '1')

//Adds 2 booleans and a carry in, returns a sum and carry out
def fullAdder (a: Boolean, b: Boolean, c: Boolean): (Boolean, Boolean) =
  (NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c)), NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c), c)), NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c), NAND(a, b)))

//The basis for all operations
def NAND(a: Boolean, b: Boolean): Boolean = !a || !b

EDIT: replaced Integer.parseInt with BigInt(...).toInt to fix NumberFormatException with negative numbers.

try it online here

[–] Minaltaz@sh.itjust.works 6 points 2 days ago* (last edited 2 days ago) (1 children)

C++ templates my beloved

#pragma pack(1)
template <size_t i> struct increment;
template <> struct increment<0> {
	char _plusone;
	constexpr static size_t value = 1;
};

template <size_t i> struct increment<i> {
	increment<i - 1> _base;
	char _plusone;  // this increments i
    constexpr static size_t value = sizeof(increment<i - 1>);
};

template <size_t i>
using increment_v<i> = increment<i>::value;

This could even be made generic to any integer type with a little more effort

[–] Minaltaz@sh.itjust.works 4 points 2 days ago* (last edited 2 days ago)

Bonus : to use it without knowing i at compile-time :

template <size_t current_value = 0>
size_t& inc(size_t& i) {
	if (i == current_value) {
		i = increment<current_value>::value;
		return i;
	} else {
		if constexpr (current_value != std::numeric_limits<size_t>::max()) {
			return inc<increment<current_value>::value>(i);
		}
	}
}

int main() {
	int i;
	std::cin >> i;
	inc(i);  // this increments i
	std::cout << i << std::endl;
	return 0;
}

Hope you're not in a hurry

[–] dont_lemmee_down@lemm.ee 32 points 3 days ago

Create a python file that only contains this function

def increase_by_one(i):
    # this increments i
    f=open(__file__).read()
    st=f[28:-92][0]
    return i+f.count(st)

Then you can import this function and it will raise an index error if the comment is not there, coming close to the most literal way

Any code which does not contain the comment "this increments i:" will produce a compile error and fail to run.

could be interpreted in python

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

C# .NET using reflection, integer underflow, and a touch of LINQ. Should work for all integer types. (edit: also works with char values)

// this increments i
private static T Increment<T>(T i)
{
    var valType = typeof(T);
    var maxField = valType.GetField("MaxValue");
    var minField = valType.GetField("MinValue");
    if (maxField != null)
    {
        T maxValue = (T)maxField.GetValue(i);
        T minValue = (T)minField.GetValue(i);

        var methods = valType.GetTypeInfo().DeclaredMethods;
        var subMethod = methods.Where(m => m.Name.EndsWith("op_Subtraction")).First();
               
        T interim = (T)subMethod.Invoke(
            null,
            [i, maxValue]);

        return (T)subMethod.Invoke(
            null, 
            [interim, minValue]);
    }
    throw new ArgumentException("Not incrementable.");
}
[–] Azzu@lemm.ee 29 points 3 days ago* (last edited 3 days ago) (1 children)
int toIncrement = ...;
int result;
do {
  result = randomInt();
} while (result != (toIncrement + 1));
print(result);
[–] AcesFullOfKings@feddit.uk 12 points 3 days ago* (last edited 3 days ago)

haha, bogoincrement! I hadn't thought of that, nice :D

(shame it fails to compile though)

[–] calcopiritus@lemmy.world 7 points 2 days ago (1 children)

C:

int increment(int i) {
    return (int) (1[(void*) i])

However, if you wanna go blazingly fast you gotta implement O(n) algorithms in rust. Additionally you want safety in case of integer overflows.

use std::error::Error;

#[derive(Debug, Error)]
struct IntegerOverflowError;

struct Incrementor {
    lookup_table: HashMap<i32, i33>
}

impl Incrementor {
    fn new() -> Self {
        let mut lut = HashMap::new();
        for i in 0..i32::MAX {
            lut.insert(i, i+1)
        }
        Incrementor { lookup_table: lut }
    }

    fn increment(&self, i: i32) -> Result<i32, IntegerOverflowError> {
        self.lookup_table.get(i)
            .map(|i| *i)
            .ok_or(IntegerOverflowError)
}

On mobile so I don't even know if they compile though.

[–] hitwright@lemmy.world 4 points 2 days ago (1 children)

Using i33 for that extra umpf!

load more comments (1 replies)
[–] kogasa@programming.dev 15 points 3 days ago* (last edited 3 days ago) (2 children)

Let f(x) = 1/((x-1)^(2)). Given an integer n, compute the nth derivative of f as f^((n))(x) = (-1)^(n)(n+1)!/((x-1)^(n+2)), which lets us write f as the Taylor series about x=0 whose nth coefficient is f^((n))(0)/n! = (-1)^(-2)(n+1)!/n! = n+1. We now compute the nth coefficient with a simple recursion. To show this process works, we make an inductive argument: the 0th coefficient is f(0) = 1, and the nth coefficient is (f(x) - (1 + 2x + 3x^(2) + ... + nx^(n-1)))/x^(n) evaluated at x=0. Note that each coefficient appearing in the previous expression is an integer between 0 and n, so by inductive hypothesis we can represent it by incrementing 0 repeatedly. Unfortunately, the expression we've written isn't well-defined at x=0 since we can't divide by 0, but as we'd expect, the limit as x->0 is defined and equal to n+1 (exercise: prove this). To compute the limit, we can evaluate at a sufficiently small value of x and argue by monotonicity or squeezing that n+1 is the nearest integer. (exercise: determine an upper bound for |x| that makes this argument work and fill in the details). Finally, evaluate our expression at the appropriate value of x for each k from 1 to n, using each result to compute the next, until we are able to write each coefficient. Evaluate one more time and conclude by rounding to the value of n+1. This increments n.

[–] SatouKazuma@programming.dev 2 points 1 day ago (1 children)

OP asked for code, not a lecture in number theory.

That said, as someone with a degree in math...I gotta respect this.

[–] kogasa@programming.dev 2 points 1 day ago* (last edited 1 day ago)

The argument describes an algorithm that can be translated into code.

1/(1-x)^(2) at 0 is 1

(1/(1-x)^(2) - 1)/x = (1 - 1 + 2x - x^(2))/x = 2 - x at 0 is 2

(1/(1-x)^(2) - 1 - 2x)/x^(2) = ((1 - 1 + 2x - x^(2) - 2x + 4x^(2) - 2x^(3))/x^(2) = 3 - 2x at 0 is 3

and so on

[–] lime@feddit.nu 7 points 2 days ago

calm down, mr Knuth

[–] frezik@midwest.social 15 points 3 days ago* (last edited 3 days ago) (1 children)
$i = 0;
$s = "foobar";
$i+=$s=~/(oo)/; # This increments $i
say $i;
[–] IronKrill@lemmy.ca 7 points 3 days ago (2 children)

I have no idea how your code works but I appreciate it for the excited guy /(oo)/

load more comments (2 replies)
[–] Boomkop3@reddthat.com 6 points 2 days ago* (last edited 2 days ago)

writing code on my phone on the train with three minutes of time, let's go and think of the worst possible solution!


int i = 1337;

for (byte x = 0; x < 32; x++) {
    if (i & (1 << x) == 1) {
        i &= 0xFFFFFFFF ^ ( // dangit I'm out of time
    } else {
        i |= 1 << x;
        break;
    }
}


Instead of a loop, you'd use 32 of these copy-pasted if statements and a goto :p

[–] Sonotsugipaa@lemmy.dbzer0.com 15 points 3 days ago* (last edited 3 days ago) (6 children)
// C++20

#include <concepts>
#include <cstdint>

template <typename T>
concept C = requires (T t) { { b(t) } -> std::same_as<int>; };

char b(bool v) { return char(uintmax_t(v) % 5); }
#define Int jnt=i
auto b(char v) { return 'int'; }

// this increments i:
void inc(int& i) {
  auto Int == 1;
  using c = decltype(b(jnt));
  // edited mistake here: c is a type, not a value
  // i += decltype(jnt)(C<decltype(b(c))>);
  i += decltype(jnt)(C<decltype(b(c(1)))>);
}

I'm not quite sure it compiles, I wrote this on my phone and with the sheer amount of landmines here making a mistake is almost inevitable.

[–] henfredemars@infosec.pub 11 points 3 days ago (1 children)

I think my eyes are throwing up.

[–] Sonotsugipaa@lemmy.dbzer0.com 9 points 3 days ago

Just surround your eyes with try { ... } catch(Up& up) { }, easy fix

load more comments (5 replies)
[–] db2@lemmy.world 15 points 3 days ago (1 children)
[–] AcesFullOfKings@feddit.uk 12 points 3 days ago (1 children)
[–] db2@lemmy.world 37 points 3 days ago

No not bool, int

[–] redxef@feddit.org 8 points 3 days ago

Something like that should do it:

i = ~((~i + 1) + ~0) + 1
[–] ptz@dubvee.org 12 points 3 days ago* (last edited 3 days ago) (1 children)
// this increments i:
// Version 2: Now more efficient; only loops to 50 and just rounds up.  That's 50% less inefficient!

function increment(val:number): number {
  for (let i:number = 0; i < 50; i = i +1) {
    val = val + 0.01
  }

  return Math.round(val)
}

let i = 100
i = increment(i)
// 101
[–] Susaga@sh.itjust.works 12 points 3 days ago (1 children)

This should get bonus points for incrementing i by 1 as part of the process for incrementing i by 1.

load more comments (1 replies)
load more comments
view more: next ›