Clean code

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Clean code

Post by Psychoman » January 5th, 2014, 1:13 pm

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 ?

mmmmmm
Posts: 38
Joined: August 12th, 2013, 6:33 am

Re: Clean code

Post by mmmmmm » January 5th, 2014, 4:14 pm

use "switch"

Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Re: Clean code

Post by Psychoman » January 5th, 2014, 5:07 pm

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.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Clean code

Post by MrGodin » January 5th, 2014, 6:15 pm

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
Curiosity killed the cat, satisfaction brought him back

Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Re: Clean code

Post by Psychoman » January 6th, 2014, 8:35 am

Well, I'm looking for such crafty generalized solution

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Clean code

Post by albinopapa » January 6th, 2014, 10:44 pm

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

Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Re: Clean code

Post by Psychoman » January 9th, 2014, 4:08 pm

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?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Clean code

Post by albinopapa » January 10th, 2014, 5:35 am

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

Post Reply