• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TrentonK

My OLD Syntax

80 posts in this topic

The OP code is not much different, however I find that

if (!something)
{
do stuff;
}

is a lot harder to read when I have a nest that is 5+ deep.

 I personally prefer

if (!something){
do stuff;
}
0

Share this post


Link to post
Share on other sites

If you have a nesting that deep probably the issue is the excessive nesting... (easier said than done, since this is not just a coding style issue, it's a programming issue as it's directly tied to the code structure)

1

Share this post


Link to post
Share on other sites

If you have a nesting that deep probably the issue is the excessive nesting... (easier said than done, since this is not just a coding style issue, it's a programming issue as it's directly tied to the code structure)

 Hmm, let me dig around and see if I can find one of my "overly nested" projects . ( LSL is a nightmare for nests, however I do not believe I have any examples in that language left . )

0

Share this post


Link to post
Share on other sites

I swear we had a programming style's discussion/argument not less than a month ago.  will it never end?!

 

Nope, it is a hazard of the profession rolleyes.gif

 

You're wasting vertical space ... this is much better biggrin.png

if (keyboard_check(vk_left)) {
        x -= 5;              }

 

There is nothing wrong with wasting space.  As Servant said "Whitespace is free.  Monitors are large."  Everyone knows this is the right way:


if 
(
      keyboard_check
      (
          vk_left
      )
) 
{
        x -= 5;              
}
Edited by ByteTroll
2

Share this post


Link to post
Share on other sites
...snip...

As a bonus, all curly brace, whitespace, and indentation arguments are solved.

I think you have an incorrect definition for the word "solved".

0

Share this post


Link to post
Share on other sites

Since the 4 ifs are nearly identical, why not wrap this up in a function or something?

 

Ex:

void CheckKeyboard(int k, int *p, int i)
{
	if(keyboard_check(k))
		*p += i;
}

...

const int i = 5;
int x, y;

CheckKeyboard(vk_left,  &x, -i);
CheckKeyboard(vk_right, &x,  i);
CheckKeyboard(vk_up,    &y, -i);
CheckKeyboard(vk_down,  &y,  i);

I Should have used a better function name than CheckKeyboard but none came to mind...

0

Share this post


Link to post
Share on other sites

Since the 4 ifs are nearly identical, why not wrap this up in a function or something?

Ex:

void CheckKeyboard(int k, int *p, int i){	if(keyboard_check(k))		*p += i;}...const int i = 5;int x, y;CheckKeyboard(vk_left,  &x, -i);CheckKeyboard(vk_right, &x,  i);CheckKeyboard(vk_up,    &y, -i);CheckKeyboard(vk_down,  &y,  i);
I Should have used a better function name than CheckKeyboard but none came to mind...
Just get rid of the if's and use the result of keyboard_check directly:

x += (int)keyboard_check(vk_right);X -= (int)keyboard_check(vk_left);y += (int)keyboard_check(vk_down);y -= (int)keyboard_check(vk_up);
and bam, no more indentation/brace positioning problems. =-)
0

Share this post


Link to post
Share on other sites

 

Since the 4 ifs are nearly identical, why not wrap this up in a function or something?

Ex:

void CheckKeyboard(int k, int *p, int i){	if(keyboard_check(k))		*p += i;}...const int i = 5;int x, y;CheckKeyboard(vk_left,  &x, -i);CheckKeyboard(vk_right, &x,  i);CheckKeyboard(vk_up,    &y, -i);CheckKeyboard(vk_down,  &y,  i);
I Should have used a better function name than CheckKeyboard but none came to mind...

Just get rid of the if's and use the result of keyboard_check directly:

x += (int)keyboard_check(vk_right);
X -= (int)keyboard_check(vk_left);
y += (int)keyboard_check(vk_down);
y -= (int)keyboard_check(vk_up);
and bam, no more indentation/bracket positioning problems. =-)

 

If the language your using uses "1", "0", and "-1" for boolean that might work ( if you can deal with the "-1" ) ,  HOWEVER the increments the OP used are by 5, which throws a wrench in your example.

Edited by Shippou
0

Share this post


Link to post
Share on other sites

Do you check the keyboard or do you keyboard the check?

 

In Soviet Russia the keyboard checks you!  (...or maybe the check keyboards you, which sounds a mite painful...)

1

Share this post


