Stylizing code

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Stylizing code

Post by bob »

Sven Schüle wrote:
Kempelen wrote:in place of:

Code: Select all

static const int EV_B_SITUACION_CABALLO_NEGRO[64] = {
    -30, -24, -14, -10, -10, -14, -24, -30,
    -20, -10,   0,   6,   6,  0, -10,  -20,
     -3,   5,  16,  22,  22, 16,   5,   -3,
     -4,   4,  14,  20,  20, 14,   4,   -4,
    -10,   0,  10,  14,  14, 10,   0,  -10,
    -14, -10,   0,   4,   4,  0, -10,  -14,
    -17, -12,  -7,  -5,  -5, -7, -12,  -17,
    -25, -20, -15, -12, -12, -15, -20, -25
};
I guess you meant:

Code: Select all

static const int EV_B_SITUACION_CABALLO_NEGRO[64] = {
    -30, -24, -14, -10, -10, -14, -24, -30,
    -20, -10,   0,   6,   6,   0, -10, -20,
     -3,   5,  16,  22,  22,  16,   5,  -3,
     -4,   4,  14,  20,  20,  14,   4,  -4,
    -10,   0,  10,  14,  14,  10,   0, -10,
    -14, -10,   0,   4,   4,   0, -10, -14,
    -17, -12,  -7,  -5,  -5,  -7, -12, -17,
    -25, -20, -15, -12, -12, -15, -20, -25
};
:-)
Kempelen wrote:This is always annoying.

How do you stylize code to avoid this things? do you have any other stylizing tips?
Personally, I prefer not to use any tool other than a text editor for this task ... Using a stylizer (beautifier, ...) tool may have some advantages but it must fit your needs "really perfect", otherwise you end up fixing all those annoying exceptions again and again, each time you run it over all your sources. I don't know any such tool, and in fact I never missed it.

So my advice would be: always write your code in its "final" layout from the beginning, then you won't need a beautifier tool for later "postprocessing".

Sven
Indent is pretty nice, because you can specify just about everything one might want to do. And once you are happy with the output, you can save those options in an .indent.pro file and you are set for life...
User avatar
phhnguyen
Posts: 1434
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Stylizing code

Post by phhnguyen »

Kempelen wrote:Hello,

I use AStyle for stylizing my code. I suppose many here use it too. But I have a problem with array initialization, like PSTs. It always show wrong:

Code: Select all

static const int EV_B_SITUACION_CABALLO_NEGRO[64] = {
    -30, -24, -14, -10, -10, -14, -24, -30,
    -20, -10,   0,   6,   6,  0, -10,  -20,
    -3,   5,  16,  22,  22, 16,   5,   -3,
    -4,   4,  14,  20,  20, 14,   4,   -4,
    -10,   0,  10,  14,  14, 10,   0,  -10,
    -14, -10,   0,   4,   4,  0, -10,  -14,
    -17, -12,  -7,  -5,  -5, -7, -12,  -17,
    -25, -20, -15, -12, -12, -15, -20, -25
};
in place of:

Code: Select all

static const int EV_B_SITUACION_CABALLO_NEGRO[64] = {
    -30, -24, -14, -10, -10, -14, -24, -30,
    -20, -10,   0,   6,   6,  0, -10,  -20,
     -3,   5,  16,  22,  22, 16,   5,   -3,
     -4,   4,  14,  20,  20, 14,   4,   -4,
    -10,   0,  10,  14,  14, 10,   0,  -10,
    -14, -10,   0,   4,   4,  0, -10,  -14,
    -17, -12,  -7,  -5,  -5, -7, -12,  -17,
    -25, -20, -15, -12, -12, -15, -20, -25
};
This is always annoying.

How do you stylize code to avoid this things? do you have any other stylizing tips?
I am interested about this. However, I think there is no simple solution:
- in technique, astyle has to know little bit about grammar instead of some line states only. It also needs to scan the code at least twice to beautify some above lines based on bellow lines. Thus if the program is designed for scanning code only one, there is no hope for new improvement in coming time
- there is no standard about style of an array
- we seem to have different ideas about code beauty of an array. the "price" for it may not be cheap

For example, I am not sure if all have the same acceptant level about the below code:

Code: Select all

    int64 array1[] = {
        0, 0, 0, 0,  0,   0, 0,            0,
        0, 0, 0, 0,  0,   0, 0,            0,
        0, 0, 0, 0,  0, 430, 0,            0,
        0, 0, 0, 0, 60,   0, 0,            0,
        0, 0, 0, 0,  0,   0, 0,            0,
        0, 0, 0, 0,  0,   0, 0, 456548098090
    };

    int64 anotherStylingArray2[] = { // will be crazy if you have double columns
                   0,            0,            0,            0,             0,              0,            0,            0,
                   0,            0,            0,            0,             0,            430,            0,            0,
                   0,            0,            0,            0,             0,              0,            0, 456548098090
    };
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: Stylizing code

Post by Kempelen »

phhnguyen wrote:
Kempelen wrote:Hello,

I use AStyle for stylizing my code. I suppose many here use it too. But I have a problem with array initialization, like PSTs. It always show wrong:

Code: Select all

