Code simplification through refactoring

Is there a refactoring tool, either for C or for Java, that can simplify this type of redundant code. I believe this is called data dissemination.

This is what the optimizing compiler will do.

public int foo() { int a = 3; int b = 4; int c = a + b; int d = c; System.out.println(c); return c; } 

in

 public int foo() { int c = 7; System.out.println(c); return c; } 
+6
source share
9 answers

I think this is not a good idea.

This is, for example, the following code:

 long hours = 5; long timeInMillis = hours * 60 * 1000; 

This is much simpler and easier than just:

 long timeInMillis = 300000; 
+20
source

I can offer a solution for C. My solution uses two tools that I described in another answer here (in reverse order).

Here is your program translated into C:

 int foo() { int a = 3; int b = 4; int c = a + b; int d = c; printf("%d", c); return c; } 

Step 1: Continuous Distribution

 $ frama-c -semantic-const-folding tc -lib-entry -main foo ... /* Generated by Frama-C */ /*@ behavior generated: assigns \at(\result,Post) \from \nothing; */ extern int ( /* missing proto */ printf)() ; int foo(void) { int a ; int b ; int c ; int d ; a = 3; b = 4; c = 7; d = 7; printf("%d",7); return (c); } 

Step 2: Slicing

 $ frama-c -slice-calls printf -slice-return foo -slice-print tt.c -lib-entry -main foo ... /* Generated by Frama-C */ extern int printf() ; int foo(void) { int c ; c = 7; printf("%d",7); return (c); } 
+6
source

Yes, the best refactoring tool I've seen people with is the brain.

The brain seems to be a remarkably good tool for logically organizing code for consumption by other brains. It can also be used to improve code with comments when necessary, and give additional meaning to layout and naming.

Compilers are good for optimizing code for consumption by the base layer closer to the transistors that make up the processor. One of the advantages of higher-generation langauge programming is that it is not readable, like something that was made by a machine.

Apologizes if this seems a little dazzling and worthless. Of course, I used various tools, but I do not remember any tool that handled "data transfer".

+3
source

Eclipse (and I'm sure NetBeans and IntelliJ) have almost all of these refactorings. I will talk about the features of Eclipse. Start with:

 public int foo() { int a = 3; int b = 4; int c = a + b; int d = c; System.out.println(c); return c; } 

Firstly, d will appear as a warning that you have an unread local variable. <CTRL>+1 on this line and select "Delete d and all destinations." Then you have:

 public int foo() { int a = 3; int b = 4; int c = a + b; System.out.println(c); return c; } 

Then highlight a in int c = a + b; and enter <CTRL>+<ALT>+I into line a . Repeat with b and you will have:

 public int foo() { int c = 3 + 4; System.out.println(c); return c; } 

Now you are almost there. I don’t know refactoring to convert 3 + 4 to 7. It seems that it will be easy for someone to implement, but this is probably not a common use case, as others have indicated that depending on the domain, 3 + 4 may be more expressive, than 7. You can go ahead and embed c by providing you with:

 public int foo() { System.out.println(3 + 4); return 3 + 4; } 

But it is impossible to find out if this has improved or a step backward without knowing the "real" problem with the source code.

+2
source

semantic code information may be lost. possible dependencies may be broken. In short: only the programmer knows which variables are important or may become important, since only the programmer knows the context of the code. I'm afraid you will have to do refactoring yourself.

+1
source

Yes, IntelliJ offers this functionality within its community version. Now, to solve a more serious problem, I am sure that you are mixing compilation with refactoring. When you compile something, you take the language above machine code and convert it to machine code (essentially). You want to remove declarations that are redundant in a high-level language that is your program file, .c, .java, etc. It is possible that the compiler has already optimized less good code in what you offer, there are tools available to view what it does. In terms of refactoring, less is usually better, but does not sacrifice maintainability for smaller lines of code.

+1
source

One possible approach is to put it in a symbolic mathematical program (like Mathematica or Maple) and make this simplification for you. He will do this regardless of whether they are constants or not.

The disadvantage is that you need to convert the code to another language. (Although this can be basically copy and paste if the syntax is similar.) Also, it can be dangerous if you expect some integer types to overflow with a certain size. Symbolic math programs do not care and optimize it according to "math". The same goes for floating point rounding errors.

In your example, if you enter this in Mathematica:

 a = 3; b = 4; c = a + b; d = c; c 

Will output this in Mathematica:

 7 

Of course, you cannot just copy and paste because it is a different language and different syntax, but it is the best that I have in mind for your question. I myself use Mathematica to simplify expressions and other maths before throwing it into C / C ++.

For a more complex example involving unknowns:

Original C Code:

 int a = 3 + x*x; int b = 4 + y*y; int c = a + b - 7 + 2*x*y; int d = c; 

Enter this into Mathematica (which still basically copies and pastes):

 a = 3 + x*x; b = 4 + y*y; c = a + b - 7 + 2*x*y; d = c; FullSimplify[c] 

Conclusion:

 (x + y)^2 

What converts back to the following C code:

 d = (x + y) d = d * d; 

This is obviously much simpler than the source code. In general, symbolic programs even process non-trivial expressions and will do the same (or even better) than any internal compilers.

The final downside is that symbolic math programs like Mathematica or Maple are not free and quite expensive. SAGE is an open source program, but I heard that it is not as good as Mathematica or Maple.

+1
source

If you are talking about C, you can look at the compiled optimized assembler code. Then you can reorganize your C code into the same structure as the optimized build. However, as Alfredo said, this can lead to more ambiguous code.

0
source

Why not compile the code using an optimization compiler. Then decompile the code. This is just my thought, and I have not tried it.

0
source

Source: https://habr.com/ru/post/896003/


All Articles