this post was submitted on 05 Jan 2025
60 points (90.5% liked)

Programming

17764 readers
544 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] FizzyOrange 1 points 14 hours ago (1 children)

that static variable is local to that function

Yes I know how static storage durations work. It's still global state, which is a code smell. Actually I'd go as far as to say global state is just bad practice, not just a smell. Occasionally it's the only option, and it's definitely the lazy option which I won't claim to never take!

[–] [email protected] 1 points 13 hours ago (1 children)

Aaand.. you didn't even bother to google it :/

This is not about storage durations, and it's local to a function

[–] FizzyOrange 1 points 3 hours ago

I don't need to Google anything. I have 30 years experience writing C & C++.

This is not about storage durations

Yes it is.

https://en.cppreference.com/w/c/language/storage_duration

it’s local to a function

Only the visibility is local. The data is still global state. You can call that function from anywhere and it will use the same state. That's what global state means.

https://softwareengineering.stackexchange.com/a/314983

Some of the biggest issues with global state are that is makes testing difficult and it makes concurrent code more error-prone. Both of those are still true for locally scoped static variables.