57
Code Smells Catalog (luzkan.github.io)
top 27 comments
sorted by: hot top controversial new old
[-] 1hitsong@lemmy.ml 23 points 1 day ago

I've been rallying against clever code for years!

Sure, it makes you have less lines for your l33t code solutions, but in the real world, it sacrifices the maintainability of code that others will eventually work on.

Between a clever 1 line fix and maintainable 10 line fix, I'll choose the 10 line every time.

[-] MonkderVierte@lemmy.ml 3 points 1 day ago

10 lines is a bit much, that's hardly more readable than one.

Then again, it depends on the language.

[-] curbstickle@lemmy.dbzer0.com 8 points 1 day ago

As an extensive commenter, I completely agree.

I need to know wtf I was doing, making it convoluted to save a few lines is pointless.

[-] pcouy@lemmy.pierre-couy.fr 5 points 1 day ago

It's often a good idea to make the code itself very explicit through verbose function and variable names, rather than writing comments that could lead to inconsistencies between code and comments (by not updating the comments at the same time as the code) (see "Fallacious Comments" from the catalog)

[-] curbstickle@lemmy.dbzer0.com 4 points 1 day ago

"Some people do a bad job commenting and updating comments, so lets not do comments" is not an approach that works for me.

Most of my code is at the prototype level. I'm concepting something out, usually paired with hardware.

If someone can't follow what I'm doing, its going to lead to problems. If a change happens to the hardware being controlled, code will not be good enough on its own.

Rather than being accepting of bad commenting practice, make comments (and updating them properly) part of good practice. In my experience, It saves time in the long run and leads to better code at the end.

[-] pcouy@lemmy.pierre-couy.fr 5 points 16 hours ago

That's not what I said. I said that comments can often (but not always) be replaced with good and explicit names.

This can be pushed to some extreme by making functions that only get called at a single place in the code, just for the sake of being able to give a name to the code that's inside (instead of inlining it and adding a comment that conveys the same informations as the function's signature)

It's definetly not for everyone, but for beginners/juniors it gives something objective they can aim for when trying to build good coding habits

[-] curbstickle@lemmy.dbzer0.com 3 points 9 hours ago* (last edited 9 hours ago)

I am going to disagree, comments should be an explanation.

The code is what's being done, a comment should be why its being done.

[-] pcouy@lemmy.pierre-couy.fr 4 points 4 hours ago* (last edited 4 hours ago)

I'm not sure how we disagree. At least, I don't disagree with you. My whole comment was talking about "what" comments. "Why" comments are a very good thing to have where they're needed

[-] curbstickle@lemmy.dbzer0.com -1 points 3 hours ago

Not updating comments with code is what I'm talking about - that's not a comment problem, thats a programmer problem.

If they aren't updating the "why", that programmer is the problem, not comments.

[-] pcouy@lemmy.pierre-couy.fr 3 points 3 hours ago

When refactoring, it's often the "what" that changes, not the "why"

[-] curbstickle@lemmy.dbzer0.com 1 points 2 hours ago* (last edited 1 hour ago)

That really depends.

Especially for a function that may see use in a variety of scenarios.

I'm going to be firmly against anyone suggesting against proper comments - which, I'm sorry, but you are by your own statement.

Code will change for many, many, many reasons beyond just refactoring.

Edit: and why it was refactored is important as well.

There are just so many reasons, and yes, I will continue to be against this newer trend of "dont comment, make codes your comments".

All that is, is a great way to make your code harder to manage later. It doesnt take much effort to explain why you're doing something.

[-] Boomkop3@reddthat.com 16 points 1 day ago* (last edited 1 day ago)

Some of these mostly just someone's opinion, and I don't quite agree with all of them

[-] FizzyOrange@programming.dev 2 points 4 hours ago

Any specific ones? I've seen this before and I thought I would feel the same way as you before I read them, but actually the vast majority are pretty basic things that are not really arguable.

It's definitely nice to have a list like this to point inexperienced colleagues to in code reviews. It's a bit more authoritative than "trust me bro, I've written a lot of code".

[-] Boomkop3@reddthat.com 1 points 1 hour ago

To preface, I think it's best to focus on what the right approaches are. Not on what to avoid. And when you see a student making a mistake, showing them how a different approach is handier (if possible) is what I suggest you do.

Having something to point at doesn't help much

vertical separation

This one argues against organizing your code in a way that shares variables are in one place. There are arguments to be made either way, but normally you'd scope your variables in a way that the ones specific to a particular bit of code are not accessible from elsewhere.

null check

Suggest writing a custom class to do what most languages can solve with inheritance or even better: the ? syntax.

inconsistent names / styles

Yes, it can be annoying. No, clarity is more important than insisting on removing that extra underscore.

complicated Boolean expression

They're advocating the use of a function to replace an expression. Sometimes this works, but the task of a boolean expression is not always easily expressed in a couple words. And so you can end up with misleading function names. Instead, just put a comment in the code.

callback hell

Not even a code smell. It's an issue from back when languages like JavaScript didn't support promises yet, but callbacks were popular. Cose got hard to read with a little complexities.

[-] SwordInStone@lemmy.world 1 points 1 hour ago

inheritance rarely solves anything

[-] Boomkop3@reddthat.com 1 points 59 minutes ago

You gotta know how to use it properly

[-] Michal@programming.dev 3 points 1 day ago
[-] Boomkop3@reddthat.com 2 points 1 day ago

And focussing on what not to do is not the best way to get things right

[-] onlinepersona@programming.dev 0 points 1 day ago

Agreed. Every time somebody links this to "prove" or underline their argument, I roll my eyes. There are a lot of subjective things there and many that are actually valid code.

Anti Commercial-AI license

[-] DrDeadCrash@programming.dev 9 points 1 day ago

I feel like if one tried to follow all of these "rules" at all times nothing would get done, at all.

[-] BradleyUffner@lemmy.world 11 points 1 day ago

A code smell isn't supposed to be automatically bad. A smell is an indication that something might be wrong. Sometimes using a smelly pattern is legitimately the only way to do something.

[-] pcouy@lemmy.pierre-couy.fr 5 points 1 day ago* (last edited 1 day ago)

Apart from the fact that, as another commenter said, "smells" are not "rules", I think most of these points come down to developing good habits, and ultimately save a lot of time in the long run by initially spending some time thinking about maintainability and preventing/limiting technical debt accumulation.

[-] morrowind@lemmy.ml 6 points 2 days ago

half of these will make your code better lmao

[-] SwordInStone@lemmy.world 9 points 2 days ago
[-] bhamlin@lemmy.world 12 points 2 days ago

I'll never tell

[-] shnizmuffin@lemmy.inbutts.lol 5 points 1 day ago

My code is exclusively Complicated Regular Expressions and it's screaming fast.

[-] morrowind@lemmy.ml 4 points 1 day ago

I'm not going through every one, but null checks, vertical separation, status variables and binary operator in name, are all things that often make your code better and more readable

this post was submitted on 05 Jan 2025
57 points (90.1% liked)

Programming

17748 readers
435 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 !webdev@programming.dev



founded 2 years ago
MODERATORS