Я использую методологию BDD agil для проектирования и создания новой системы, но я новичок в рефакторинге кода, не могли бы вы мне помочь? Что я могу сделать, чтобы улучшить этот код, и какими советами вы будете следовать каждый раз, когда вы, ребята, хотите улучшить код.
Большое спасибо.
Здесь вы можете найти код Groovy, созданный для тестирования некоторых функций:
package client
import com.resiflex.client.domain.Client
class DeleteSpec extends Base {
def "delete client"() {
given: "I have the code of client created"
def clientCreatedResponse = clientService.createClient(clientName, clientColor, currentDescription)
def code = null
if (clientCreatedResponse.getObject() != null) {
def clientCreated = (Client) clientCreatedResponse.getObject()
ids.add(clientCreated)
code = clientCreated.getCode()
}
when: "delete client"
def result
if (code == null) {result=clientService.deleteClientByCode(code ,tenants).getError().code} else if (tenants != "0"){
result=clientService.deleteClientByCode(code ,tenants).getError().code
}
then: "client is deleted"
expect == result
where:
clientName | clientColor | currentDescription | tenants | expect
"PCI" | "#121273" | "Positive Care Ireland" | "0" | null
"POCI" | "#FF5FF5" | "Positive Care" | "1" | 4022
"POCIN" | "#FF555F" | "Positive Care" | "0" | null
null | null | null | "0" | 4021
}
}
Здесь вы можете найти основной класс Java:
package com.resiflex.client.service;
import com.resiflex.client.domain.Client;
import com.resiflex.client.dto.ClientResponse;
import com.resiflex.client.utils.cleaner.StringCleaner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.resiflex.client.repository.ClientRepository;
import com.resiflex.client.utils.BusinessException;
import com.resiflex.client.utils.validators.ClientValidator;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
public class ClientService {
@Autowired
ClientRepository clientRepository;
ClientValidator validator = new ClientValidator();
StringCleaner cleaner = new StringCleaner();
public ClientService(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
public ClientResponse createClient(String name, String color, String description) {
ClientResponse response = new ClientResponse();
try {
String cleanName = cleaner.removeWhiteSpace(name, (short) 4003, "name");
String cleanColor = cleaner.removeWhiteSpace(color, (short) 4004, "color");
if (validator.isValidName(cleanName) && validator.isValidColor(color)) {
if (validator.isUniqueName(clientRepository.findByName(cleanName.toUpperCase())) &&
validator.isUniqueColor(clientRepository.findByColor(cleanColor))) {
Client client = new Client();
client.setName(cleanName.toUpperCase());
client.setColor(cleanColor);
client.setCode(client.generateCode(0, 0, getClientCodes()));
client.setDescription(description);
clientRepository.save(client);
response.setObject(client);
}
}
} catch (BusinessException be) {
response.setError(be);
} catch (Exception e) {
response.setError(new BusinessException((short) 4007, "client_form"));
e.printStackTrace();
}
return response;
}
public ClientResponse getAllClients(String status) throws BusinessException {
ClientResponse response = new ClientResponse();
try {
response.setObject(clientRepository.findAllByStatus(status));
} catch (Exception e) {
response.setError(new BusinessException((short) 4015, "client_list"));
}
return response;
}
private List<String> getClientCodes() {
List<String> codes = new ArrayList<>();
for (Client cli : clientRepository.findAll()) {
codes.add(cli.getCode());
}
return codes;
}
@Transactional
public ClientResponse updateClientDesc(String code, String desc){
ClientResponse resp = new ClientResponse();
Client client = clientRepository.findByCode(code);
if (client==null){
BusinessException businessException = new BusinessException((short) 4016, "client_form");
resp.setError(businessException);
}else{
if (desc.length()<=30) {
client.setDescription(desc);
resp.setObject(clientRepository.save(client));
}else{
BusinessException businessException = new BusinessException((short) 4008, "description");
resp.setError(businessException);
}
}
return resp;
}
@Transactional
public ClientResponse updateClientColor(String code, String color){
ClientResponse resp = new ClientResponse();
Client client = clientRepository.findByCode(code);
if (client==null){
BusinessException businessException = new BusinessException((short) 4016, "client_form");
resp.setError(businessException);
}else try {
String cleanColor = cleaner.removeWhiteSpace(color, (short) 4004, "color");
if (validator.isValidColor(color)) {
if (validator.isUniqueColor(clientRepository.findByColor(cleanColor))) {
client.setColor(cleanColor);
clientRepository.save(client);
resp.setObject(client);
}
}
} catch (BusinessException be) {
resp.setError(be);
}
return resp;
}
public ClientResponse deleteClientByCode(String code, String tenants) {
ClientResponse resp = new ClientResponse();
Client client = clientRepository.findByCode(code);
if (client==null){
BusinessException businessException = new BusinessException((short) 4021, "user_blank");
resp.setError(businessException);
}else{
if (tenants.equalsIgnoreCase("0")) {
clientRepository.delete(client);
resp.setObject(null);
}else{
BusinessException businessException = new BusinessException((short) 4022, "delete");
resp.setError(businessException);
}
}
return resp;
}
public Client findByCode(String code) throws Exception {
return clientRepository.findByCode(code);
}
}