Refactoring and Code smells

What is refactoring?

Amr elshaer
5 min readMay 1, 2022

A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its behavior

Why do it?

  1. To Keep Your Code Clean
  2. To Improve The Performance of The Application
  3. You Save Time and Money in The Future
  4. To Reduce the Technical Debt
  5. Your Code is Outdated
  6. Makes Bugs Easier to Be Found
  7. Improves the System Design

When Should you refactor?

1- After writing a unit test (TDD) Write a test, Make it run, and Change the code to make it right i.e. Refactor.

2- As part of fixing a bug

3-As part of a code review

When not to refactor?

1- Current Code doesn’t work

2- Deadline

“Other than when you are very close to a deadline, however, you should not put off refactoring because you haven’t got time. Experience with several projects has shown that a bout of refactoring results in increased productivity. Not having enough time usually is a sign that you need to do some refactoring.”

Martin Fowler

The refactoring process?

1- commit(or back up) current working code

2- verify existing behavior (ideally with automated tests)

3- Apply a Refactoring

4- Confirm original behavior has been preserved

What is Code smell?

is not a bug but it is a sign of bad construction of your program.

Classifications of code smell

Bloaters

Make codebase bigger than necessary,usually impact code slowly over time

Object-Orientation Abusers

Break polymorphism, create inappropriate tight coupling

Change Preventers

if you need to change something in one place in your code, you have to make many changes in other places too.

Dispensables

is something pointless and unneeded whose absence would make the code cleaner,provide little or no value,can be safely removed with little/no effort.

Couplers

All the smells in this group contribute to excessive coupling between classes,

Tie unrelated part of the system together.

Obfuscators

Confuse the reader ,hide intent,impede clear communication.

Steve smith aka ardalis

Statement code smells and refactorings

Statment Code smells

Smell: Primitive Obsession(Bloater)

Overuse of primitives, instead of better abstractions or data structures

What is problem with primitives ?

  • Difficult to clarify the intent of the code . You don’t Know what 7 is mean and 4.
  • They are very general you can pass any value.

Refactoring Statement Primitive Obsession

refactoring statement primitive obsession

Introduce Named Variable

Use Named Arguments

Replace Primitive with constant

Replace Primitive with Enums

Smell: Vertical Separation(Obfuscator)

Define, assign, and use variables and functions near where they are used, Avoid forcing the reader to scroll.

Smell: Inconsistency(Obfuscator)

Be consistent in your naming, formatting, and usage patterns within your application.

Smell: Poor Names(Obfuscator)

Naming things has often been cited as one of the hardest problems in computer science. Use descriptive names and avoid encodings where possible.

Ideal Naming Characteristics

Descriptive Names

😒😒
😊😊

Abstraction Level

In this example you expected that order get from file what if behavior change and order get from Database or anything else.

❤💕

Follow Standards and conventions

✔✔

Unambiguous

🤦‍♂️🤦‍♂️
😘😘

Long Names for Long Scopes (And vice versa)

😢🤢
🌹🌹

Avoid Encodings

🤢🤢
😘😘

Smell: Switch Statments(Object-Orientation Abuser)

switch statements, and complex if-else chains, may indicate a lack of proper use of object-oriented design.One switch in your codebase on particular value is probably fine,when you duplicate them that it’s a code smell

Smell: Duplicate Code(Disposable)

Duplication is the root of all software evil.Follow the Don’t Repeat yourself principle and avoid repetition in your code when possible.

Duplicate Code
Duplicate Code

Smell:Dead Code(Disposable)

Get rid of useless code that is never executed. it’s not adding value.it’s only adding weight to the codebase it’s a distraction.bury it.

Lower and next don’t use in the code visual studio can help you to discover dead code

Smell: Hidden Temporal Coupling(coupler)

Certain operations must be called in a certain sequence, or the won’t work.

imagine you prepare pizza some steps you must to follow to make pizza like this.

you can use Template Method Design Pattern

--

--

Amr elshaer
Amr elshaer

Written by Amr elshaer

Software engineer | .Net ,C# ,Angular, Javascript

Responses (5)