1. Quick Poll: what is the correct indentation for ifdef?

I am currently updating Edita's Re-indent function to cope with ifdef statements.

I can either indent ifdef as if part of the code:

      while 1 do 
          ifdef WIN32 then 
              msg = "windows" 
          elsedef 
              msg = "linux" 
          end ifdef 
          puts(1,msg) 
      end while 

or, and I think I prefer this way, to emphasize them as preprocessing statements:

      while 1 do 
  ifdef WIN32 then 
          msg = "windows" 
  elsedef 
          msg = "linux" 
  end ifdef 
          puts(1,msg) 
      end while 

I suppose the real question I am asking is do I need an option, and if so what should the precise wording of that question be?

Pete

new topic     » topic index » view message » categorize

2. Re: Quick Poll: what is the correct indentation for ifdef?

Sorry, I forgot about things like this:

      ifdef WIN32 then 
          if c_func(xGetWindow,{}) then 
      elsedef 
          if int80(getwin) then 
      end ifdef 
              ... 
          end if 

If you try and treat ifdef as "part of the code", there is no proper indent for that...

new topic     » goto parent     » topic index » view message » categorize

3. Re: Quick Poll: what is the correct indentation for ifdef?

This probleem has been intro'd by the definition .
There for I did a other definition in PEU :
add conditional compiling : ifdef , elseifdef elsedef enddef . (02-02-09) Menno
max 10 define's and 10 if levels . (no checks)
So it has nothing to do with the syntax as is done in C with the #if.. .

new topic     » goto parent     » topic index » view message » categorize

4. Re: Quick Poll: what is the correct indentation for ifdef?

petelomax said...

I suppose the real question I am asking is do I need an option, and if so what should the precise wording of that question be?

There is no right way to indent things. Everyone sees indentation differently.

I'd ask ... Align 'ifdef' at column 1? Yes/no

new topic     » goto parent     » topic index » view message » categorize

5. Re: Quick Poll: what is the correct indentation for ifdef?

petelomax said...

Sorry, I forgot about things like this:

      ifdef WIN32 then 
          if c_func(xGetWindow,{}) then 
      elsedef 
          if int80(getwin) then 
      end ifdef 
              ... 
          end if 

If you try and treat ifdef as "part of the code", there is no proper indent for that...

That is not valid syntax anyway. An if statement must be totally enclosed by an ifdef block.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Quick Poll: what is the correct indentation for ifdef?

The first way is what I prefer. The options could be called: 'Indent defined word statements with other code' ot 'indent defined words separately'

Shawn Pringle

new topic     » goto parent     » topic index » view message » categorize

7. Re: Quick Poll: what is the correct indentation for ifdef?

I personally like to indent ifdef as any old if statement. The reason being is I treat it like a normal if statement. I don't care (when reading the logic of the code) that it's done in the pre-processing of the Euphoria source vs. runtime of the Euphoria source. All I care about is that:

if 1 then 
    ifdef WINDOWS then 
         alert("Hello") 
    end ifdef 
end if 

If 1 is true and we are running on Windows then a hello alert box will appear. To me, having ifdefs on the first column really breaks up the flow of the code and is very difficult for me to follow. I am sure that one will adapt to their coding style and be able to read the flow no matter how they indent it, but for me it's easier to treat them all the same for the purpose of readability.

Jeremy

new topic     » goto parent     » topic index » view message » categorize

8. Re: Quick Poll: what is the correct indentation for ifdef?

Oh, also, I really try to keep the ifdefs to definitions for clarity. Some places I use it in code but most places I try to abstract. I know your code was a simple example and not really intended for real code, but to give an example I would have:

-- Application constants 
ifdef WINDOWS then 
    constant platform_name = "windows" 
elsedef 
    constant platform_name = "linux/bsd/osx/something else" 
end ifdef 
 
-- Application code 
while 1 do 
    puts(1, platform_name) 
end while 

If some logic needs to change deeply nested, I try to abstract it as well:

ifdef WINDOWS then 
    procedure alert(sequence msg) 
        win32:msgbox(msg) 
    end procedure 
elsedef 
    procedure alert(sequence msg) 
        puts(1, msg) 
    end procedure 
end ifdef 
 
while 1 do 
    if error then 
        alert("bad code") 
    end if 
end while 

instead of

while 1 do 
    if error then 
        ifdef WINDOWS then 
            win32:msgbox("bad code") 
        elsedef 
            puts(1, "bad code") 
        end ifdef 
    end if 
end while 

This makes me think out the code and make it more reusable. I know the above is a simple example, but having an alert() for windows, linux, os/x all different is good programming as it allows me to fix the alert or make it more platform centric very easily without affecting my code not related to displaying an alert message, for example my error detection/reporting code.

Thus, with the above style, I think that how to indent ifdefs becomes less of a problem as you do not tend to have deeply nested ifdefs. Once exception, of course, is ifdef DEBUG type structures.

Jeremy

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu