Тёмный

Static Variables & Methods are Evil? 😈 

CodeOpinion
Подписаться 88 тыс.
Просмотров 8 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 70   
@sunefred
@sunefred 4 дня назад
Static _state_ is evil, or any state for that matter. Pure functions FTW. Its funny, I use static private methods a lot for this reason, to signal that they don't have side effects.
@RaVq91
@RaVq91 3 дня назад
Private static method can call public static causing some side-effects. Private static function can also do some in-place modifications on passed objects. I like the idea of methods clearly treated as doing no state transitions.
@sunefred
@sunefred 3 дня назад
@@RaVq91 yah, that’s why I find it funny. 😄 it’s a reasonable compromise between simplicity and correctness, for csharp .
@markovcd
@markovcd 3 дня назад
This kind of generalisation is simply wrong. You can't make any useful with just pure functions. You need to have some point of entry that has some form of state. I agree that we should strive to minimize state though.
@krumbergify
@krumbergify 3 дня назад
@@markovcdIsn’t the math library useful? Isn’t a JSON marshaller useful? There are many usecases for static functions. Constants are also great! As said in this thread, the only evil here is global state which should be replaced by dependency injection when possible.
@nickbarton3191
@nickbarton3191 3 дня назад
Every large system has state. The trick is a design to manage it and not to leak it into the core business logic. Declarative functional design is the way to go for the latter.
@buriedstpatrick2294
@buriedstpatrick2294 4 дня назад
One use I really like for static methods is to extract the pure helper functions in a piece of business logic and then run unit tests against those static functions. Reason being that, as long as they are pure, they are by far the easiest things to test. You don't need to set up any state, it's just basic input/output testing. If I then need to test the stateful parts of the code, I tend to favor integration tests.
@captainnoyaux
@captainnoyaux 4 дня назад
What you call integration are my unit tests. Unit testing as in behaviour level if much more maintainable and efficient to do than technical unit test or implementation detail unit tests
@buriedstpatrick2294
@buriedstpatrick2294 4 дня назад
@@captainnoyaux That depends wildly on your tooling and stack. My integration tests typically just involve a test-container for my database. Sometimes there's a message queue involved as well. Quite maintainable. Avoids mocks.
@nickbarton3191
@nickbarton3191 3 дня назад
AKA extension methods
@captainnoyaux
@captainnoyaux 3 дня назад
for me it's more dependent on architecture, I use clean archi / hexagonal architecture and *my* unit tests are use case levels to summarize.
@nickbarton3191
@nickbarton3191 3 дня назад
@@captainnoyaux OK but there are many kinds of unit testing, not all of them are about use cases.
@pompiuses
@pompiuses 3 дня назад
Adopt functional programming with immutable state and pure functions. Then almost all functions can be static.
@CodeOpinion
@CodeOpinion 3 дня назад
When thinking about this video before I created it, I was thinking about that exactly.
@codewkarim
@codewkarim 4 дня назад
Horror story where I am the authro, when I was in the game development industry the singleton pattern was a big thing, GameManager, LevelManager, *Manager where al singletons, in they were implemented as static type that holds an single instance inside (so, it has a global state). It was really an unpleasant experience, changing one line breaks everything, global state was hard to manage. Luckily (or not..) we had no tests (which I think is still very common in game dev) so determinism wasn't a big of an issue. With time the singleton just became a god class that has everything and referenced from everywhere!!!
@imadetheuniverse4fun
@imadetheuniverse4fun 3 дня назад
i'm always curious about these stories from game dev. everyone always hates on singletons, global state, or god classes for game dev, but literally no one ever shows a full blown non-demo proper game without.
@CodeOpinion
@CodeOpinion 3 дня назад
Ah yes, the "Manger" static class. Been there, done that.
@DavidSmith-ef4eh
@DavidSmith-ef4eh 3 дня назад
I use static for pure function that are grouped in a utility class.. or for global state that doesn't change, which is basically global config, then.
@CodeOpinion
@CodeOpinion 3 дня назад
Ya, I totally wanted to bring config that's static and totally forgot. Good point.
@stefan-d.grigorescu
@stefan-d.grigorescu 3 дня назад
​@@CodeOpinionWhat do you think of (custom) smart enums? I personally enjoy very much using them, since they are quite versatile and simplify my code a lot. Do you think they might have any drawbacks though?
@andrewackroyd6136
@andrewackroyd6136 3 дня назад
Horror story working on a web app which has some of the legacy desktop functions hoisted up to a web service. Now we have multiple web session threads running through the legacy code which was never tested to be thread-safe. Static object variables (at class level) are being changed by parallel web sessions leading to intermittent race condition bugs. Only option is to sync lock the public methods causing scalability issues. So static state can be an issue in multi-threading situations. My understanding of static methods is all threads which pass through the method have their own copy of the local variables declared within the method...but the state of static variables declared on the class is shared, leading to such issues.
@CheefCoach
@CheefCoach 22 часа назад
If function is pure, there is no problem being static. If function isn't pure, that can be problem on itself.
@birdasaurusrex
@birdasaurusrex 3 дня назад
I have seen two critical issues in the last few months which were a result of improper use of static variables. Despite that, I totally agree with the nuanced take that they are perfectly fine when used appropriately.
@roeiohayon4501
@roeiohayon4501 4 дня назад
Something to be aware of when you're using static stuff is that you are creating a dependency to that static object without the advantage of using dependency injection.
@PelFox
@PelFox 3 дня назад
Which is often fine if you use static for utility classes and extension methods.
@roeiohayon4501
@roeiohayon4501 3 дня назад
@PelFox Yes, exactly!
@CodeOpinion
@CodeOpinion 3 дня назад
Coupling in one of the highest forms.
@margosdesarian
@margosdesarian 4 дня назад
I built quite a few websites with a severe misuse of static objects in the early 2000's, and apart from two or three occasions - i got away with it! Long live the internet :)
@CodeOpinion
@CodeOpinion 3 дня назад
ha! You and me both. Thanks for sharing.
@holger_p
@holger_p 3 дня назад
This is learning by try&error. The good thing is, you don't just learn how to do things right, but also what happens if you do it wrong.
@Soupflakez
@Soupflakez 2 дня назад
Statics can be really great! Especially if you're maintaining old framework projects without DI
@Zeero3846
@Zeero3846 3 дня назад
I fall under the mentality that non constant static variables should be configurable by some file or environment variable eventually, if not immediately.
@PelFox
@PelFox 3 дня назад
I use static all the time for stateless utility methods and validators.
@CodeOpinion
@CodeOpinion 3 дня назад
Yes, I use them quite a bit often because they are supplied everything they need via params.
@Bosslogq
@Bosslogq 3 дня назад
In the Globals Cache example, does it really matter that it is static or not? If for example we inject non static dependency via constructor (as I guess that would be alternative method) we still have the problem that we cannot be sure what is the state 😅 isn’t it more about knowing how the dependency works under the hood?
@JohnDoe-bu3qp
@JohnDoe-bu3qp 3 дня назад
It's not a problem until you want to enhance it or change it. If you want to have a decorator, for example, that will be invisible if you're using dependency injection but will require refactoring all usages for the static call.
@Bosslogq
@Bosslogq 3 дня назад
@@JohnDoe-bu3qp thanks. This definitely makes it in favour of NOT using static global.
@z0nx
@z0nx 3 дня назад
I wish it wasn't so "hard" to create pure static functions in c#, as it's not the default. But thinking about it, functions just taking parameters is in and of itself simpler than a class getting the parameter injected, possibly sharing that parameter over different methods. Then because classes are used, mutable state is just around the corner. Really miss the simplicity when comparing it against languages that don't force you into classes, like typescript, f#, rust, haskell etc
@CodeOpinion
@CodeOpinion 2 дня назад
Agree
@saniancreations
@saniancreations 3 дня назад
This "static is bad" mentality makes me so angry. Static variables are globals, okay you don't like those, fine. But static methods? Static methods are just functions. *Every* piece of functionality you have should be a static method (function!!!) until you have a *good reason* to attach it to an object. Attaching behaviour to a class before you have a reason to leads to some of the worst designs I've ever had to deal with, like objects with no member variables that you for some reason need to instantiate and allocate just so that you can do a simple calculation (0:13 is a nice example of such a useless object), or member methods that do not need to be member methods. People! Member methods have an invisible 'this' parameter! If you are not using that, THEN DON'T MAKE THAT A MEMBER METHOD, MAKE IT STATIC FFS. You will guaranteed want to use that functionality in a situation where you do not have access to the object that you made it a member method of. Why? BECAUSE THE OBJECT YOU ATTACHED IT TO PLAYS NO ROLE IN WHAT THAT METHOD DOES.
@TomEugelink
@TomEugelink 3 дня назад
You can just as easily have none deterministic / thread unsafe instance methods? IMHO static variables are foremost a potential memory leak, and static methods difficult to mock.
@gardnerjens
@gardnerjens 4 дня назад
well by this logic then extension methods are banned. Agree it depends on the context
@madsxcva
@madsxcva 4 дня назад
extension methods are just sugar syntax that are more similar to Partial implementations than Singletons and static methods/members. When you call an extension method you call it on an instance of the class you "extend" and handle instance data, not global data (unless you use other global resources)
@georgehelyar
@georgehelyar 3 дня назад
​​@@madsxcva extension methods are just static methods with syntactic sugar. You can call them just like normal static methods. There's nothing stopping you from mutating static state in them etc. You just have to be aware that users will not be able to mock it out (without monkeypatching) so be careful what you do in it.
@adambickford8720
@adambickford8720 4 дня назад
Static variables are bad 99% of the time. Fact. Static methods are fine as long as they are pure (i.e. functions) and are a natural fit for FP. In oop methods inherently take every piece of state in scope of the object as implicit arguments. This means there's no real way to replace those arguments w/o inheritance, which conflicts with static. So yeah, they are kind of bad in this context.
@sanhomealex
@sanhomealex 3 дня назад
> Static variables are bad 99% of the time. Fact. How can you proof this statements? What are the criteria of their "badness" ? What the 99% facts?
@adambickford8720
@adambickford8720 3 дня назад
@@sanhomealex If you don't understand why shared mutable state, especially at a global scope, is bad, i'm not sure i can explain it in a comment.
@sanhomealex
@sanhomealex 3 дня назад
@@adambickford8720 but I would insist. Could you be so kind or prefer to be rude ?
@adambickford8720
@adambickford8720 3 дня назад
@@sanhomealex he showed an example in the video. The `now()` type functionality is shared global mutable state i.e. the system clock. It makes code unpredictable and difficult to understand. Google "are static variables bad" and read the first result. This is a well-accepted and supported position.
@CodeOpinion
@CodeOpinion 3 дня назад
I often use thread-safe static variables for in-memory caches, hence my example. Also for static config. They do have utility.
@DinHamburg
@DinHamburg 4 дня назад
it depends...
@mohmin8226
@mohmin8226 4 дня назад
Public static void Donald Trump
Далее
Microservices are Technical Debt
31:59
Просмотров 576 тыс.
ДУБАЙСКАЯ ШОКОЛАДКА 🍫
00:55
Просмотров 2,5 млн
Random Emoji Beatbox Challenge #beatbox #tiktok
00:47
Implementing JWT Authentication in ASP.NET Core
23:51
Is Hono the holy grail of web frameworks?
18:33
Просмотров 7 тыс.
Does Deno 2 really uncomplicate JavaScript?
8:55
Просмотров 435 тыс.
How is this Website so fast!?
13:39
Просмотров 646 тыс.
My top 5 most popular front-end tips
22:07
Просмотров 38 тыс.
STOP Using Classes In JavaScript | Prime Reacts
14:02
Просмотров 244 тыс.
Radical Simplicity
45:53
Просмотров 287 тыс.
Claude has taken control of my computer...
4:37
Просмотров 887 тыс.