I am trying to refactor my code. But I dont know how to do what I want to do. I dont know how it’s called so I dont find it with google. My Code: I want to refactor everything so the final version looks like this: or like this: So the “If(from).IsMultipleOf(3)” shall return true/false and if…
Tag: refactoring
argument mismatch; invalid functional descriptor for lambda expression
I am using the jdbi library like so: This try catch pattern is repeated a few times, with various different queries passed to the jdbi.withHandle method. Another example: The jdbi.withHandle method has this signature: I am trying to find a way to reduce the duplication around the try-catch, so that I can use …
How do I refactor multiple if-else statements in Java?
How do I refactor all this code that seems repetitive and too long, is there a way to make it shorter? I have tried using the Switch statement but that does not work in this circumstance. the “typeOfData” variable holds a String that is used to match relevant lines. Answer Simplify You can extract…
Refactoring code that contains multiple if conditions
Here is some code I’ve written to save a UrlEntity : The above code exists in a service class and looks unnecessarily complex. I’m attempting to refactor so that the intent is clear. This is a basic refactoring I’ve written: This refactoring does not improve the code substantially. Is there …
Refactor regex Pattern into Java flavor pattern
I have a regex pattern created on regex101.com: https://regex101.com/r/cMvHlm/7/codegen?language=java however, that regex does not seem to work in my Java program (I use spring toolsuite as IDE): I get the following error: Is there a way to find out where index 1337 is? Answer The main problem with the regex …
Extracting multiple string to constant in Intellij IDEA
In my code, there are a lot of strings appeared in the following structure: I would like to extract the string key to a constant. However, doing this one by one will take forever…So, is there a better way to achieve this? Answer No idea in Intellij, but if your lines are genuinely that simple, you can d…
How to change a package name in Eclipse?
In Eclipse I have a simple Java project that contains a package named (default package) and inside this package I have a class. I want to rename this package into something like: com.myCompany.executable I tried to select the (default package) —> right click —> refactor but now I see only th…
How to simplify a null-safe compareTo() implementation?
I’m implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): I want the natural ordering for these objects to be: 1) sorted by name and 2) sorted by value if name is the same; both comparisons should be case-in…