r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

14

u/Sedowa Apr 05 '22

I'm reading through stuff about virtual functions and class pointers to them and I've found I just don't understand why it has to be so complicated. Seems it would be a lot easier to just avoid using them and code around it but maybe there's a performance reason for it.

Same with lambda functions. It was explained to me as being a function that just gets run once so doesn't need a fancy definition but seems to mostly just make readability harder for not a lot of gain. Once again this could just be a performance thing and maybe it helps with larger applications rather than the smaller ones used in teaching material.

It's hard to wrap my mind around the point of it all.

14

u/[deleted] Apr 05 '22

[removed] — view removed comment

5

u/Sedowa Apr 05 '22

I hadn't even thought about using a lambda as a parameter. That actually makes a lot of sense.

Still working through the section for class inheritence so hopefully by the end it makes more sense what to use and where. I suppose I do get it but it's gonna be hard to remember all the rules.

5

u/faceplanted Apr 05 '22

Parameters are I think what they're most used for by far.

1

u/ExtraFig6 Apr 05 '22

C++?

1

u/Sedowa Apr 06 '22

Yup. I have no idea how it differs from language to language. The little experience I had with other languages never got far enough to get into these.

1

u/ExtraFig6 Apr 06 '22

Did you do function pointers?

1

u/Sedowa Apr 06 '22

Of course. It's not the pointers themselves that I'm having issues with. It's inheritance as a whole. Seeing the big picture and remembering all the little details and how it all connects. It just seems overly complex. Probably for a reason but still.

1

u/__Fred Apr 06 '22

Use case for lambda functions:

Imagine you have a function "sortBy(list, comparison_function)".

sortBy([32,5,3,2,4,6], smallerThan) would return [2,3,4,5,6,32]

Now, let's say in your program you need to sort things often, but you need different comparison functions, and you only need each comparison function once, and nowhere else.

highscore = sortBy(list_of_some_scores, (entry1, entry2) => {
    entry1.points > entry2.points || entry1.points === entry2.points && entry1.goals > entry2.goals
});

(JavaScript syntax)

Okay... It's kind of a long line. The alternative would be to define a function and call it "higherPointsOrHigherGoals(entry1, entry2)".


Other usecase:

You have a function that applies another function to each node in a tree.

treeMap(myTree, twice) replaces each value in myTree with twice(value).

Now I want to do something else with the values, like sorrounding each string with angle brackets. I could define a named function or I could define a lambda function:

treeMap(myTree, value => "<" + value + ">");

In some languages, that might look more verbose.

I think it's also nice that you write the definition exactly in the place where you use it. If you defined a function "sorroundWithAngleBrackets", you would have to search for where it's used at the place you defined it and you would have to search for where it's defined, if you are at the place you apply it.