this post was submitted on 01 Oct 2023
473 points (96.8% liked)

Programmer Humor

32054 readers
2156 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

The audacity to do such a thing…

you are viewing a single comment's thread
view the rest of the comments
[–] wethegreenpeople@sopuli.xyz 62 points 11 months ago (4 children)

I distinctly remember asking this question during a 100 level programming class but I just can not remember why I'd ever want to do this?

What problem could I have possibly have been trying to solve where this would seem like the answer.

[–] Brainsploosh@lemmy.world 34 points 11 months ago (2 children)

A common problem (before learning it is impossible/fraught with danger) is categorisation, like sorting of strings.

Say you have a text, and need to count words of different lengths.

One intuitive approach is to pass through it once and add each word to a list for the corresponding length, as well as making lists as needed. No 7 letter words, no 7-letter-word-list, even though there are longer words.

As humans we're good at sorting things into an unknown number of categories, and we have to unlearn that for programming

[–] mumblerfish@lemmy.world 28 points 11 months ago (2 children)

Would one not just use a dict/hashmap with int keys labelling lengths and the list of strings as the values?

[–] Brainsploosh@lemmy.world 29 points 11 months ago* (last edited 11 months ago) (1 children)

A programmer might, as trained/conditioned by the limits of programming languages.

A human would intuitively not, these are meaningless and/or convoluted concepts to the untrained human.

[–] QuazarOmega@lemy.lol 16 points 11 months ago

I like the implication that programmers aren't humans, but a sort of alien being naturally apt at algorithmic thinking, while puny humans are an irrational species that needs to undergo training from the mighty race of programmers if they hope to get into the field brought us here by the aliens

[–] Diplomjodler@feddit.de 5 points 11 months ago (3 children)
[–] Silinde@lemmy.world 7 points 11 months ago

'List' is the correct abstract term for any data structure that holds a given number of values in an order, regardless of the implementation. So Python's List, or C++'s Array or Vector, or a Linked List are all considered lists in the abstract sense.

[–] mumblerfish@lemmy.world 5 points 11 months ago

I did use 'list' and forgot it is called an 'array' or 'vector' in other languages. So sure, close enough :-)

[–] avonarret1@programming.dev 3 points 11 months ago

Because those are limited to Python? 😜

[–] wethegreenpeople@sopuli.xyz 6 points 11 months ago (1 children)

This makes a ton of sense and I think you probably solved this mystery for me.

"Oh I need to iterate over something, and keep track of new information as I do it, therefore I should be able to create 'dynamic variables' as I progress."

[–] wewbull@feddit.uk 0 points 11 months ago

Yep, what you failed to realise at the time is you've just invented a dynamic data structure like a list or a dictionary.

[–] CoderKat@lemm.ee 14 points 11 months ago

I remember wondering this when I was first trying to self learn. It's because I needed a map (or list + struct or something) and was such a noob I didn't know what maps were. Whatever material I was learning from wasn't good enough, especially for winging things. Plus I was trying to learn C++ and maps aren't quite so built into the language as they are with a better first language like Python.

[–] RyeMan@lemmy.ml 13 points 11 months ago

In lower level languages like C/C++ the reason becomes much more apparent when you learn about memory allocation and management (as a bonus it also really helps to understand how OS's handle memory). Dynamically declaring variables in a loop would mean you need to allocate a chunk of memory for each variable that's generated on the fly, most of, if not all of the dynamically declared variables would not even use most of their allocated memory resulting in a ton of extra overhead and wasted space within memory. An array is usually the answer when someone asks how to dynamically define variables. With an array you allocate the space needed in memory and can iterate across it block by block resulting in more control and efficiency within your reserved memory block. Linked lists are also a fun thing to look into when you aren't sure how big your array needs to be. It's a hard question to answer in a 100 level class because the answer actually goes pretty deep into low level programming, operating system and hardware principles.

[–] apotheotic@beehaw.org 4 points 11 months ago

I distinctly remember having the same experience. For some reason I believed dynamic variable naming was a good idea. What was I on??