Macro line continuation style?

Which macro line continuation style should Minetest use?

Poll ended at Sat May 30, 2015 20:45

Aligned backslashes
7
78%
Backslashs after single space
2
22%
 
Total votes : 9

hmmmm
Member
 
Posts: 47
Joined: Tue Apr 02, 2013 04:04

Macro line continuation style?

by hmmmm » Sat May 23, 2015 20:45

Not all of Minetest's code style is well-defined at this point. For 95% of matters we defer to the Linux kernel code style, since it seems to be somewhat of a standard among open source projects, is low friction, and tends to make a lot of sense. However, there is a problem with the Linux recommended macro line continuation style. The Linux style dictates a tab stop spacing of 8, and further, alignment indentation is done using tabs, so it always comes out consistent for Linux. For our own alignment indentation we override this, using spaces, like so:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
#define FOOBAR(x) do {         \
    int __temp = (x);          \
    if (thing)                 \
        some_stuff_here(&(x)); \
    other(__temp);             \
} while (0)


However, if the tab size is set to 8 rather than 4, this same code would display as:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
#define FOOBAR(x) do {         \
        int __temp = (x);          \
        if (thing)                 \
                some_stuff_here(&(x)); \
        other(__temp);             \
} while (0)

which is argued by some to be broken and ugly.

So, the proposed solution is to not align macro line continuation characters at all, using a single space instead:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
#define FOOBAR(x) do { \
    int __temp = (x); \
    some_stuff_here(&(x)); \
    other(__temp); \
} while (0)


I argue that, while this style may be more consistent, it is uglier and less readable than the broken version, and makes the code equally ugly when viewed under any tab stop spacing.

What do the rest of you think?

Here is the associated PR.
 

paramat
Member
 
Posts: 2662
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat

Re: Macro line continuation style?

by paramat » Sat May 23, 2015 21:56

For me the backslashes are visual clutter that gets more intrusive and disorientating the closer to the code it is. Although aligned may at times become unaligned and messy, at least it is at a reasonable distance away from the code, the misalignment is far less irritating than close backslashes.
 

User avatar
Casimir
Member
 
Posts: 1101
Joined: Fri Aug 03, 2012 16:59

Re: Macro line continuation style?

by Casimir » Sun May 24, 2015 10:55

Why didn't we use tabs too?
 

est31
Member
 
Posts: 172
Joined: Mon Dec 29, 2014 01:49

Re: Macro line continuation style?

by est31 » Sun May 24, 2015 12:41

You can change your editor's behaviour regarding tabs to different modes:
1. The "tabstop" mode. This is where every "tab" will move the following text to the next "tabstop". Its the original way the tab key has been invented for, back then still on typewriters, and the linux code style guidelines are designed for tabstops that are configured for 8 spaces exactly.
The problem with tabstops on the right is, that the spacing may be completely uncertain.
So, in the best case, tabstops can help you with alignment:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
int __temp = (x);*TB*\
other(__temp);*TAB*\

but what happens with this example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
int __temp = somelongfunctionname(x);*TB*\
other(__temp);*TAB**TAB**TAB*TAB*TAB*TAB*\

you will have to use multiple tabs on the second line! Thats even worse than using spaces, because you'll never be certain how much tabs to use. Imagine if you watch the same file, just with another number of spaces configured to tabs:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
int __temp = somelongfunctionname(x);*TB*\
other(__temp);*TAB..**TAB..**TAB..*TAB..*TAB..*TAB..*\

So, tabbing style is broken here.
Elastic tabstops would help against this, but we shouldn't introduce completely new concepts.
2. The "constant space" mode. In here every tab character is associated a fixed number of characters.

I think, tab usage should be portable across all configurations, whether its "tabstops" or "constant spaces" (and both feature countless submodes). My proposal would even support "typewriter style" tabstops, that can be set manually (like in word): 1 cm for the first tab level, 1.5 for the second, and 2.7 for the 3rd.
Tabs give flexibility. We shouldn't take it away again. Moving those "ugly" characters doesn't *re*move them, they even create more problems.
Its a dead end if we want to align tab level differences on the left with spaces on the right.

