Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace if statements without braces to include braces

I've been recently using sonar for code analysis. When I go thorough violation drilldown, I found many java files with if statement defined without braces (thousands of places). Is there a simple way to replace or add braces to if statements or what are the steps that I can perform to achieve this task without doing it manually in each of the files.

I'm currently using intelliJ.

like image 342
darkapple Avatar asked Dec 21 '22 17:12

darkapple


2 Answers

Is there a simple way to replace or add braces to if statements or what are the steps that I can perform to achieve this task without doing it manually in each of the files.

I don't know if there is a tool to do this automatically. (There probably is ...) But assuming that such a tool exists, I'm not convinced it would be the right approach.

Step back for a moment and consider why the code analysis has reported this as a problem. A lot of people (like @pst and me) think that the braces always should be there, even though various style guides don't insist on this. There is a good reason for this ... apart from "it looks ugly". Consider these example code snippets:

if (i == 1) 
    i++;
    doSomething();

while (i < 1)
    i++;
    doSomething();

If you don't read those carefully, your eyes will trick you into thinking that doSomething(); is called conditionally ... due to the incorrect indentation. And it happens.

(Aside: labelling someone as "inept" for misreading that code is not helpful. If you are desperately trying to fix a show-stopper bug and you've been working for 14 hours straight, then you are likely to miss this kind of thing. And not because you are inept. Once you've been in that situation a couple of times, the lesson sinks in ...)

OK, now suppose that you run an automatic tool to add the braces. What you will get is this:

 if (i == 1) {
    i++;
 }
    doSomething();

 while (i < 1) {
    i++;
 }
    doSomething();

It means exactly the same thing as the original code. BUT ... what if the original code was actually a bug? What if the programmer intended the doSomething() calls to be conditional?

In short, by adding the braces automatically, we've obscured the original programmer's intention, and made these bug(s) harder to track down.


Bottom line - I think it would be prudent to manually review each of these occurrences ... rather than just "fixing" them automatically. Indeed, I'd argue that if you don't have the time or patience to review them manually, it would be better to leave the code alone. It would be better to turn off the warning ... IMO.

like image 188
Stephen C Avatar answered Dec 24 '22 03:12

Stephen C


I can reformat the code to make intelliJ do the thing for me, but I need to go through all the files and reformat it. Yes, I might turned off the check but wondering if there is a good tool to do the task. I've good set of tests to check whether it introduce bugs during the process.

If you are sure that you have some ways to test that you will not introduce bugs then use the IntelliJ Reformat Code feature.

Just make sure that the Code Style you have in IntelliJ is in line with your company's policies. Otherwise you will force your style on everybody else too.

To force braces just mark them as Always on the Wrapping and Braces tag in the Code Style settings dialog in IntelliJ.

Mark the source folder in the project view and press Ctrl-Alt-L. A dialog pops up and there you can chose All files in directory <...>.

Then press Run and see what happens. If you are not satisfied then just revert from your VCS.

like image 21
maba Avatar answered Dec 24 '22 03:12

maba