How SDET smells code… an Introduction

Lalitha
3 min readOct 5, 2020

Sometimes a new pair of glasses is needed to spot the problem — Martin Fowler

Photo by Andriyko Podilnyk on Unsplash

Before heading how SDET smells code, let us understand what are the roles and responsibilities of SDET first.

SDET is a person who is involved in every step of the software development process, such as designing, development, and testing.

SDETs should be able to produce high-performance code, setting up a testing framework as well as Test automaton.

So, as a part of the code review process, automatic code review tools/ utilities are used to detect bugs, vulnerabilities, and code smells in code.

What is Code smell…?

Code smells are not actually bugs or errors.

Code Smells are signals that your code should be refactored to improve extendability, readability & supportability.

Once code smells are located, they can be removed by code refactoring techniques.

Some of the bad smells in code are,

  • Mysterious name
  • Duplicated code
  • Long function
  • Long parameter list
  • Global data

Now let us see what refactoring is…

Refactoring :

Code refactoring is the process of changing a program’s source code without modifying its external functional behavior, in order to improve some of the nonfunctional attributes of the software to improves its internal structure. Testing plays an important role in refactoring. so what we do here, is to improve the design of the code after it is written.

Refactoring is similar to performance optimization. Both these concepts are related to each other because they involve code changes/manipulations without changing the program’s overall functionality.

The purpose of refactoring is to make the code easier to understand. The key to keeping the code readable and modifiable is refactoring for frameworks in general but also in software.

Code refactoring should be done as a series of small changes, each of which makes the existing code slightly better while still leaving the program in working order. Don’t mix a whole bunch of refactorings into one big change.

Benefits of refactoring:

  • Improves design of software
  • Makes software easier to understand
  • Helps to find bugs
  • Helps to program faster
  • Can see the whole picture
  • Reduce complexity

Two Hat Metaphor -

When using refactoring for developing software, two activities performed are “Adding Functionality “ and “Refactoring.

Adding Functionality — Here existing code is not modified but new capabilities are added. It is also called a developer hat.

Refactor — Here new functionality is not added, but we restructure code. This is also called a refactoring hat.

So, here programmer swaps hats and refactors for a while. Then he swaps hat again and adds the functionality.

Some of the Code Refactoring Techniques :

  • Red-green refactoring
  • Preparatory refactoring — is making it easier to add a feature.
  • Comprehensive refracting — making code easier to understand.
  • Litter pickup refactoring.
  • Planned and opportunistic refracting.
  • Long Term refactoring.
  • Refactoring in code review

Some of the refactoring tools/utilities in python -

SonarQube:

  • Automatic code review tool to detect bugs and code smell in your code.
  • It is an open-source platform, which can inspect the code quality to perform automatic reviews with static analysis of code to detect bugs, code smells.

More details about this tool can be found here — https://docs.sonarqube.org

Rope:

  • Free Python utility for refactoring Python tool.
  • Comes with an extensive set of APIs for refactoring and renaming components in Python codebase.

Using PyCharm:

  • From the main menu, choose Refactor | Refactor This, or press Ctrl+Alt+Shift+T, and then select the desired refactoring from the popup.

Book references,

https://books.google.com/books?hl=en&lr=&id=2H1_DwAAQBAJ&oi=fnd&pg=PT14&ots=Ngwryaq0R1&sig=z8rVdRqlD9T6XzlhlDikvTL_CZ0#v=onepage&q&f=false

Thanks for reading…!!

Originally published at https://www.numpyninja.com on October 5, 2020.

--

--