Questions tagged [refactoring]

1

votes
2

answer
38

Views

refactoring a code with inline JavaScript

I'm trying to refactor this w3schools for a toggleable tabs code because all of us know that using inline JavaScript is a very bad practice so I'm trying to sepearte them as much as possible so I selected the tablinks and I added an event listener but I'm struggling with the city names (take a look...
Med Amine Elwere
1

votes
1

answer
26

Views

How Do I Refactor Common React Components?

I am styling a required TextField like this const styles = theme => ({ labelAsterisk: { color: 'red' }, cssLabel: { color: 'orange' }, cssRequired: { '&:before': { borderBottom: '2px solid orange' } }, }); I have lots of these required fields in my forms and would like to standardise it instead of c...
opticyclic
0

votes
3

answer
43

Views

How to refactor methods

I have two methods. They're very similar. I've tried use Generics, but it doesn't work with TryParse() public static int EnterIntengerNumber() { while (true) { Console.Write('Enter an intenger number: '); if (int.TryParse(Console.ReadLine(), out int number)) { return number; } else { ConsoleError('I...
Evgen
1

votes
3

answer
155

Views

Auto remove getter-setters with lombok?

I have a project. I don't use Lombok. I use manually created getters and setters. Now, I want to import Lombok. After I import, I will use for all entities. Do I have to manually add @Data to all entities? And I have to remove all entities manually? Is not there another way to automatically convert...
asdasasd
1

votes
3

answer
445

Views

Reduce the Cyclomatic complexity of a function

