A collection of Bad Code Smells in a Catalog form for Developers & Researchers. Code Smell is a typical bad code implementation, and learning these concepts immiedietly makes you a better developer!
Also, have another look at that example code snippet though: that static variable is local to that function. It’s a weird feature in c.
I’ve used it quite often in embedded code where a single variable was only for one function, and only for that one app/device. Wrapping it in a struct would’ve made the code needlessly more complex (that’s a code smell). And yet, these static locals are very easy to refactor to one local to a struct. May the situation change, that’s still an option.
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!
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.
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.
Definitely another code smell!
https://luzkan.github.io/smells/global-data
In most cases it’s a bad idea, yes.
Also, have another look at that example code snippet though: that static variable is local to that function. It’s a weird feature in c.
I’ve used it quite often in embedded code where a single variable was only for one function, and only for that one app/device. Wrapping it in a struct would’ve made the code needlessly more complex (that’s a code smell). And yet, these static locals are very easy to refactor to one local to a struct. May the situation change, that’s still an option.
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!
Aaand… you didn’t even bother to google it :/
This is not about storage durations, and it’s local to a function
I don’t need to Google anything. I have 30 years experience writing C & C++.
Yes it is.
https://en.cppreference.com/w/c/language/storage_duration
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.
Again, it’s an easy refactor to make it not global. There are cases where that extra abstraction work simply does not add value.
With your background, you should know that