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

Show parent comments

3

u/[deleted] Apr 05 '22

[removed] — view removed comment

12

u/TheEpicSock Apr 05 '22

In languages like Java where everything is an object, you straight up can’t do the second one.

5

u/plastikmissile Apr 05 '22

The second one is local to that bit of code. If you want to use it somewhere else then you'll want to have it in a class so everybody can have access to it.

2

u/[deleted] Apr 05 '22

[removed] — view removed comment

2

u/plastikmissile Apr 05 '22

Oh I think I misunderstood your question. So basically, if I understand your question correctly, it's whether you should have your method in a class or in a module. As far as I know they are functionally the same. Whether you want your method to be a static one in a class depends on if it makes sense for it to be part of the class.

1

u/rasmustrew Apr 05 '22

I'm not sure that there are any programming languages that allow both of those, but if there are i would certainly prefer the second form, it reads much more clearly.

1

u/[deleted] Apr 05 '22

[removed] — view removed comment

1

u/rasmustrew Apr 06 '22

Fair enough, I actually use Rust quite a bit so I should have known better!

1

u/FountainsOfFluids Apr 05 '22

Setting aside the fact that some programming languages won't let you do the function version, the static method is simply a convenient way of keeping the code organized. It makes sense to group math tools together under the class Math.

3

u/[deleted] Apr 05 '22

[removed] — view removed comment

1

u/FountainsOfFluids Apr 05 '22

I haven't worked with namespaces since college.

Can you import a namespace to get all the tools gathered in it?

1

u/[deleted] Apr 06 '22

[removed] — view removed comment

1

u/FountainsOfFluids Apr 06 '22

That's cool.

I'm accustomed to static methods coming with packages that are also used as proper classes. The static methods are tools that are either needed or just helpful.

For example, in Mongoose each object is a data record, and you can execute methods on a record that affects only that record.

Static methods affect all data records. So if you were searching for all records that matched a certain term, you'd use the static method for querying.