Link to post
Share on other sites

 

 

Since the 4 ifs are nearly identical, why not wrap this up in a function or something?
Ex:

void CheckKeyboard(int k, int *p, int i){	if(keyboard_check(k))		*p += i;}...const int i = 5;int x, y;CheckKeyboard(vk_left,  &x, -i);CheckKeyboard(vk_right, &x,  i);CheckKeyboard(vk_up,    &y, -i);CheckKeyboard(vk_down,  &y,  i);
I Should have used a better function name than CheckKeyboard but none came to mind...

Just get rid of the if's and use the result of keyboard_check directly:
x += (int)keyboard_check(vk_right);
X -= (int)keyboard_check(vk_left);
y += (int)keyboard_check(vk_down);
y -= (int)keyboard_check(vk_up);
and bam, no more indentation/bracket positioning problems. =-)
If the language your using uses "1", "0", and "-1" for boolean that might work ( if you can deal with the "-1" ) ,  HOWEVER the increments the OP used are by 5, which throws a wrench in your example.

I did that from my phone, i meant to wrap the values by a *5 to do what the OP did, but we drove to a place that doesn't have service when i was trying to edit it. I also subtract to do the negatives.

Also, what sane language that allows bool to int casting doesn't represent the bool as 1 or 0?

 

edit: editted code that i was trying to change before i lost service:

x += ((int)keyboard_check(vk_right))*5;
X -= ((int)keyboard_check(vk_left))*5;
y += ((int)keyboard_check(vk_down))*5;
y -= ((int)keyboard_check(vk_up))*5;
Edited by slicer4ever
0

Share this post


Link to post
Share on other sites

 


Also, what sane language allows bool to int casting?

There, fixed that for you.

 

 

out of curiosity, are you being funny, or serious?  and if serious, for what reason?

0

Share this post


Link to post
Share on other sites

I think the point Swiftcoder is driving at is that bool->int, and int->bool conversion only makes (some kind of) sense inside of C's kind-of-insane interpretation of any non-zero integral type as true. I mean, it might be a neat party trick, but what would we really benefit by being able to multiply some integer by a boolean value? There's no actual relationship between the two types, except for the arbitrary rule that was applied.

2

Share this post


Link to post
Share on other sites

Which ends up in this
 

if ( var = 3 )
{
    //Do something.
}
being completely fine.

 

I might argue that issue is caused by another of C's idiocies...

Making assignment an expression, rather than a statement. Not a very good decision, in retrospect.

0

Share this post


Link to post
Share on other sites

I thought that the final integer value of var gets evaluated to true, instead of the assignment getting evaluated to true.

 

For example, in GCC 4.8, this code returns false:

if ( var = 0 )
{
   cout << "true";
}
else
{
    cout << "false";
}
0

Share this post


Link to post
Share on other sites

I thought that the final integer value of var gets evaluated to true, instead of the assignment getting evaluated to true.

 

Yes, that's precisely what I mean. In most other languages, the assignment operator is a statement (i.e. it doesn't result in a value at all, and you can't use it in a conditional).

 

Only in C/C++ can you write such monstrosities as:

if ( (y += 12)/5 > 10) { /*...*/ }
0

Share this post


Link to post
Share on other sites

Only in C and C++? If I recall correctly that was a common trait of the languages of the era. This was the reason why Pascal used := for assignment and not =, if I remember (= was used for conditions instead).

 

The reason for making it an expression was to allow stuff like x = y = z... I'm not sure that got much use in practice. Probably more used was to do an assignment within a condition and then check if the assigned value is true or not (which made sense especially with pointers, since null pointers evaluate to false, so this'd save a sentence).

 

But yeah, it'd be better if this was never allowed for starters.

0

Share this post


Link to post
Share on other sites

 

Is that legal syntax? I tried that in VS 2012 and it yelled at me.

It looks like it's not, at least according to section 6.4 of the C++ standard. It defines selection-statements as:

selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condition ) statement
 
The parentheses here are part of the definition of if and switch, so I would conclude that no, they are not optional.

 

 

Oh, it's perfectly legal. With function macro wrappers you can bend the syntax and remove the need to write unnecessary parenthesis.

extern bool keyboard_check(keycode);
#define keyboard_check(keycode) (keyboard_check(keycode))

if keyboard_check(vk_left) { … }
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0