Optimize empty 'else' sections

Issue #139 resolved
Christian-W. Budde created an issue

Considering the following code:

procedure FooBar(a: Float);
begin
  if a < 0.5 then
    if a < 0.3 then
      Console.log('Foobar')
    else
  else
end;

FooBar(0.4);

will compile to

function FooBar(a) {
   if (a<0.5) {
      if (a<0.3) {
         console.log("Foobar");
      } else {
         /* null */
      }
   } else {
      /* null */
   }
};
FooBar(0.4)

With two empty else sections.

When I enable the cgoOptimizeForSize option I might expect that at least the last empty 'else' section would be removed by the optimization. This is currently not the case. The code is:

function FooBar(a){if(a<0.5){if(a<0.3){console.log("Foobar")}else{}}else{}};FooBar(0.4)

While it could be just:

function FooBar(a){if(a<0.5){if(a<0.3){console.log("Foobar")}}};FooBar(0.4)

The priority is set to minor as it's not what happens often in the real world. I just want to write it down here.

Comments (4)

  1. Eric Grange repo owner

    These cases should probably be optimized earlier than the codegen (in the compiler).

    In regular code such "else" clauses are probably a sign of an incorrect code, but they could also happen in "normal" code through conditional ifdef'ing

  2. Christian-W. Budde reporter

    In my case they are indeed a result of several IFDEFs. I am aware that I should probably improve the code to reduce the amount of conditional compilation, especially for sake of legibility, but so far I'm having quite a few sections with this "issue".

    As I mentioned earlier, it's not critical. I just found it while looking through some already minified code.

    In fact this sort of issue will get easily removed by the closure compiler, which runs at the build stage.

  3. Log in to comment