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.
User avatar
mvanthoor
Posts: 586
Joined: Wed Jul 03, 2019 2:42 pm
Full name: Marcel Vanthoor

Re: Complicating code in C#

Post by mvanthoor » Tue Dec 08, 2020 11:40 am

JohnWoe wrote:
Tue Dec 08, 2020 10:26 am
Without looking any further. I count 6 indentation levels. After 3 you are generally kinda fucked. :D

It's code smell.
LOL. It's C#. I've been looking into the language for professional reasons, and I've come to the conclusion that often, you're at 5-6 indentation levels deep even BEFORE you start writing your very first line of code in a class. Therefore 8-9 levels of indentation in C# is more the rule than exception.

Code: Select all

namespace Whatever {
	class Thing {
		public void Function() {
			using context(some_stuff) {
				try {
					//
				}
				catch {
					//
				}
				finally {
					//
				}
			}
		}
	}
}
You write your code where the comments are, and if you need ifs and loops and whatever, you'll have even more indentation levels.

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Tue Dec 08, 2020 12:14 pm

I even did not know that context was not thread safe. So I tried to re-use it. Terrible. I can throw that code in the dustbin.
So always use using for databaseContext.

By the way locking is dangerous too for clients start waiting.

User avatar
maksimKorzh
Posts: 525
Joined: Sat Sep 08, 2018 3:37 pm
Location: Ukraine
Full name: Maksim Korzh
Contact:

Re: Complicating code in C#

Post by maksimKorzh » Tue Dec 08, 2020 7:36 pm

mvanthoor wrote:
Mon Dec 07, 2020 2:37 pm
PS: with regard to implementing MVV-LVA sorting/move picking, this is my current implementation:

Code: Select all

// Move sorting routines.

use super::Search;
use crate::{defs::NrOf, movegen::defs::MoveList};

// MVV_VLA[victim][attacker]
pub const MVV_LVA: [[u8; NrOf::PIECE_TYPES + 1]; NrOf::PIECE_TYPES + 1] = [
    [0, 0, 0, 0, 0, 0, 0],       // victim K, attacker K, Q, R, B, N, P, None
    [50, 51, 52, 53, 54, 55, 0], // victim Q, attacker K, Q, R, B, N, P, None
    [40, 41, 42, 43, 44, 45, 0], // victim R, attacker K, Q, R, B, N, P, None
    [30, 31, 32, 33, 34, 35, 0], // victim B, attacker K, Q, R, B, N, P, None
    [20, 21, 22, 23, 24, 25, 0], // victim K, attacker K, Q, R, B, N, P, None
    [10, 11, 12, 13, 14, 15, 0], // victim P, attacker K, Q, R, B, N, P, None
    [0, 0, 0, 0, 0, 0, 0],       // victim None, attacker K, Q, R, B, N, P, None
];

impl Search {
    pub fn score_moves(ml: &mut MoveList) {
        for i in 0..ml.len() {
            let m = ml.get_mut_move(i);
            let value = MVV_LVA[m.captured()][m.piece()];
            m.add_score(value);
        }
    }

    pub fn pick_move(ml: &mut MoveList, index: u8) {
        for i in (index + 1)..ml.len() {
            if ml.get_move(i).score() > ml.get_move(index).score() {
                ml.swap(i as usize, index as usize);
            }
        }
    }
}
And that's everything there is.
Marcel, I always enjoy reading your code snippets))) It's like if I was reading my own code but if I had a better habits)
JavaScript chess engine with UCI support, own GUI and public API:
https://github.com/maksimKorzh/wukongJS

Chess programming YouTube channel:
https://www.youtube.com/channel/UCB9-pr ... KKqDgXhsMQ

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Wed Dec 09, 2020 9:50 am

Talking about idiotic names. Is kind the right name.
Inspired by three of a kind etc. Or should that be called PieceType.

Code: Select all

public class ChessPiece 
{ 
 public enum Kind { Pawn_Kind, Knight_Kind, Bishop_Kind, Rook_Kind, Queen_Kind, King_Kind, None_Kind };
}

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Wed Dec 09, 2020 2:49 pm

Refactoring taking too much time or effort. Maybe better write only static methods. That makes it easier to move them to another file.

c# also has this:

Code: Select all

using Skipper.Models.Interface;
using static System.Math;

So you can't see where methods come from. And when you move the wrong using statements. You get compile errors.
So I would almost say don't use 'using static'

Now the procedure is this. Extract a method. Make it static and repair at least twenty compile errors.
So taking much time/effort.

I even think about putting evaluation methods in a separate map like I had some years ago.

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 11:04 am

C# 9 has records. Some kind of immutable classes. So I downloaded some new stuff.
Also latest visual studio version. But yesterday I forget to close visual studio. This morning it asked should I restore. I said yes.
What happened? Some thousands of compile errors. Because it deleted my old ChessPiece.Sort type which I had renamed yesterday.


But Including all its references!!!

Tried to repair. But hopeless. In the end I had to uncommit changes. Grrr. Really shocking.

Still have compile errors but much less.

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 11:29 am

Maybe conclusion should be can't change lowest level datastructure. For they are referenced too much.

Or maybe beware if method has a large reference count. For you can't change it.

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 12:05 pm

I don't see how you can debug:

Code: Select all

object.f1()
        .f2()
        .f(3)
for debugger does not allow to set a breakpoint to line of f2.

So again with functional programming you have to use intermediate variables with silly names to make it possible to debug your source code. By the way linq queries are even more terrible to debug. Or maybe using let expressions.

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 12:31 pm

Henk wrote:
Thu Dec 10, 2020 12:05 pm
I don't see how you can debug:

Code: Select all

object.f1()
        .f2()
        .f(3)
for debugger does not allow to set a breakpoint to line of f2.

So again with functional programming you have to use intermediate variables with silly names to make it possible to debug your source code. By the way linq queries are even more terrible to debug. Or maybe using let expressions.
You set The breakpoint in f2

Henk
Posts: 6711
Joined: Mon May 27, 2013 8:31 am

Re: Complicating code in C#

Post by Henk » Thu Dec 10, 2020 12:53 pm

Pio wrote:
Thu Dec 10, 2020 12:31 pm
Henk wrote:
Thu Dec 10, 2020 12:05 pm
I don't see how you can debug:

Code: Select all

object.f1()
        .f2()
        .f(3)
for debugger does not allow to set a breakpoint to line of f2.

So again with functional programming you have to use intermediate variables with silly names to make it possible to debug your source code. By the way linq queries are even more terrible to debug. Or maybe using let expressions.
You set The breakpoint in f2
Yes but less convenient. I sometimes have trouble by looking up the definition of a method. Best would be to keep files small.

Post Reply