Whats "good code"

New to programming here, trying to make a chess game. Are my if statements bad or too big? My game keeps getting more bloated as time goes on and my classes are somewhat meshing together a bit. How do I know if my code is good?

Attached: my_if.png (2447x1138, 222.18K)

Other urls found in this thread:

chessprogramming.org/Main_Page
en.wikipedia.org/wiki/Linter_(software)
twitter.com/SFWRedditGifs

If it looks good, it is good. If it looks bad, it is bad.

It basically doesn't matter, the compiler will optimise better than a human could anywya.

>interpreted language
>compiler
what did he mean by this?
(not to mention quality of code isnt determined only by its performance)
your code looks bad because its in python

I see a lot of code duplication here.
You could refactor by taking code you use more than once, parametrizing it, turning it into a function, and replacing all original instances of the code with function calls.

Looks messy. Hard to judge such a small sample but I suspect it's not very good.
>classes
OOP is not typically the preferred approach for chess. But explaining why would take too long. For now, ignore what I just said and keep going.
As a beginner, writing shitty code is totally fine. Getting things done is more important than getting them done well.
When you feel you've made a mess, start again from scratch.

On your broader question: code is rarely ever "good" or "bad" without context (so long as it is correct).
There are no hard and fast rules that apply in every domain and each situation.

Your append can be folded in a for loop with a counter variable.

finish the project first, complain later
otherwise you will get nothing done

put the if statements in a function maybe also if you write huge if statements explain what they do

>How do I know if my code is good?
this is a very difficult question to answer, maybe impossible. but it's good that you ask it.

short term, look for patterns of repeated code and try to replace them with functions. a suggestion for the snippet you posted might be to write a function which checks if there are only empty spaces between one space and another. you can use that to check both sides of the castle, and probably for other piece moves too.

long term:
1: keep writing code. this is most important. code a lot. ideally long projects you change incrementally, as opposed to something you write all at once and then stop.
2. work with other programmers. friends, other newbies, professionals, open source, doesn't matter.
3: read a book or two on the subject. it doesn't really matter which books

Is it ok to define functions inside functions? I also think I need to check all the variables and dictionaries, there's too many of them and some might redundant (there was a 4 month gap where I didn't work on it so I'm kinda having to learn about my own code at this point).


I know you said it would take too long but why? It feels really intuitive with each piece being a class with a different method for moving.

I will look into this

Its basically done, I think now I only need to make players take turns, add en passant and maybe display the taken pieces. Could also make a chess engine or take one from somewhere and make it interact with the the game to make it singleplayer (This feels like another project in itself).

Trying to make a webm but this place only accepts

Yeah you can put a function in a function in Python no problem.

>there was a 4 month gap where I didn't work on it so I'm kinda having to learn about my own code at this point

good. anything that you can read now is good code. anything that makes you go "wtf is this" is bad.

keep it up user

>It feels really intuitive with each piece being a class with a different method for moving.
Yes, you can do it that way. But chess is a simple game. There is no hidden information.
Think about the pieces' positions. You either store them in the pieces as their state, you store them externally in "the board" or you duplicate that information. The last is the worst of these options and the first can be very awkward.
And if you don't store the position in the pieces they have no state at all so why even model them as objects.

As another point, a chess board's positions can be stored *very* efficiently which becomes important when you want to add an AI and have big game trees.
That is very much limited when you introduce OO.

You don't need to worry about any of this just yet. It's great as an exercise in OOP, go for it.
But keep in mind that many things can be modeled in other ways that end up more efficient and more natural. That applies to this kind of games in particular because there is no information hiding and every move needs to conceptually interact with the global state, i.e., everything.

>short term, look for patterns of repeated code and try to replace them with functions. a suggestion for the snippet you posted might be to write a function which checks if there are only empty spaces between one space and another. you can use that to check both sides of the castle, and probably for other piece moves too.

Yeah, alot of people talk about this, maybe I'll try to optimize this code after I finish this. Read it all, comment and try to make it as simple as possible.

>2. work with other programmers. friends, other newbies, professionals, open source, doesn't matter.

I've been thinking about downloading some open source code and trying to understand it on my own, maybe it will give me some idea about how big projects work.

>read a book or two on the subject. it doesn't really matter which books

You mean things like SICP or are there books about making big projects? I never see one that talks about things like frameworks, work flow, git, readable code and shit, seems like you just learn some of these things on the go.

IMHO short code is good code, but do not try to make it shorter artificially

not related to the code but for chess programming i recommend this website
chessprogramming.org/Main_Page
are writing an AI or GUI?
anyways good luck because chess programming is an addiction!

Good code is code that you yourself are able to understand a year from now. In terms of style, use a linter:
en.wikipedia.org/wiki/Linter_(software)

I guess its a GUI.

Attached: chess-2.webm (640x360, 770.36K)

good code is one step less than great code, which is one step less than epic code, which of course is one step less than leetcode

>I've been thinking about downloading some open source code and trying to understand it on my own, maybe it will give me some idea about how big projects work.
that's a good idea. i'd recommend also trying to make a small change to whatever it is, even if its just for you. reading something something and saying "i think i get this" is not the same as actually trying to make a change to how it works.

>You mean things like SICP or are there books about making big projects?
yes, there are lots of books about big projects. but its not a science so people will argue about which bits are good advice. the best thing to do is read lots, keep what you feel is good advice and discard the rest.

if you want a starting point I'd recommend Code Complete by Uncle Bob. he covers a lot of stuff you mentioned, and most of the advice is good. don't treat it as a bible though. just try apply it and see what feels good

very nice, i started the same way
if you want to support playing with chess engines you should look up the UCI protocol.
basically you start the engines as processes and control their input/output though pipes.

B-but what o-of pajeet code, user-sama?