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

Refactoring: Enhancing code maintainability by reducing code smells. #1336

Open
wants to merge 32 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d2ffe8b
Add default constructor to WriteAccessAllowed (#1273)
aNNiMON Jan 22, 2024
ac32664
Update AllowedUpdates (#1276)
bryan-zhu-dev Jan 22, 2024
47be445
fix ReactionType validations (#1303)
fullmoonrising Jan 28, 2024
b0d3989
Update Using-Replies.md (#1282)
ChelouteVR Jan 28, 2024
870d1e5
Update MaybeInaccessibleMessage.java (#1310)
Chase22 Feb 2, 2024
6b50d68
Fixed incorrect Chat#profileBackgroundCustomEmojiId field type. Chang…
link1107 Feb 4, 2024
d615fbf
Missing fields and constructor update for API 7.0 (#1322)
rbondar Feb 17, 2024
ed75ff9
Add missing methods from API 6.6 (#1323)
aNNiMON Feb 17, 2024
0e96778
API 7.1
rubenlagus Feb 17, 2024
e344f9c
API 7.1
rubenlagus Feb 17, 2024
6406b1f
Test cases for ASDC assignment-1
Yukta026 Mar 1, 2024
11b5569
Implementation code smell - 1 - Refactored code for Magic number usin…
Yukta026 Mar 23, 2024
e80a519
Implementation code smell - 2 - Refactored code for Complex Method us…
Yukta026 Mar 23, 2024
3e8d8c7
Implementation code smell - 3 - Refactored code for Long Statement us…
Yukta026 Mar 23, 2024
aafebe6
Implementation code smell - 4 - Refactored test code for Magic number…
Yukta026 Mar 23, 2024
4344b84
Implementation code smell - 5 - Refactored test code for Empty Catch …
Yukta026 Mar 23, 2024
b9c94fd
Implementation code smell - 6 - Refactored test code for Long Identif…
Yukta026 Mar 23, 2024
3d3099b
Design code smell - 1 - Refactored code for Insufficient Modularizati…
Yukta026 Mar 23, 2024
848266c
Design code smell - 2 - Refactored code for Unnecessary Abstraction u…
Yukta026 Mar 23, 2024
240fb70
Design code smell - 3 - Refactored code for Cyclically-dependent Modu…
Yukta026 Mar 23, 2024
2b9aea9
Fix: merge conflict for story file fixed
Yukta026 Mar 26, 2024
5fba3b2
Removed/renamed files for design smell-1
Yukta026 Mar 29, 2024
e70155a
Reset code changed in implementation smell-1
Yukta026 Mar 29, 2024
1b6598a
Removed unnecessary comments from refactored code for implementation …
Yukta026 Mar 29, 2024
423ceca
Made necessary changes for refactoring implementation smell-3
Yukta026 Mar 29, 2024
3729e7b
Reset code changed for refactoring implementation smell-4
Yukta026 Mar 29, 2024
fb128ba
Removed unncessary comments
Yukta026 Mar 29, 2024
f4cef77
Removed unnecessary comments
Yukta026 Mar 29, 2024
3c98bfa
Removed unnecessary comments
Yukta026 Mar 29, 2024
5b97c30
Removed unnecessary comments
Yukta026 Mar 29, 2024
6451919
Code reset for story and chat files
Yukta026 Mar 29, 2024
1278586
Design code smell - 4 - Refactored code for unexploited encapsulation…
Yukta026 Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.9.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -135,6 +140,11 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version> <!-- added dependency for refactoring empty catch clause code smell-->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -218,6 +228,25 @@
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions telegrambots-abilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
</properties>

<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version> <!-- Added for refactoring -->
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static java.lang.String.format;
import static java.util.Objects.hash;
import static java.util.Optional.ofNullable;
import static org.telegram.abilitybots.api.util.AbilityUtils.isValidCommandName;
import static org.telegram.abilitybots.api.util.AbilityUtils.isValCmdName;

/**
* An ability is a fully-fledged bot action that contains all the necessary information to process:
Expand Down Expand Up @@ -50,10 +50,18 @@ public final class Ability {
private final List<Reply> replies;
private final List<Predicate<Update>> flags;


@SafeVarargs
private Ability(String name, String info, Locality locality, Privacy privacy, int argNum, boolean statsEnabled, Consumer<MessageContext> action, Consumer<MessageContext> postAction, List<Reply> replies, Predicate<Update>... flags) {
checkArgument(isValidCommandName(name), "Method name can only contain alpha-numeric characters and underscores," +
" cannot be longer than 31 characters, empty or null", name);
// Refactored checkArgument to remove long statement code smell by renaming method name and introducing explaining variables.
// Explaining variables - INVALID_METHOD_NAME_ERROR and METHOD_NAME_CONSTRAINT
final String INVALID_METHOD_NAME_ERROR = "Method name can only contain alpha-numeric characters and underscores, " ;
final String METHOD_NAME_CONSTRAINT = "cannot be longer than 31 characters, and cannot be null";

// isValueCommandName renamed to isValCmdName
checkArgument(isValCmdName(name), INVALID_METHOD_NAME_ERROR+METHOD_NAME_CONSTRAINT, name);

Choose a reason for hiding this comment

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

in java, it's inconvenient to use abbreviations like here. use isValidCommandName



this.name = name;
this.info = info;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class MessageContext {
private final Update update;
private final BaseAbilityBot bot;

// Refactored code-Added thirdArg as explaining variable to remove magic number 2
int thirdArg = 2;

Choose a reason for hiding this comment

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

when did array index become a magic number?


private MessageContext(Update update, User user, Long chatId, BaseAbilityBot bot, String[] arguments) {
this.user = user;
this.chatId = chatId;
Expand Down Expand Up @@ -88,7 +91,9 @@ public String secondArg() {
*/
public String thirdArg() {
checkLength();
return arguments[2 % arguments.length];

// Replaced magic number 2 with thirdArg
return arguments[thirdArg % arguments.length];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public class DefaultSender implements MessageSender {

private DefaultAbsSender bot;

/*
* Refactored code - Insufficient Modularization design smell refactored by moving the methods related to send functionality
* and extracting class DefaultSender1.
*/

Choose a reason for hiding this comment

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

This makes more "code smell" by creating non-obvious DefaultSender1 class (how end-users should understand the difference?) why methods related to sending are moved from DefaultSender class?

public DefaultSender(DefaultAbsSender bot) {
this.bot = bot;
}
Expand Down Expand Up @@ -106,34 +110,4 @@ public void getWebhookInfoAsync(SentCallback<WebhookInfo> sentCallback) throws T
public Message sendDocument(SendDocument sendDocument) throws TelegramApiException {
return bot.execute(sendDocument);
}

@Override
public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
return bot.execute(sendPhoto);
}

@Override
public Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
return bot.execute(sendVideo);
}

@Override
public Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
return bot.execute(sendSticker);
}

@Override
public Message sendAudio(SendAudio sendAudio) throws TelegramApiException {
return bot.execute(sendAudio);
}

@Override
public Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
return bot.execute(sendVoice);
}

@Override
public Message sendVideoNote(SendVideoNote sendVideoNote) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.telegram.abilitybots.api.sender;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.*;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;

import java.io.Serializable;
public class DefaultSender1 implements MessageSender1{
private static final String TAG = MessageSender.class.getName();

private DefaultAbsSender bot;

@Override
public <T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void executeAsync(Method method, Callback callback) throws TelegramApiException {

}

@Override
public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) throws TelegramApiException {
return null;
}

@Override
public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
return bot.execute(sendPhoto);
}

@Override
public Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
return bot.execute(sendVideo);
}

@Override
public Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
return bot.execute(sendSticker);
}

@Override
public Message sendAudio(SendAudio sendAudio) throws TelegramApiException {
return bot.execute(sendAudio);
}

@Override
public Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
return bot.execute(sendVoice);
}
@Override
public Message sendVideoNote(SendVideoNote sendVideoNote) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,4 @@ public interface MessageSender {

Message sendDocument(SendDocument sendDocument) throws TelegramApiException;

Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException;

Message sendVideo(SendVideo sendVideo) throws TelegramApiException;

Message sendAudio(SendAudio sendAudio) throws TelegramApiException;

Message sendVoice(SendVoice sendVoice) throws TelegramApiException;

Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException;

Message sendSticker(SendSticker sendSticker) throws TelegramApiException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.telegram.abilitybots.api.sender;

Choose a reason for hiding this comment

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

See my comments about DefaultSender1



import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.*;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;

import java.io.Serializable;

/**
* A sender interface that replicates {@link DefaultAbsSender} methods.
*
* @author Abbas Abou Daya
*/
public interface MessageSender1 {
/*
* Refactored code - Interfaces related to send functionality.
*/
<T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void executeAsync(Method method, Callback callback) throws TelegramApiException;

<T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) throws TelegramApiException;


Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException;

Message sendVideo(SendVideo sendVideo) throws TelegramApiException;

Message sendAudio(SendAudio sendAudio) throws TelegramApiException;

Message sendVoice(SendVoice sendVoice) throws TelegramApiException;

Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException;

Message sendSticker(SendSticker sendSticker) throws TelegramApiException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ public static boolean isValidCommand(String command){
* @param commandName the command name to be checked for validity
* @return whether the command name is valid
*/
public static boolean isValidCommandName(String commandName){

// Refactored code by renaming isValueCommandName to isValCmdName method
public static boolean isValCmdName(String commandName){

Choose a reason for hiding this comment

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

in java, it's inconvenient to use abbreviations like here. No need for renaming

if (commandName == null || commandName.length() > 31) return false;
return commandName.matches("[A-Za-z_0-9]+");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder;

class AbilityTest {
// Refactored magic number -4 using extract method technique
final int inputNegativeNumber = -4;

Choose a reason for hiding this comment

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

When did array index become a magic number?

@Test
void argumentsCannotBeNegative() {
Assertions.assertThrows(IllegalArgumentException.class, () -> getDefaultBuilder().input(-4).build());
Assertions.assertThrows(IllegalArgumentException.class, () -> getDefaultBuilder().input(inputNegativeNumber).build());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.telegram.telegrambots.extensions.bots.commandbot.commands;
import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.bots.AbsSender;
import static org.junit.jupiter.api.Assertions.assertEquals;


public class BotCommandTest
{

@Test
public void testToString()
{
// Act
BotCommand commandidentifier = new TestBotCommand("test","Description");

// Assert
assertEquals("<b>/test</b>\nDescription",commandidentifier.toString());
}

@Test
public void testcommandIdentifierStartsWithCOMMAND_INIT_CHARACTER()
Copy link

@Senderman Senderman Mar 26, 2024

Choose a reason for hiding this comment

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

Only String objects and its methods are involved in this test. What are you trying to test here is core java's String.substring method, but it's not related to the library code, because this library doesn't hack into JVM

{
// Arrange
String commandidentifier = "/YuktaGurnani";

// Act
String result = commandidentifier.substring(1);

// Assert
assertEquals("YuktaGurnani",result);
}
private static class TestBotCommand extends BotCommand
{
public TestBotCommand(String commandIdentifier, String description){
super(commandIdentifier,description);
}
public void execute(AbsSender absSender, User user, Chat chat, String[] arguments)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.telegram.telegrambots.extensions.bots.commandbot.commands;

import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.extensions.bots.timedbot.TimedSendLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;
import static org.mockito.Mockito.mock;


public class TimedSendLongPollingBotTest
{
@Test
public void testMessageProcessing()
{
// Arrange
TimedSendLongPollingBot testbot = new TimedSendLongPollingBot()
{
@Override
public String getBotUsername() {
return null;
}

@Override
public void onUpdateReceived(Update update)
{

}
@Override
public void sendMessageCallback(Long chatId, Object messageRequest)
{

}
};

// Act
Long chatId = 002L;

Choose a reason for hiding this comment

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

Why not to use 2L or just 2?

Object messageRequest = mock(Object.class);

// Assert
testbot.sendTimed(chatId,messageRequest);
testbot.finish();
}
}
Loading