Complicating code in C#

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 1:01 pm

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: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 1: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.

Pio
Posts: 252
Joined: Sat Feb 25, 2012 9:42 pm
Location: Stockholm
Contact:

Re: Complicating code in C#

Post by Pio » Thu Dec 10, 2020 2:43 pm

Henk wrote:
Thu Dec 10, 2020 1: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: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 3:03 pm

Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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 3:10 pm, edited 1 time in total.

Pio
Posts: 252
Joined: Sat Feb 25, 2012 9:42 pm
Location: Stockholm
Contact:

Re: Complicating code in C#

Post by Pio » Thu Dec 10, 2020 3:07 pm

Henk wrote:
Thu Dec 10, 2020 3:03 pm
Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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: 252
Joined: Sat Feb 25, 2012 9:42 pm
Location: Stockholm
Contact:

Re: Complicating code in C#

Post by Pio » Thu Dec 10, 2020 3:16 pm

Pio wrote:
Thu Dec 10, 2020 3:07 pm
Henk wrote:
Thu Dec 10, 2020 3:03 pm
Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 3:19 pm

Pio wrote:
Thu Dec 10, 2020 3:07 pm
Henk wrote:
Thu Dec 10, 2020 3:03 pm
Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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: 252
Joined: Sat Feb 25, 2012 9:42 pm
Location: Stockholm
Contact:

Re: Complicating code in C#

Post by Pio » Thu Dec 10, 2020 3:25 pm

Henk wrote:
Thu Dec 10, 2020 3:19 pm
Pio wrote:
Thu Dec 10, 2020 3:07 pm
Henk wrote:
Thu Dec 10, 2020 3:03 pm
Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 3:34 pm

Pio wrote:
Thu Dec 10, 2020 3:25 pm
Henk wrote:
Thu Dec 10, 2020 3:19 pm
Pio wrote:
Thu Dec 10, 2020 3:07 pm
Henk wrote:
Thu Dec 10, 2020 3:03 pm
Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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: 252
Joined: Sat Feb 25, 2012 9:42 pm
Location: Stockholm
Contact:

Re: Complicating code in C#

Post by Pio » Thu Dec 10, 2020 4:32 pm

Henk wrote:
Thu Dec 10, 2020 3:34 pm
Pio wrote:
Thu Dec 10, 2020 3:25 pm
Henk wrote:
Thu Dec 10, 2020 3:19 pm
Pio wrote:
Thu Dec 10, 2020 3:07 pm
Henk wrote:
Thu Dec 10, 2020 3:03 pm
Pio wrote:
Thu Dec 10, 2020 2:43 pm
Henk wrote:
Thu Dec 10, 2020 1: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

Post Reply