60
Code Smells Catalog
(luzkan.github.io)
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
Follow the wormhole through a path of communities !webdev@programming.dev
Sounds like you agree with that one to me? I'm not sure I follow their arguments about regions there (I've never used regions), but the example of declaring a variable in a block way before it is every used is spot on. I've seen code written like that and 99% of the time it's a bad idea. I think a lot of it comes from people who learnt C where you have to do that (or maybe Javascript which has weird rules for
var
).Yeah I'll give you that one. They even suggest using
Optional
as a solution, which is what their "smelly" code did in the first place!Not sure what your point is here. Of course inconsistent naming is a code smell. Do you want inconsistent names?
Erm, yeah that's why this is a code smell. They aren't saying never have complex boolean expressions - just that if you do you'd better have a good reason because probably you'd be better off splitting it up into named parts.
Indeed, so now it is a code smell.
Fair enough, code from a different era smells different
That's not what I'm saying either. But I think this is to be judged on a case by case basis. And it can depend on your understanding of the context. I think there's simply too much nuance here to just say "this smells"
I disagree. I've seen very complex boolean expressions and they were clearly code smell. Sometimes acceptable but definitely a fertile area for refactoring.
Their example is crap to be fair - two comparisons is not complex.
If you do have a super complex boolean expression, I wouldn't call it a smell. It'll be much more obvious
Of course not. But in some (uncommon in my experience) cases method names can be unclear or just plain impractically long. In such cases, I would rather see an exception to the rule than having to rely on a comment to explain the name choice.
I had a great example a couple months back, but I can't remember it right now. But here's a (bad) example of such a situation.
An example of this could be a button that triggers a click. You might call it BtnClick. Then the click event for it could be BtnClickClick. In this case, I'd rather see BtnClick_click. Ugly? Yes. Bad example? yes. But the idea is that it's more clear that the _Click action is seperate from the name.
There is no rule. Smells are not rules.
They're arguing to do this:
rather than
A bad example of encapsulation would be:
Of course, there is nuance here. Is this class encapsulating enough that it's got a right to exist? That'll depend on the situation.
Also, c has local static variables. Depending on your use case, it might just be easier in c than in C# and similar.
And just in case you're still reading and curious:
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.