Complicating code in C#

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

I see no better solution than replace ICoord (coordinate of a square) by an int otherwise an instance of it would become an object with extra overhead. If you would use predefined coord objects then you have to lock them when they are created. Might be a problem when there are many. Maybe you will implement three or multi dimensional chess in the future. Who knows.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Complicating code in C#

Post by Pio »

Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent to
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Last edited by Henk on Thu Dec 10, 2020 4:10 pm, edited 1 time in total.
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Complicating code in C#

Post by Pio »

Henk wrote: Thu Dec 10, 2020 4:03 pm
Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Just check for the range in the constructor and throw an exception if out of range
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Complicating code in C#

Post by Pio »

Pio wrote: Thu Dec 10, 2020 4:07 pm
Henk wrote: Thu Dec 10, 2020 4:03 pm
Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Just check for the range in the constructor and throw an exception if out of range
It might be better to use debug.Assert in your case since it might be in a time critical part of the engine and debug.Assert is only included while running in debug mode so it won’t affect speed in release mode
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Pio wrote: Thu Dec 10, 2020 4:07 pm
Henk wrote: Thu Dec 10, 2020 4:03 pm
Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Just check for the range in the constructor and throw an exception if out of range
It is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.

Or maybe a struct with one property Value. Still not efficient.
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Complicating code in C#

Post by Pio »

Henk wrote: Thu Dec 10, 2020 4:19 pm
Pio wrote: Thu Dec 10, 2020 4:07 pm
Henk wrote: Thu Dec 10, 2020 4:03 pm
Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Just check for the range in the constructor and throw an exception if out of range
It is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.

Or maybe a struct with one property Value. Still not efficient.
A struct is a value type just like an int. it has no reference. What are you talking about?
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Pio wrote: Thu Dec 10, 2020 4:25 pm
Henk wrote: Thu Dec 10, 2020 4:19 pm
Pio wrote: Thu Dec 10, 2020 4:07 pm
Henk wrote: Thu Dec 10, 2020 4:03 pm
Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Just check for the range in the constructor and throw an exception if out of range
It is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.

Or maybe a struct with one property Value. Still not efficient.
A struct is a value type just like an int. it has no reference. What are you talking about?
Doubt if
struct Coord
{
int Value;
}

is similar to int Value. You have to use coord.Value to access it.

c# does not support subrange.

Also want an interface for its implementation might change.

So I get

interface ICoord
{
int Value
}

That should be implemented by a struct?

I think I just use an int Coord; for now.

Unit tests should capture the errors. I don't know.

Just use int instead of an ICoord and see what happens. Check its subrange in constructor where Coord is part of.

O wait it is not the constructor everwhere where I use Coord in interface I have to validate it.

So
interface ICoord
{
int Value
}

would be more correct if it would not be slow and inconvenient. (coord.Value)
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Complicating code in C#

Post by Pio »

Henk wrote: Thu Dec 10, 2020 4:34 pm
Pio wrote: Thu Dec 10, 2020 4:25 pm
Henk wrote: Thu Dec 10, 2020 4:19 pm
Pio wrote: Thu Dec 10, 2020 4:07 pm
Henk wrote: Thu Dec 10, 2020 4:03 pm
Pio wrote: Thu Dec 10, 2020 3:43 pm
Henk wrote: Thu Dec 10, 2020 2:54 pm https://stackoverflow.com/questions/161 ... in-c-sharp

You can do something with 'using' statement in C#

Code: Select all

using BitCoord64 = Int;
Doubt if that will be practical.
You can use structs. See documentation with an example with coords see https://docs.microsoft.com/en-us/dotne ... pes/struct
In the example for X and Y they use double. I could use int.

I like to have an equivalent
say typedef BitCoord64 = int range [0 ..63]
So EnPassantSquare() would return BitCoord64 and I would be sure its range is limited to 0 .. 63. If it returns an int it might return 99 or something. There is no implicit validation. Or i have to add extra code in EnPassantSquare method or property or whatever.
Just check for the range in the constructor and throw an exception if out of range
It is still using a struct. A one dimensional coord would be more efficient. Thought that new operations should be avoided or only used in factory methods.

Or maybe a struct with one property Value. Still not efficient.
A struct is a value type just like an int. it has no reference. What are you talking about?
Doubt if
struct Coord
{
int Value;
}

is similar to int Value. You have to use coord.Value to access it.

c# does not support subrange.

Also want an interface for its implementation might change.

So I get

interface ICoord
{
int Value
}

That should be implemented by a struct?

I think I just use an int Coord; for now.

Unit tests should capture the errors. I don't know.

Just use int instead of an ICoord and see what happens. Check its subrange in constructor where Coord is part of.

O wait it is not the constructor everwhere where I use Coord in interface I have to validate it.

So
interface ICoord
{
int Value
}

would be more correct if it would not be slow and inconvenient. (coord.Value)
You can use implicit conversion operators to achieve what you want. See https://stackoverflow.com/questions/15 ... r-a-struct for how to write the conversions.

You completely misunderstand when to use interfaces. As long as you don’t have two or more classes there is no benefit of using interfaces it just makes the code a lot worse. Interfaces should only be used with behaviour so properties are really really bad to use in interfaces since those expose the data not behaviour. The only place where an interface might be used in an engine is if you use it for supporting different protocols or maybe if you want to use different search algorithms but probably you won’t need an interface in that case either you could just use a function declaration