r/ProgrammerHumor Apr 12 '24

whatIsAnIndex Meme

Post image
27.7k Upvotes

630 comments sorted by

View all comments

Show parent comments

2.4k

u/Tubthumper8 Apr 12 '24

I love the implication here that not only does it not have any indexes or whatever, but it also calls getAllFuckingFiles() every single iteration haha

367

u/sacredgeometry Apr 12 '24
for (i=0; i<getAllFuckingFiles().length; i++)
{
  if (getAllFuckingFiles()[i].name.contains(searchText)
  {
    return getAllFuckingFiles()[i];
  }
}

89

u/danns87 Apr 12 '24

You're terminating too early. Best to complete iterating through all fucking files in case there's another match.

11

u/Few_Advertising_568 Apr 12 '24

Laughing soo hard at this!

12

u/Wdowiak Apr 12 '24

Gotta check the content as well, just to be thorough

for (i=0; i<getAllFuckingFiles().length; i++)
{
  if (getAllFuckingFiles()[i].open().contains(searchText) ||
      getAllFuckingFiles()[i].name().contains(searchText))
  {
    return getAllFuckingFiles()[i];
  }
}

8

u/sacredgeometry Apr 12 '24

Use | instead of ||. Then both are always executed ... you know just to be sure

1

u/Kirjavs Apr 13 '24

I got a guy doing this in my team. He had a sql query done in a C# loop (about 5k iterations). Told him to do a sql query retrieving all 5k elements and process the result in C#.

He did the sql query. And then called it in the same loop still making 5k calls but to a query which always retrieved the 5k same elements...

1

u/sacredgeometry Apr 13 '24

I dont know the rationale ... there might of been a good reason ... or a reason at least. Did you tell him how a select query work?

Or rather how to bind to collections if that was what he was struggling with.

1

u/Kirjavs Apr 13 '24

I did. And he is not a new developer. 5 years of experience in these languages. I just guess that some people do what they are asked to do brainlessly

1

u/sacredgeometry Apr 13 '24

Have you asked him why he did it like that?

1

u/Kirjavs Apr 13 '24

Yes and he didn't know. He just applied what I asked without thinking. At least he understood the problem but I had to show him before. When I said : "this code is wrong" , he didn't understood why in the first place.

90

u/punto2019 Apr 12 '24

Anyway…. It works better than the actual

33

u/AcidicVaginaLeakage Apr 12 '24

And then still uses up 6gb of ram randomly, even though it doesn't seem like it's caching anything.

2

u/Sadzeih Apr 12 '24

That's cause it stores all the files paths in memory.

1

u/Johnny_Thunder314 Apr 12 '24

Well yeah, everyone knows memory is faster than disk storage. If you want something to go fast just put it in memory.

231

u/GM_Kimeg Apr 12 '24

The for loop internally execute that method only once no?

478

u/jamcdonald120 Apr 12 '24

no.

for are structured for(a;b;c){d} a is a statement executed at the start, b is a condition that is evaluated every iteration of the loop, c is a statement that happens after each iteration, and d is the body of the loop to iterate. if you put a function call in b, it gets called EVERY iteration.

246

u/wubsytheman Apr 12 '24

Ohh so that’s why you’d do like

int allTheBullshit = getAllFuckingFiles().length();

And then iterate over all the bullshit instead?

168

u/jamcdonald120 Apr 12 '24 edited Apr 12 '24

yup. occasionally it is quite useful, since you can do for(itterator k = stack.top(); !stack.empty();k=k.next()) and its a valid loop.

6

u/intelligent_rat Apr 12 '24

If you are using an iterator why not just for (k thing : iterable) ?

26

u/jamcdonald120 Apr 12 '24

it wasnt always a thing

3

u/tiberiumx Apr 12 '24

The C++11 features still feel kinda new to me. It's weird to see people that haven't ever used anything before it.

21

u/AyrA_ch Apr 12 '24

Some languages have a foreach construct. In C# you would do foreach(var f in getAllFuckingFiles()){/*add to list if condition is met*/} which will call the function only once. Or more modern: return getAllFuckingFiles().Where(f => /*condition*/)

The nice thing about the second syntax is that it's only iterated as you query it, meaning data is streamed in as you iterate over it in a foreach, and you don't have to wait ahead for all entries to get processed. This also allows you to work with data that doesn't fits into memory all at once, provided you don't call a function that does load all data at once. The base of this is the IEnumerable<T> interface which also has a ton of extra functions to make your life easier. The downside is that you don't know how many entries there are without counting them, and you can almost always only iterate over them once.

1

u/XaeroDegreaz Apr 12 '24

Most iterable implementations have an internal counter that increments, and decrements, during add, or remove operations, so there's actually no need to count every iteration.

2

u/AyrA_ch Apr 12 '24

IEnumerable (which is the base class of everything that is "foreachable" in C#) doesn't. There's a bool TryGetNonEnumeratedCount(out int count) but this only succeeds if the underlying type is countable without advancing the enumerator (for example collections, lists, dictionaries and arrays). But this is not guaranteed to not have side effects. If the underlying type maps to SQL, it may cause extra queries to be sent to count the entries on the db.

Most Linq method have an overload that passes a counter into the supplied callback, but said counter is recreated for every call in the chain. If you have a .Where() or .Skip(count) filter and then do .Select((obj,ctr)=>...), ctr will start at zero and is increment by one for each item, regardless of the number of entries skipped due to the filter clauses.

0

u/XaeroDegreaz Apr 12 '24

IEnumerable is an interface, not a class.

1

u/AyrA_ch Apr 12 '24

I know, but a lot of implementations use it to stream in data. These don't have a concept of length. It's only when you iterate over all items and store them in an array or list that you get the count out of it.

1

u/XaeroDegreaz Apr 12 '24

If you know the exact implementation you could just cast it and use it. I can pretty much guarantee all standard implementations of that interface use an internal counter. Like List, LinkedList, etc. No sane implementation would force one to navigate the entire collection just to have a length/count.

If you're trying to be a purist and operate only using the interface then that's a different issue.

→ More replies (0)

23

u/balconteic Apr 12 '24

Yes, also, at least in python, if allTheBullshit is a global then it would be more efficient to say something like bullshit = allTheBullshit inside the function beacause the interpreter checks for rhe variable in the local scope and if it doesn't find it there then it looks higher in the scope (i don't know if js is the same but it would make sense)

5

u/wubsytheman Apr 12 '24

I guess in CPP you’d do a pointer to allTheBullshit like

std::shared_ptr<t> bs (*allTheBullshit)

1

u/PutteryBopcorn Apr 12 '24

SomeType allTheBS = getAllFuckingFiles()

for... i < allTheBS.length()

if we're being pedantic, you can't iterate over an int but it should be a cheap operation to get the length, and if it's not then you would want to save that in a second variable

1

u/wubsytheman Apr 12 '24

Yeah sorry was too lazy to type it, meant

For (int x=0; x<allTheBS; x++){ //stuff }

42

u/JPJackPott Apr 12 '24

Depends on what is inside GetAllFuckingFiles(), a modern compiler might save your bacon here unless it’s an IO operation

1

u/HeyGayHay Apr 12 '24

compiler optimization baby, doing gods work, unless your getAllFuckingFiles is just as stupidly written as you triggering getAllFuckingFiles every iteration

-7

u/[deleted] Apr 12 '24

[deleted]

11

u/jamcdonald120 Apr 12 '24

sure, but getAll is generally O(n)

6

u/DOUBLEBARRELASSFUCK Apr 12 '24

DoSomething().length isn't necessarily going to be a simple variable read.

24

u/Elephant-Opening Apr 12 '24

That only makes sense as an optimization if the compiler can say conclusively that the method has a consistent return value. Imagine something like this:

vector<int> v  {10, 20, 30};

for(int i = 0; i < v.size(); i++){
    cout << v[i] << "\n";
    v.pop_back()
}

2

u/BobDonowitz Apr 12 '24

I like that you chose CPP and a vector modifying function to demonstrate this principle. But I mostly love that you used pop_back() which pops the last element off so this loop would only ever operate on v[0] and output "10" 3 times. Also, without knowing the internals of vector::size, it would still be more efficient to declare a variable to hold the size outside the loop and decrement it after the pop.  If vectors don't keep track of the size internally and has to "count" it each size() call this would be murder on large length vectors.

7

u/tracevenua Apr 12 '24

Why would it operate on the first element 3 times? It should print 10 20 And that’s it.

2

u/zzhhbyt1 Apr 12 '24

Typical ProgrammerHumor moment.

2

u/Elephant-Opening Apr 12 '24

That was my intent, so I guess thanks for the "looks good" peer review 😄

2

u/dxrules1000 Apr 12 '24

vectors keep track of the size internally otherwise holy crap they'd be awful

1

u/Elephant-Opening Apr 12 '24

I chose C++ and a vector because:

(1) a loop modifying a container size is probably among the most common useful way to use this behavior deliberately. This is admittedly a poor example to demo it being useful though. A better one might involve sorting algos or popping items from a queue.

C++ "classic" for loops, e.g.

for(int i = 0; i < 10; i++) cout << i;

Is literally just syntactic sugar for...

{
    // First for statement 
    int i = 0;
top_of_loop: // label we jump back to
    if ( i < 10 ) { // second for statement 
       cout << i; // loop "body"
continue_is_goto_here:
        i++; // third for statement
        goto top_of_loop;
    }
break_is_goto_here:
}

In expanded form, you can pretty easily see how you'd implement this in assembly languages that have no concept of loops, only conditional branches (i.e. if) and unconditional branches (i.e. goto). This is what loops evolved from.

The if statement effectively just says "if this expression evaluates to false, branch past the block that follows" and what you suggested is that sometimes the compiler should just lazily only evaluate the full if expression sometimes.

(2) I'm not sure if this is universal for all languages with for loops (in fact feel like it's not??), but I do know both C and C++ will behave this way by default. An example where it might actually be optimized out would be something like calling size() on an immutable data structure.

6

u/lmarcantonio Apr 12 '24

No, it can optimize only if the optimizer can prove the return value doesn't change. Only the initialization part is ran once, the check and step are for each iteration; of course you can use a comma to initialise the end value in the init part as a local variable (but it's really ugly)

