this post was submitted on 19 Jul 2023
276 points (95.7% liked)

Programmer Humor

18250 readers
1819 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 1 year ago
MODERATORS
 

saw a 2nd year CS guy run a maze solver (for final exam) with 300 nested if-statements. worked about 50% of the time. so he added another 5000 nested ifs and got it up to 90%. good enough!

top 18 comments
sorted by: hot top controversial new old
[–] [email protected] 70 points 11 months ago (4 children)

"CS Major" is one of my favourite memes

[–] [email protected] 43 points 11 months ago (3 children)

It's so rare for me to have to use the modulo operator I'm actually excited when I come across a situation where I can.

[–] [email protected] 11 points 11 months ago (1 children)

It's like how when you were a kid, you thought quicksand was something you would need to be aware of as an adult in the big wide world. In my undergrad, mod everywhere. Very important knowledge. Now not so much.

[–] [email protected] 9 points 11 months ago

I (a teen boy) was with a bunch of other dudes on a river, one found quicksand, and it quickly became a game of who could get deepest

[–] [email protected] 7 points 11 months ago* (last edited 11 months ago)

I did it the other day!

I was trying to pad a base64-encoded string with the proper number of = characters to get it up to a multiple of 4 because our stupid build toolchain would explode if it wasn't.

[–] [email protected] 5 points 11 months ago

I'm using it regularly to convert metric into imperial fractions.

I'm writing for a factory that is manufacturing windows and door so I'm this field everything is imperial imperial fractions. When I arrived we switched all the internal measurement to metric because imperial SUCKS for automatic processes.

I still need to do the conversion for everything that will be shown to the client.

[–] [email protected] 17 points 11 months ago

Cs get degree

[–] chandz05 6 points 11 months ago

Wow I literally laughed out loud

[–] [email protected] 2 points 11 months ago

i logged in to upvote you, thank you

[–] [email protected] 49 points 11 months ago* (last edited 11 months ago) (1 children)

In my first CS class, the professor announced an extra credit project due at the end of the semester. It was to create a formatted terminal calendar given a year from user input. I finished it after learning about condition but before I learned about classes... or functions... or loops... or searching the internet... partially. I searched how leap years worked, but didn't bother to search for code (Stack Overflow didn't exist yet)

Anyway, long ass program with each month hard-coded with 7 possible calendars for each month depending on the first day of the week. Lots of copy and paste. Professor was speechless, but accepted it.

[–] [email protected] 10 points 11 months ago

"It's not stupid if it works"

[–] [email protected] 19 points 11 months ago* (last edited 11 months ago) (2 children)

300 nested if statements, (...) added another 5000 nested if statements.

At this point I want to doubt that they actually wrote it themselves, vs writing a metaprogram to generate the code.

[–] [email protected] 4 points 11 months ago

Not sure if that would've made it better or worse 🤔

[–] [email protected] 1 points 11 months ago

Oh, a metaprogram?

ifs printing ifs...

[–] [email protected] 17 points 11 months ago (2 children)

One of the first real programs I wrote was a program to display telemetry data from a CAN bus. I was on the solar car team at uni, and we wanted to be able to view the data from the various systems live during the race. The CAN network was connected to a CAN-ethernet converter, which sent UDP packets over a wireless ethernet link to our lead car. I had no experience with networking, or UDP or CAN at all, but I had some documentation and a lot of free time, so I got to work.

Each device on the CAN network had a bit mask to identify it. For example, the bit mask for the motor controller might have been 0x1200. This meant that any packet starting with 0x12 belonged to the motor controller. For example, 0x1201 was one type of message, and 0x1202 another type, but both belonged to the motor controller.

There was specific logic for each device on the network, so you needed to first figure out which device owned a packet using the bit mask, then apply the relevant logic to decode the packet.

Looking back, I realise the correct way to approach this would be to have a list of bit masks:

0x1200
0x1300
0x1400

Then simply bitwise & any incoming packet with 0xff00, and lookup the result in the list of bit masks.

Not knowing better however, what I actually did was create a giant dictionary of every possible packet value, so I could lookup any packet and determine which system it came from. This was so repetitive that I had to make use of my newfound super-power -- vim macros -- to complete the 8000 line dictionary...

Excerpt from real code below:

{
    0x102:
    {
        'name':             'SHUNT_CMU_STATUS_TEMPERATURE_AND_VOLTAGE_1_2',
        'data':
        [
            'cell_0_voltage',
            'cell_1_voltage',
            'cell_2_voltage',
            'cell_3_voltage',
        ],
        'unpack_string':    'intle:16, intle:16, intle:16, intle:16'
    },

    0x103:
    {
        'name':             'SHUNT_CMU_STATUS_TEMPERATURE_AND_VOLTAGE_1_3',
        'data':
        [
            'cell_4_voltage',
            'cell_5_voltage',
            'cell_6_voltage',
            'cell_7_voltage',
        ],
        'unpack_string':    'intle:16, intle:16, intle:16, intle:16'
    },
}
[–] [email protected] 3 points 11 months ago (1 children)

If it works, it works. Let your successor worry about maintainabilty.

[–] [email protected] 2 points 11 months ago

It worked, but it was slow and dropped packets sometimes. I think the next team switched to Java. I met with them and walked them through the code and suggested they try a different approach. Hopefully they did!

[–] SniffBark 3 points 11 months ago

Well, we have all been there and fortunately learned from these “mistakes” xd

load more comments
view more: next ›