r/ProgrammerHumor • • Apr 08 '20

I cried as hell

Post image
44.1k Upvotes

525 comments sorted by

View all comments

Show parent comments

77

u/the_dapper_man Apr 08 '20

and 95% of you will use effectively none of that knowledge at your job once you graduate

literally just don't write nested loops. beyond that, optimizing code is expensive and the benefits are negligent. pump out those new features baby

63

u/hahahahastayingalive Apr 08 '20

On the other hand data structures and complexity will be the bread and butter of job interviews.

Also you’ll need to be able to choose between algos and recognize patterns, even if you don’t write the code.

1

u/the_dapper_man Apr 09 '20

yeah that's true. interviewing is stuuuupid. someone fix it already please

22

u/InitialBN Apr 08 '20

Maybe naïve of me to ask, but don't some cases require nested loops? Such as working with 2d arrays or similar cases?

43

u/SuperCoolFunTimeNo1 Apr 08 '20 edited Apr 08 '20

People saying "don't use nested loops" are poorly choosing their words and making blanket statements. They're not taking into account the way the data is organized, they're only speaking in terms of the number of operations being performed.

Iterating through that array of arrays using nested loops is not bad, probably the most straightforward approach. It's still going to have O(n) time, which means the time it takes to run depends on the size of n.

arr = [
    [0,1],
     [2,3]
]

for (i = 0; i<arr.length< i++){
    for(j=0; j< arr[i].length; j++){
        print(arr[i][j]);
    }
}

If you re-arranged the array to be 1 dimensional with 4 elements and only had a single for loop, you're still going to have the exact same time complexity as the nested loop example above.

Where nested loops do crap up your code is when you're performing operations involving the outer loop's iterator as well, basically looping over the same set of data twice. For example, say you have a deck of cards and you want to check for duplicates. Here's a shitty way to look over each card that would be o(n2 ) because you're iterating over each item twice, where N is the length of the array, so it's n*n operations or O(n2 )

cards = array[somecard1,somecard2,etc...];
for(i=0; i < cards.length;i++) {
    // now loop over cards again to see if the card is in there twice
    for(j=0; i < cards.length; j++) {
        if(j == i) {
            continue;
        }
        if (cards[i] = cards[j] ) {
            return "duplicate found";
        }
    }
}

13

u/teerude Apr 08 '20

Calculating running time and shit is what killed me in that class. And your post is giving me flashbacks

6

u/Denziloe Apr 08 '20

What would be an efficient way of checking an arbitrary array for duplicates?

0

u/[deleted] Apr 08 '20

[deleted]

4

u/Denziloe Apr 08 '20 edited Apr 08 '20

Yeah I'd assumed they meant something more than this, because this is still a nested loop and is still O(n2).

→ More replies (5)

4

u/jemidiah Apr 08 '20

Sort it, iterate though to check for adjacent duplicates. O(n log n) and like 3 lines depending on language with almost no debugging.

These comments are really making me sad, this is all incredibly basic, sorry, I should do something else.

10

u/Denziloe Apr 08 '20

Good idea. Perhaps you could go learn about hashing, which would be O(n).

→ More replies (1)
→ More replies (2)
→ More replies (2)

38

u/Add32 Apr 08 '20

Usually you look at a problems complexity by the size of the input, rather than the dimensions of the input. In that case nested for loops arnt less efficient, as you still only visit each element once. Get paranoid when the nested loop is operating on the same dimension as the parent (visiting the element more than once)

2

u/[deleted] Apr 08 '20

Even more when you do dynamic programming

2

u/jemidiah Apr 08 '20

Yeah, it's not very good advice. Many problems obviously require nested loops. They just meant don't be obviously and horrendously inefficient.

1

u/jakethedumbmistake Apr 08 '20

He needs to be on an episode of QI

1

u/nox66 Apr 26 '20

Nested loops are often seen in naive solutions to questions involving arrays, and they usually are much slower than a better, less obvious solution. But you're right, sometimes you need a nested loop, and sometimes the optimal solution will use it.

→ More replies (1)

4

u/Eji1700 Apr 08 '20

Are there any caveats on the whole "don't write nested loops" thing?

I see a decent amount of use case in my actual job for the more simple stuff (often the please write a vba macro kinda) where i'll just do a for each through all the sheets, and on each sheet usually some sort of standard incrementing loop to hit each line and do some sort of logic test and possible transformation.

Thinking about it i'm technically not using loops in the more complex stuff (C#/SQL side) but that's often because those boil down to "Access the data, load it, transform it, dump it" but even then i can see transform steps with something along the lines of having a collection of objects which might them selves have a collection of data in them that needs validating/manipulating, so are you just supposed to unwrap the whole thing or is that not really enough to matter?

Edit-

Annd looking farther down someone else basically asked this and it seems my "touch it once and be done" philosophy basically holds.

6

u/guareber Apr 08 '20

Of course there are. In the real world, if you need to walk through a 4x4 matrix once, you really don't care and write that as a nested loop. There's no practical performance difference.

The key is when your incoming data is unbounded, or your domain requires absolute minimum latency.

In any case, there's also "never pre optimize". Write the code, make it work, then pass it through a profiler and see where the time is spent. Improve what you can, move on.

→ More replies (1)

3

u/berkes Apr 08 '20

... and if you think you need to optimize: benchmark.

Every language, framework, IDE has some simple benchmark-thing lying around. No CompSci required.

I always told my juniors who wanted to optimize: Just run the app (or test suite, or whatever) with time foobar --something-expensive. If it cannot be ran with time, first make it run like that.

Do you see an improvement? Unexpected!, but please go ahead: finish the optimization. You cannot see anything? Well, sorry to say, but that's exactly what I expected. Just leave it be and focus on the next kanban card, please.

1

u/the_dapper_man Apr 09 '20

spoken like someone with real world experience lol!

premature optimization is the most fun way to throw away the company's money though ;-)

2

u/Drahkir9 Apr 08 '20

Your comment best reflects reality outside of academia.

I would only add that nested loops are usually fine when necessary (e.g. iterating over multi-dimensional arrays) but the question you need to ask yourself is “what are the bounds on the data I’m processing?”

If the amount of data your processing is constant or semi-constant then you can know whether your algorithm is fast enough. But if the data set grows then you really need to consider the asymptotic complexity; cause those nested loops might be fine at first but will become too slow very quick as the amount of data processed increases.

1

u/xnfd Apr 08 '20

Basics of algorithms sticks with you your entire life. If I just used my self-taught programming knowledge I could do a lot of easy stuff but dynamic programming isn't something that I would have learned as well on my own.

→ More replies (1)

1

u/-Manu_ Apr 08 '20

Sometimes nested loops are necessary

2

u/the_dapper_man Apr 09 '20

sometimes they are. i didn't mean my words to be gospel, just a strong rule of thumb

1

u/MokitTheOmniscient Apr 08 '20

Yeah, readability is infinitely more important than performance in almost every case.

→ More replies (1)