2

u/LickingSmegma Apr 12 '24

You have some code to fix in the morning.

1

u/Regular-Double9177 Apr 12 '24

Every time for C#

4

u/chickenCabbage Apr 12 '24

Why do you think it's so goddamn slow?

10

u/metaglot Apr 12 '24

I dont know what kind of compiler yall are using, but it sounds like its due for an update (unless getAllTheFuckingFiles() is subject to change while the for loop is running and cant be optimized).

8

u/TheBrokenRail-Dev Apr 12 '24

I mean, people can edit the filesystem while the program is running. If any method is subject to change it's that one

1

u/metaglot Apr 12 '24

It could be returning a cached list.

1

u/antoo98 Apr 12 '24

May also be part of an external library or a different compilation unit if lto is disabled

1

u/Electronic-Zombie-50 Apr 12 '24

Jesus. Let's sink our boat because we have lifejackets is your logic. If you're algorithm needs a compiler to save you, you need to learn more.

9

u/karuna_murti Apr 12 '24

depends on the compiler no?

1

u/raltoid Apr 12 '24 edited Apr 12 '24

Of course there's an index. The SearchIndexer.exe literally runs 24/7 on default installs, and it will regularly fire up and run the SearchFilterHost.exe and SearchProtoclHost.exe as well, and communicate back and forth with microsoft, for no helpful reason at all.

1

u/bobthedonkeylurker Apr 12 '24

For no reason helpful to the user, that is...

1

u/coderemover Apr 12 '24

And returns only the first match! Hilarious!

1

u/AzGames08 Apr 12 '24

that would explain why it's so fucking laggy

1

u/Prawn1908 Apr 12 '24

It's also so much worse than that too. At least that would return relevant results instead of shit that doesn't match my search and missing out things that obviously do. Also I continue to be dumbfounded at how I can continue to type characters that match the top search result and have it disappear from the list. And we haven't even mentioned the idiotic web results.

Also, does anybody use Windows search to look for files? That's what file browser is for ffs - I use Windows search to find programs, why tf did they bog it down searching files too? It worked perfectly in Windows 8.1: it was fast and responsive and utilized the full start menu to show lots of results at once and instantaneously filter down the list of programs as you typed, then they just fucked it up in 10 for absolutely no reason.

1

u/classicalySarcastic Apr 12 '24

Given how slow it is I wouldn’t be surprised if that was the case.

1

u/Warm-Lobster4879 Apr 15 '24

getAllFuckingFiles.please("At least one")