Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Code Smells #1603

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open

Refactor Code Smells #1603

wants to merge 6 commits into from

Conversation

elanonc
Copy link

@elanonc elanonc commented Nov 29, 2023

PR for college assignment. We refactored the Any Type smells, Enum Implicit Values, Missing Union Type, and Large Component.

@brusherru
Copy link
Member

@elanonc thank you and welcome!
I love your job regarding getting rid of anys and moving stuff around the transactions component! ❤️

However, I'm not sure about two other commits. Could you explain to me why you think it is better to have:

  • enums explicitly bound to specific numbers,
  • extracted union types that are used in the single place of the function?

Comment on lines +111 to +113
type GoNextType = string | undefined;

const goNext = (genesisID: GoNextType) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be just

Suggested change
type GoNextType = string | undefined;
const goNext = (genesisID: GoNextType) => {
const goNext = (genesisID?: HexString) => {

What is the purpose of extracting it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a tool that my teacher recommended for detecting code smells. One of these smells was 'Missing Union Type Abstractions' which suggests using type aliases to avoid the repetition of union types.

The tool is called ReactSniffer and is still in development (it's part of a research project at my university). If you have any observations about this code smell, please feel free to share them in the comments, and I'll pass them on to the responsible parties.

Link: https://github.com/maykongsn/react-typescript-code-smells

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in case these types will be used (or at least may be used or it is something tricky) — it is fine.
But most of the extracted union types look silly: they would not be used more than once or it is a simple union of primitives — I don't see any benefits.

For example, it is good to have:
type HexOrBytes = Uint8Array | HexString
which also may have a guard, some utils (e.g. toHex or toBytes), may be reused, and so on

But having NumberOrString instead of number | string in some specific function argument — looks silly.
And especially type WeirdName = string | undefined for an argument of the function that accepts optional string +)

I believe that no tool can determine what exactly you need in which case (at least without some well-trained AI model), so it is good to have automatic suggestions, but afterward, the coder should make a decision on what to replace, and what to keep...


Let's keep commits that fix Anys and refactoring of transactions components.

@@ -1,4 +1,5 @@
import React, { useState, useRef, useEffect } from 'react';
// import styled from 'styled-components';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the trash code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make the corrections and resend. Thank you for the feedback.

@brusherru
Copy link
Member

@elanonc could you keep only these two commits:

And omit the other two?
I will merge it 🙏

@elanonc
Copy link
Author

elanonc commented Dec 16, 2023

I had an issue with my computer, so I couldn't make the commit changes before. But finally i did it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants