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

42

u/nutterontheloose Apr 05 '22

I understand the usefulness of overloading, but static functions... I just make it static when the IDE tells me to without really understanding why. So, thank you, you taught me something today.

21

u/EvolvedCookies Apr 05 '22

If its called on an object of that class it should not be static. Of the method has nothing to do with the class object then it should be static.

8

u/CozyRedBear Apr 05 '22

Static functions and static variables, when made public, are granted a sort of super-scope. They're decentralized. They transcend attachment to any individual objects instances. They effectively become a shared variable, without ownership to a specific object instance like how it is with most variables and functions. You can create 20 object instances and data can vary among them, but a static variable is the same across all of them. Static functions work similarly in that they can execute code without having to go through an object reference.

I use public static variables often when creating singletons and collections in game design. Every time I spawn a projectile, I add it to a static list so I can iterate over them as needed.

There exist use-cases for protected or private static variables, but it's harder to give simple examples of why you'd want to do that.

3

u/Fear_UnOwn Apr 06 '22

Pretty quick use case for a private static variable, a getter for a static variable!

6

u/Longjumping_Round_46 Apr 05 '22

Without static functions, (in C# atleast) to use a function you have to instantiate a class.

Car myCar = new Car("Honda", "Jazz", 2014);

myCar.driveSomewhere();

But with static functions, you don't need to instantiate a class. This can be used very well in say, logging.

Car myCar = new Car("Honda", "Jazz", 2014);

myCar.driveSomewhere();

CarLogger.LogMiles(myCar, 100);

You don't need to do this:

CarLogger logger = new CarLogger();

logger.LogMiles(myCar, 100);

Anywhere in your code you can access that class, you can access that function without creating a new instance of that class.

If you understand the concept, this static functions/classes can be a very useful too, for logging, checking something, hashing, etc.

4

u/SurfingOnNapras Apr 05 '22 edited Apr 05 '22

Static means that something doesn’t change regardless of where or in what context an object is created.

Static functions, therefore take up less space because your computer wouldn’t have to allocate new memory for every instance of Character to have a getName() or getHealth() or getAttack(). They can just all call the class defined getName() on its own properties. Hopefully this example highlights a real performance benefit in where many things of class NPC or Players that extend Character need to be instantiated.

Static methods are also protected from being overridden, which can make things generally less buggy.

3

u/TheTomato2 Apr 05 '22

People are just answering without talking language semantics lol. Static depends on the language. In C++ it means like 5 different things. However for most OOP languages static members in classes make that a global thing. So say you have a static int in class foo, that means that int independent of any instantiated foo. It shouldn't be very hard to connect the dots on why that is useful.

1

u/kneeonball Apr 07 '22

Just gotta be careful not to use it on a class and method that reads the state of the object (like a property or field on the class). Had a team at an old company I worked at make something static and all of a sudden users started seeing other peoples bank account info.

People requesting the same data through that class and function at the same time on the web page had a chance of getting other data since it wasn’t working off a specific instance of the class related to that user, but was constantly getting overridden with new data every time a user went to the page.