The \ character is similar to the ; character, you don't align to that either. Yes, it looks different at first glance, but I'm sure you get accustomed to it.
 

User avatar
Linuxdirk
Member
 
Posts: 497
Joined: Wed Sep 17, 2014 11:21
GitHub: dsohler
In-game: Linuxdirk

Re: Macro line continuation style?

by Linuxdirk » Sun May 24, 2015 14:06

… why are there backslashes needed at all?
 

est31
Member
 
Posts: 172
Joined: Mon Dec 29, 2014 01:49

Re: Macro line continuation style?

by est31 » Sun May 24, 2015 14:17

In minetest, we use backslashes at two points:

1. For C preprocessor macros. The examples you see above aren't regular C code, they are preprocessor macros. You can use those to spare ugly repetition of similar code, like with functions, but in some ways different (for example you can use internal variables without passing, but its in no way an actual replacement, you have no stack, and you don't have any way to check for the type of your "arguments"). It is simply a part of the C standard, that if there is a new line without a backslash, the macro is considered to be terminated.

2. For the android makefile rules (The PR linked above is about those). Here, having multiple lines without a backslash between will start them in different subshells instead of executing them in one, meaning you can't use local variables, you'll lose pwd changes (the 'cd' stuff), and if constructs won't work (Btw. if starting in subshells would be sufficient for us, we won't need the ";"s either).

Also, it would theoretically work to cram everything into one line, but that's obviously a bad choice.
 

User avatar
Linuxdirk
Member
 
Posts: 497
Joined: Wed Sep 17, 2014 11:21
GitHub: dsohler
In-game: Linuxdirk

Re: Macro line continuation style?

by Linuxdirk » Sun May 24, 2015 16:10

est31 wrote:It is simply a part of the C standard, that if there is a new line without a backslash, the macro is considered to be terminated.

I never really used C. Thanks for the explanation!

So then the backslash is for escaping the newline, right? I’d use the last variant then (single space followed by the backslash). Oh, and spaces instead of tabs. Is there something like PEP-8 for C?
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Macro line continuation style?

by rubenwardy » Sun May 24, 2015 18:47

Linuxdirk wrote:
est31 wrote:It is simply a part of the C standard, that if there is a new line without a backslash, the macro is considered to be terminated.

I never really used C. Thanks for the explanation!

So then the backslash is for escaping the newline, right? I’d use the last variant then (single space followed by the backslash). Oh, and spaces instead of tabs. Is there something like PEP-8 for C?


This isn't Python, we follow the Linux code style. http://dev.minetest.net/Code_style_guidelines

Also, I always use tabs in python due to the reliance on indentation - editors always seem to muck up and remove spaces causing inconsistant indentation errors.
 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

Re: Macro line continuation style?

by Sokomine » Sun May 24, 2015 18:53

rubenwardy wrote:editors always seem to muck up and remove spaces causing inconsistant indentation errors.

It's alarming if an editor changes the code without beeing told to do so.
A list of my mods can be found here.
 

Zeno
Member
 
Posts: 140
Joined: Sun Jun 29, 2014 03:36
GitHub: Zeno-

Re: Macro line continuation style?

by Zeno » Mon May 25, 2015 07:31

I guess we could require that right-alignment be one tab at EOL followed by the required amount of spaces for alignment. I doubt anyone would remember to do it though :)
 

est31
Member
 
Posts: 172
Joined: Mon Dec 29, 2014 01:49

Re: Macro line continuation style?

by est31 » Mon May 25, 2015 15:45

I think git will see this as whitespace error, Zeno.
 

nrz
Member
 
Posts: 51
Joined: Sat Feb 07, 2015 17:16
GitHub: nerzhul
IRC: nrzkt
In-game: nrz

Re: Macro line continuation style?

by nrz » Mon Jun 01, 2015 06:34

I have a preference for one whitespace before backslash when we are developping, it's better and faster, but i also agree that align backslash permit a better reading of the code. Then i think we should use the secondary, maintenance is better than faster coding.
 


Return to Minetest General

Who is online

Users browsing this forum: No registered users and 14 guests

cron