Stylizing code

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Stylizing code

Post by Kempelen »

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?
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
User avatar
towforce
Posts: 11585
Joined: Thu Mar 09, 2006 12:57 am
Location: Birmingham UK

Re: Stylizing code

Post by towforce »

How about using a text editor that has regular expressions like Notepad++?

It also knows the syntax of most of the commonly used languages, matches parenthesis etc...
Writing is the antidote to confusion.
It's not "how smart you are", it's "how are you smart".
Your brain doesn't work the way you want, so train it!
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Stylizing code

Post by Sven »

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
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: Stylizing code

Post by Kempelen »

towforce wrote:How about using a text editor that has regular expressions like Notepad++?

It also knows the syntax of most of the commonly used languages, matches parenthesis etc...
I was refering to formating style, not to colorize. That is, placing the correct indents, tabs, spaces, putting brances in correct places and so on..... and mainly to correctly formating tables like those used for PST

I don't fully understand how regular expression in notepad++ can help. It looks to me is more a pain than calling a command tool like astyle.exe to do all that work
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
User avatar
towforce
Posts: 11585
Joined: Thu Mar 09, 2006 12:57 am
Location: Birmingham UK

Re: Stylizing code

Post by towforce »

Kempelen wrote:I don't fully understand how regular expression in notepad++ can help.
You could have it find numbers in a list and ensure that:

leading spaces + number = constant length
It looks to me is more a pain than calling a command tool like astyle.exe to do all that work
Yes - this is absolutely right. It probably isn't the right solution for you.
Writing is the antidote to confusion.
It's not "how smart you are", it's "how are you smart".
Your brain doesn't work the way you want, so train it!
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Stylizing code

Post by Don »

Kempelen wrote:Hello,

I use AStyle for stylizing my code. I suppose many here use it too.
Wow! I have just recently considered doing this too since Larry is now touching my code and doesn't yet have any sense of proper formatting.

I would also be annoyed by the failure to line up columns properly.


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?
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: Stylizing code

Post by Kempelen »

towforce wrote:
Kempelen wrote:I don't fully understand how regular expression in notepad++ can help.
You could have it find numbers in a list and ensure that:

leading spaces + number = constant length
It looks to me is more a pain than calling a command tool like astyle.exe to do all that work
Yes - this is absolutely right. It probably isn't the right solution for you.
I have written a suggestion to developer. Maybe he could implement this feature, or at least to treat exceptions.
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Stylizing code

Post by Don »

Kempelen wrote:
towforce wrote:
Kempelen wrote:I don't fully understand how regular expression in notepad++ can help.
You could have it find numbers in a list and ensure that:

leading spaces + number = constant length
It looks to me is more a pain than calling a command tool like astyle.exe to do all that work
Yes - this is absolutely right. It probably isn't the right solution for you.
I have written a suggestion to developer. Maybe he could implement this feature, or at least to treat exceptions.
Agreed. Arrays like that probably just need to be left alone for the programmer to format.

I would also like to see a formatter that has a mode that re-formats every line of code completely - in other words no matter how you may format the source code the output is standardized and will always come out the same.
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Stylizing code

Post by jdart »

I've used emacs as my primary editor for 30+ years. It has code styling for C and C++ built in (it is customizable).

I also sometimes use a tool called bcpp, available in most Linux distros. It works reasonably well and also has a lot of options but it is sometimes confused and does the wrong thing.

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

Re: Stylizing code

Post by bob »

Don wrote:
Kempelen wrote:
towforce wrote:
Kempelen wrote:I don't fully understand how regular expression in notepad++ can help.
You could have it find numbers in a list and ensure that:

leading spaces + number = constant length
It looks to me is more a pain than calling a command tool like astyle.exe to do all that work
Yes - this is absolutely right. It probably isn't the right solution for you.
I have written a suggestion to developer. Maybe he could implement this feature, or at least to treat exceptions.
Agreed. Arrays like that probably just need to be left alone for the programmer to format.

I would also like to see a formatter that has a mode that re-formats every line of code completely - in other words no matter how you may format the source code the output is standardized and will always come out the same.
I have a simpler answer. :)

I format such data by hand, one time. I use indent regularly to make sure indenting and spacing is consistent. If you put this before that kind of array initialization:

/* *INDENT-OFF* */

and this after that code:

/* *INDENT-ON* */

all is well. Indent won't touch the stuff between the two lines and leave the nicely formatted initialization stuff alone, while fixing everything else up as it should...