I have a function which uses the JPA Criteria API to retrieve data from the database depending on some params I pass to it. The params are passed using the object gridParams, as following : public List find(final GridParamsDTO gridParams) { final CriteriaBuilder builder = getCriteriaBuilder(); final...
Ichigo Kurosaki
1

votes
1

answer
60

Views

How do I refactor hibernate entities?

My use case is as follows: I've inherited a project that is using hibernate. The entity I'm focused on right now is, for the purposes of this exercise, closed to modification. The object of the exercise is to replace the use of the legacy entity with an unrelated implementation that is better suit...
VoiceOfUnreason
1

votes
0

answer
25

Views

Refactor validation rules generating function

I need assistance with refactoring a function (the function is long and can be parted out later). I have a fields object that contains every column in a database table and has some additional information on the column such as the max_length and whether or not it can be null. These parameters form th...
Alex
1

votes
1

answer
45

Views

How do you add a new argument to a pure function without having to update all calls to the function?

I make a pure function (example in js, but the question doesn't depend on the language) with one input argument and call it all over my application. var day = 'Tuesday'; var doYouExist = function(you) { return !!you; } Then I realise that I should only check if you exist on Tuesdays so I decide to...
P0lska
1

votes
2

answer
21

Views

Is there a simple way to refactor file indentation for a while code base?

I have a project which started with 4-space indentation, then switched to 2-space for some files only. Now I have .editoconfig file to standardize the indentation *.{ts}] charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true indent_style = space indent_size = 2 Is there a way t...
dragonmnl
1

votes
1

answer
34

Views

turning a validate function from if statements to a switch statements

I have a form in react that I'm refactoring. I'm going to move most of the state and logic to the parent, because the parent state will be updated with the form result... but I was going to refactor before and can't seem to get a switch statement to work. I was told it would help performance in the...
AJdevs
1

votes
2

answer
26

Views

Is it safe to replace less-specific variable types with a new class that just inherits from the previously defined type?

For example, if I have a class such as this: public class TestClass { public IDictionary Variable {get; set;} } and I decided I need to use this type elsewhere, but don't want to keep using IDictionary everywhere as it seems a bit smelly.. how 'safe' is it for me to refactor the code by adding 2 mor...
James Trotter
1

votes
0

answer
115

Views

Clang-Format: recombine string literals

Is there a way to get clang-format to turn this: function_name('a very very ' |
iPherian
1

votes
1

answer
32

Views

How to build a query based on a number of different URIs

What I currently have done: Since I am using aws lambda and aws API Gateway, I have learned to capture specific parts of the URI such as the Path and the queryParameters, and I am building different queries to my database based on the path and parameter given. Example: https://ApiURI/user?id=id_1 Pa...
monkey intern
1

votes
2

answer
45

Views

Refactor code duplication of XCTestAssertions in a seperate method while writing unit test cases in Objective C

I have a unit test class with multiple test cases. Some test cases have common XCTest Assertions. These test cases implements an API call. This API call can have different input request parameters but the response is same. I have put test assertions on response. So, is it good by design to extract o...
tech savvy
1

votes
1

answer
143

Views

How to find unneeded PackageReference in csproj

I am in the process of doing clean up on a project that has gone through much of the dev cycle and is almost ready to go to production. I wanted to remove any unneeded PackageReferences in the the csproj but can not seem to find any way to detect them, except to perhaps to remove each one individual...
Serj Sagan
1

votes
0

answer
67

Views

Converting/Mapping between objects/DTO's and Entity or vice versa that require a call to Database

We have an integration system. We are converting/mapping one object returned by an API to another different object to be used in POST request of another totally separate API. Right now, everything is done in one giant function making debugging really time consuming. However, it is not as simple as u...
H. Grewal
1

votes
0

answer
113

Views

How to restructure a git repository into branches

I have a git repository with a structure like this: .git\ AB1\ AB2\ CD1\ EF1\ EF2\ and want to remodel it so that it is contained in three branches: branchA with folders AB1\ and AB2\, branchB with the folder CD1\ and branchC with folders EF1\ and EF2\. So that each branch contains only the folders...
JackJadon
1

votes
0

answer
32

Views

Defeatng Cyclomatic Complexity with ActiveModel::assign_attributes in Rails

I wish to share how I solved a Cyclomatic Complexity in ROR. If any of you have another solution or a better idea, please share, it will be wellcome. BACKGROUND I am writing a quotation system, it needs 10 images and 3 files (dxf, skp and sh3d). I used carrierwave to accomplish the images/files proc...
OfficeYA
1

votes
1

answer
99

Views

Eclipse refactor to add superclass to type hierarchy

I've read the different options that are listed in the manual for refactoring here but I don't think what I want is there. I have a project with many batch jobs which are classes that extend org.quartz.Job to which I of course I can't make changes. As I find myself writing a bunch of the same code i...
Brian
1

votes
0

answer
69

Views

Refactoring java classes where methods have different parameter types

I have two classes that extend an abstract class in a very similar manner, and I'd like to abstract out the common parts since I will likely have to use this again. They each return a ThingLink containing data linking them to a Parent object. They also return a Widget that varies based on the class,...
TrivialCase
1

votes
1

answer
111

Views

pyCharm refactoring python class attribute name, does not rename all attribute usages

Considering the following: class test: att = 7 def print_class(class_instance): print(class_instance.att) if (__name__ == '__main__'): t = test() print_class (t) print_class expects a class test instance as a parameter, but this is not typed. Now I'd like to rename by refactoring att However, prin...
OJNSim
1

votes
0

answer
37

Views

Refactoring - Going from specific to generic interfaces

I have a question about refactoring and how to make interfaces and classes generic. Suppose I have the following situation: Before a plane can take off, a number of checks (validations) must be made. Some of these validations are on plane-level (e.g. plane has enough fuel). Other validations are...
northerner
1

votes
1

answer
87

Views

VS Code Refactoring: Change all occurences - but only in block scope

When using 'change all occurences' in VS Code, it will just search the whole file for matches and change them. Is there a similar feature doing the same thing, but limiting it to function or block scope? Let's take an example where I would need that: I'm having a React file with several components a...
Adrian
1

votes
1

answer
63

Views

Java refactor programmatically

As a process of rebranding, we have a change the names in one of our huge project. Is there a way that let me refactor my Java code base (rename class and package) programmatically using eclipse/other tools?
1

votes
0

answer
20

Views

ImproperlyConfigured while porting code from Django 1.8 to Django 2.0

I am trying to make some project described in a book based on Django 1.8. This part should enroll a student to some course: CBV: class CourseDetailView(DetailView): model = Course template_name = 'courses/manage/course/detail.html' def get_context_data(self, **kwargs): context = super().get_contex...
Chiefir
1

votes
0

answer
52

Views

How to keep track of methods used in Java

Is there a way to keep track of methods from all files used by a particular module (all files that are used to complete the functionality of a module) without using existing tools. I would like to hear your thoughts on how to design a tool that would get the code coverage of a C and Java file using...
KaelJasper
1

votes
0

answer
193

Views

Good interactor / use case class design?

I've heard that an interactor / use case class should only have one method which is the execute() method as it was discussed here and implemented here. But I found an example where the interactor class has more than one methods here. Which one is better? And as far as I know, the interactor classes...
Andika Wasisto
1

votes
1

answer
95

Views

Refactoring nested foreach loop

Can you help me refactor this block of code? I'm quite having a hard time to decide how can I refactor nested for each loop or not use foreach loop at all. $matcherResults = []; foreach ($resultItems as $reqId => $resultItem) { if (empty($resultItem)) { continue; } foreach ($resultItem as $reg => $d...
alvirbismonte
1

votes
1

answer
36

Views

In VS Is there a way to change all derived classes method arguments at once?

Basically the title. But I will provide an example in case I didn't word it correctly. public abstract class A { public abstract void Method(Foo foo); } public class B : A { public override void Method(Foo foo) { throw new System.NotImplementedException(); } } public class C : A { public override vo...
craftomega
1

votes
0

answer
44

Views

refactoring Rxjs observables using functions that make semantic sense and reduce mess

I want to refactor a chain of observables so that thay make semantic sence, my code looks close to this. //Ajax call that gets the users data this.getUser .pipe( flatmap((user)=>{ //Stuff that gets the user's picture }), flatmap((picture)=>{ //Stuff that saves the user's picture }), ) All that does...
Joaquin Brandan
1

votes
0

answer
43

Views

Splitting linq code into smaller methods: type definition noise

I make extensive use of linq for my calculation code (accounting-related). Because of common 'rule' that method body should not exceed 1 screen, I decided to split my lengthy linq-based method into chunks of smaller methods. After refactoring, my methods look similar to this one: private static IEnu...
Alexei S
1

votes
0

answer
19

Views

How to write an ITranslationUnit to a file

There is no setPath function as a part of the translationunit. so how does one save it to another file? (a file different from the one it parsed) I have been able to parse a C++ file (in a java program outside of Eclipse using the jars) and get a working ITranslationUnit from it. I want to modify a...
Leo
1

votes
0

answer
62

Views

How to avoid duplicates using inheritance or polymorphism, best way to refactor

I have a class Operation Class Operation { virtual void execute(Operation, OperationCallback) = 0 } With a callback using OperationCallback = std::function; and like 30 different classes extending from it InsertOperation : public Operation EditOperation : public Operation Each of them implemented e...
Kenenisa Bekele
1

votes
2

answer
706

Views

Providing default value on typesafe config getters

So I am having multiple occurrences of similar snippets in my code: val optionValue = try { Some(config.getString(key)) } catch { case _: Missing => None } I want to somehow eliminate this duplicates from my code. I know that typesafe provides a way to give a fallback config file to provide default...
vatsal mevada
1

votes
1

answer
58

Views

How to remove code block from Rails Controller

I am trying to remove a code section from my controller so that it looks clean and thin. In one of my controller I have to use many conditions and that is making my controller look very heavy. I just want to remove just that code block in other file and want to include it into my controller. My cont...
Nilay Singh
1

votes
1

answer
352

Views

Javascript - Google Drive v3 API and functions

Trying to create a connection to the google drive v3 api using the JavaScript SDK. Basically the end goal I want to achieve is to access various files from google drive and add in metadata (but not got quite that far yet). Ran through the tutorial on the google drive api guides (https://developers.g...
strlight
1

votes
1

answer
49

Views

Swift iOS: How to refactor a lot of conditions for if statement?

I have a string that is retrieved from a keychain, and I transform the string to a dictionary. Then, I check the values of the dictionary to see wether or not it is nil or empty. override func awake(withContext context: Any?) { super.awake(withContext: context) var dictionary = [String : Any]() if l...
Curt Rand
1

votes
0

answer
20

Views

How do I match multiple arguments in rope?

Note: rope is a tool for building Python refactoring into editors like Emacs and vim. It can also be used directly to execute refactors outside of an editor. I'm trying to apply a restructuring to a function taking a variable number of arguments, e.g. going from # math.py (before refactor) add(1, 2,...
qff
1

votes
4

answer
44

Views

Refactor if statement with many or

How to refactor this conditions to make simpler and cleaner? Logic is completely different for each if. if(in_array($sides, [self::ALL_SIDES, self::HORIZONTAL_SIDES, self::TOP_SIDE])) { //do a() } if(in_array($sides, [self::ALL_SIDES, self::HORIZONTAL_SIDES, self::BOTTOM_SIDE])) { //do b() } if(in_a...
zen
1

votes
0

answer
37

Views

How to safely move packages in IntelliJ Idea?

When I want to move a package, say org.company.project.numbers to org.company.project.data resulting in org.company.project.data.numbers, I use Refactor -> Rename/Move. However, I want to move the entire Maven structure and make the test packages to copy the structure. Example, I want to refactor t...
Nikolas

View additional questions