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

1

u/[deleted] Apr 05 '22 edited Apr 05 '22

So question about static objects/methods. I read that you should try not to use them if you don’t need to. I have been struggling on whether or not to implement them.

For example, I have a FileHandler class that deals with reading and writing to text files to deal with saved data. Is it better practice to always create a new object whenever these tasks are needed and set up the object in it’s constructor, or just make the object or methods static so they can always be called upon elsewhere in the program?

3

u/plastikmissile Apr 05 '22

This will be a matter of opinion, but generally speaking I use static whenever a method is something of a "utility" function that I want to use outside of the context of the class hosting it. So a bunch of math functions would be a perfect candidate for static.

As for the file handler, generally speaking you want to spend as less time as you can doing I/O since it's a bottleneck. That means grabbing resources when needed and immediately releasing them when finished. So if the method does all of this inside of it, without needing any outside resources, then yeah you can make it static.

1

u/Le_9k_Redditor Apr 05 '22

I never use static classes, as it's such a nightmare when you end up a few years down the line and realise the class now needs a dependency injected and has to be converted to instanciated only uses