static const int EV_B_SITUACION_CABALLO_NEGRO[64] = {
    -30, -24, -14, -10, -10, -14, -24, -30,
    -20, -10,   0,   6,   6,  0, -10,  -20,
    -3,   5,  16,  22,  22, 16,   5,   -3,
    -4,   4,  14,  20,  20, 14,   4,   -4,
    -10,   0,  10,  14,  14, 10,   0,  -10,
    -14, -10,   0,   4,   4,  0, -10,  -14,
    -17, -12,  -7,  -5,  -5, -7, -12,  -17,
    -25, -20, -15, -12, -12, -15, -20, -25
};
in place of:

Code: Select all

static const int EV_B_SITUACION_CABALLO_NEGRO[64] = {
    -30, -24, -14, -10, -10, -14, -24, -30,
    -20, -10,   0,   6,   6,  0, -10,  -20,
     -3,   5,  16,  22,  22, 16,   5,   -3,
     -4,   4,  14,  20,  20, 14,   4,   -4,
    -10,   0,  10,  14,  14, 10,   0,  -10,
    -14, -10,   0,   4,   4,  0, -10,  -14,
    -17, -12,  -7,  -5,  -5, -7, -12,  -17,
    -25, -20, -15, -12, -12, -15, -20, -25
};
This is always annoying.

How do you stylize code to avoid this things? do you have any other stylizing tips?
I am interested about this. However, I think there is no simple solution:
- in technique, astyle has to know little bit about grammar instead of some line states only. It also needs to scan the code at least twice to beautify some above lines based on bellow lines. Thus if the program is designed for scanning code only one, there is no hope for new improvement in coming time
- there is no standard about style of an array
- we seem to have different ideas about code beauty of an array. the "price" for it may not be cheap

For example, I am not sure if all have the same acceptant level about the below code:

Code: Select all

    int64 array1[] = {
        0, 0, 0, 0,  0,   0, 0,            0,
        0, 0, 0, 0,  0,   0, 0,            0,
        0, 0, 0, 0,  0, 430, 0,            0,
        0, 0, 0, 0, 60,   0, 0,            0,
        0, 0, 0, 0,  0,   0, 0,            0,
        0, 0, 0, 0,  0,   0, 0, 456548098090
    };

    int64 anotherStylingArray2[] = { // will be crazy if you have double columns
                   0,            0,            0,            0,             0,              0,            0,            0,
                   0,            0,            0,            0,             0,            430,            0,            0,
                   0,            0,            0,            0,             0,              0,            0, 456548098090
    };
I am looking into indent tool, recommendad bad Bob, it is great at first view, and you can hand format one time this arrays and tell the program not format then only adding 2 lines of codes as the sample put by bob.
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
User avatar
phhnguyen
Posts: 1434
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Stylizing code

Post by phhnguyen »

It is good that you have a solution for your code!

For me, I am very lazy to add any lines just for styling. I also don't want to explain them later for other people. Both code formatted by astyle and code with adding those lines are not perfect and at same annoying level.

Thus I will accept any thing a beautifier can do.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Stylizing code

Post by Don »

bob wrote: Indent is pretty nice, because you can specify just about everything one might want to do. And once you are happy with the output, you can save those options in an .indent.pro file and you are set for life...
I may have to look at indent again. I tried to use it years ago and it immediately was confused by my commenting style. I remember that it balked on '/** ...' and the issue was that it was looking for just '/*' followed by a space or newline. I could not work around it and I figured it was useless to me if before I even started I had to go through the entire code base and start modifying it.

I'm sure that bug has been fixed by now ...
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Stylizing code

Post by bob »

Don wrote:
bob wrote: Indent is pretty nice, because you can specify just about everything one might want to do. And once you are happy with the output, you can save those options in an .indent.pro file and you are set for life...
I may have to look at indent again. I tried to use it years ago and it immediately was confused by my commenting style. I remember that it balked on '/** ...' and the issue was that it was looking for just '/*' followed by a space or newline. I could not work around it and I figured it was useless to me if before I even started I had to go through the entire code base and start modifying it.

I'm sure that bug has been fixed by now ...

It works fine, although I don't like the syntax. but if you want to use that, it won't cause any problems. Just indented this to test:

/****** comment ******/
int f(int v) {
if (v >= 0.0)
return 0;
else
return 1;
}

And I get the same thing as the final result.
UncombedCoconut
Posts: 319
Joined: Fri Dec 18, 2009 11:40 am
Location: Naperville, IL

Re: Stylizing code

Post by UncombedCoconut »

bob wrote:int f(int v) {
if (v >= 0.0)
ಠ_ಠ
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Stylizing code

Post by Don »

bob wrote:
Don wrote:
bob wrote: Indent is pretty nice, because you can specify just about everything one might want to do. And once you are happy with the output, you can save those options in an .indent.pro file and you are set for life...
I may have to look at indent again. I tried to use it years ago and it immediately was confused by my commenting style. I remember that it balked on '/** ...' and the issue was that it was looking for just '/*' followed by a space or newline. I could not work around it and I figured it was useless to me if before I even started I had to go through the entire code base and start modifying it.

I'm sure that bug has been fixed by now ...
Thanks, I will check it out again then.




It works fine, although I don't like the syntax. but if you want to use that, it won't cause any problems. Just indented this to test:

/****** comment ******/
int f(int v) {
if (v >= 0.0)
return 0;
else
return 1;
}

And I get the same thing as the final result.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Stylizing code

Post by bob »

UncombedCoconut wrote:
bob wrote:int f(int v) {
if (v >= 0.0)
ಠ_ಠ
That was code used during the great "0.0" debate. I just added a "Don format" comment at the top to make sure indent didn't crap out...