What is Code Smell?

Asked

Viewed 11,214 times

23

I started to study swift and I stumbled across a widely used term called Code Smell from what I could understand in Wikipedia and on some other sites it is any symptom in the source code that indicates a deeper problem, e.g: duplicate code.

By this definition I end up questioning the following thing, Code Smell is not the same thing as the principles of DRY, KISS and YAGNI?

How can I identify code smell/bad smell in a code?

  • 1

    I thought it was something related to technical debt, but I’m not sure.

  • @Math is there in the Wikipedia article, "Code Smells are an Important Reason for Technical Debt."

  • @Piovezan is right, I did not read the page, but I knew it had something to do :P

  • @Math I had never heard of technical debt, I think there are so many factors that should be considered when it comes to seeing if a code can be refactored or not that make me more confused, I end up remembering an old phrase of the programmers "If this works,."

  • 8

    Do you know the expression "where there is smoke there is fire"? The meaning of this is, good, not always smoke means fire, but it’s a good sign, so better stay alert... From the form table, in my understanding, "code Smell" has the sense to indicate things that are not "wrong" in the code, but that may indicate a deeper problem. Sometimes the programmer deviates from "good practice" for good reason. But most of the time, it does it out of ignorance, so it is good to be aware of the possible consequences (review the code), because it may not be broken now but has a good chance of breaking in the future.

  • @mgibsonbr Very well spoken.

  • In good English, code Smell is the famous "gambiarra".

Show 2 more comments

3 answers

12


Determining what is or is not a code Smell is always a subjective judgment, and will always vary according to the programming language, developer and development methodology.

There are tools that detect certain types of Smells code, such as:

Java: Checkstyle, PMD and Findbugs.

.Net: Resharper.

To Php.

Some common Code Smells:

Duplicate code: identical or very similar code exists in more than one location.

Long method: a very extensive method, function or process.

Extended class: a class that ended up getting very extensive (God Object).

Feature Envy (no translation): a class that overuses methods of another class.

Inappropriate intimacy: a class that has dependency on implementation details from another class.

Refused legacy: a class overriding (override) the generic class method so that the generic class contract is not fulfilled by the derived class.

Lazy class: class that does very little.

Artificial complexity: forced use of extremely complicated Patterns design, where a simple design would suffice.

Excessively long identifiers: in particular, the use of naming conventions to avoid ambiguities, which should be implicit in the software architecture.

8

They are not the same thing. Many code Smells (bad smell in code) will follow these principles of DRY, KISS and YAGNI, which are prevalent in software development, but not all will (examples: Excessively short Identifiers, Constants class).

But regardless. If smelly situations were obvious to everyone, you wouldn’t need to compile a list of code Smells first. Suffice it to say "always follow principles such as DRY, KISS and YAGNI".

As has been said there are tools that detect some code Smells, but the most complete way to know if your code has code Smells is to get a good list of code Smells (have one for example in Robert C. Martin’s "Clean Code"), study them and see if they exist in his code. If all were obvious and you avoided all in your code, congratulations! You are very good at applying the principles of DRY, KISS and YAGNI, among others.

Otherwise, you knew better the code Smells and the principles behind them and with that you will know to be more attentive to code with bad smell, being able to apply this in your existing code and in future codes. This will help you to have more robust code and following good principles.

6

code Smell/bad Smell, is the term used to show that something in the code does not make sense (lack of cohesion), if this problem is not treated will spread, the cost and effort to correct it will increase. Some examples are classes with more than one reponsability and lack of modularization.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.