Clean code
Clean code
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
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
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
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
Curiosity killed the cat, satisfaction brought him back
Re: Clean code
Well, I'm looking for such crafty generalized solution
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Clean code
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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Clean code
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?
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Clean code
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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com