Page 1 of 1

Clean code

Posted: January 5th, 2014, 1:13 pm
by Psychoman
Hello ppl!!! I haven't been here for a long time, so a lot of things happened in my programming life. I wanted to ask a question that has a big interest to me and I hope to everyone. Everytime, when I write an algorithm, that concerns games I have the same thing all the time. You need to check all exclusive situations of the game and with that comes hole crew of "if". F.e. you have good ol' tic tac toe game. When you're writing an AI , you need all that "if" considering which move player made. So the thing is , how can you avoid all this crew of "if" ? I know 1 way to do so , it's pattern "State" , but I'm too lazy to write a pattern , so is there some another way ?

Re: Clean code

Posted: January 5th, 2014, 4:14 pm
by mmmmmm
use "switch"

Re: Clean code

Posted: January 5th, 2014, 5:07 pm
by Psychoman
Man, I'm talking about different style of making program , not "if" or "switch". Just to make things more clear , my question is about semantics not syntax.

Re: Clean code

Posted: January 5th, 2014, 6:15 pm
by MrGodin
programs basically boil down to " is it, or is it not", so you need to write functions or conditions to test this. There really is no other way. Just as binary is either 0 or 1.. it's on or its off. Unless you can come up with some crafty way of determining this, you're stuck with if's or switches ect or in simple cases you can use .. example .
bool DoMove = false
(DoMove) ? x = 1 : x = 0;
if DoMove is true then x= 1 otherwise x =0
can't think of much more to say
X is an arbitrary value as is 1 and 0 in this example

Re: Clean code

Posted: January 6th, 2014, 8:35 am
by Psychoman
Well, I'm looking for such crafty generalized solution

Re: Clean code

Posted: January 6th, 2014, 10:44 pm
by albinopapa
Break up what you can and put the grouped if statements or switch/case in their own functions. It won't change the behavior of the program, but it will make it look nicer if that's what you're going for.

Re: Clean code

Posted: January 9th, 2014, 4:08 pm
by Psychoman
Well you might ( very often ) have a situation , where you have nested if statements or switch , doesn't rly matter and good code turns bad , what about this situation?

Re: Clean code

Posted: January 10th, 2014, 5:35 am
by albinopapa
Functions are the only way I can think of to "clean up" code, especially if you find you can reuse portions of code if you find yourself retyping. There's always the possibility that different approach would reduce the amount of ifs or switch/cases.