Followers 0

# 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 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 on other sites

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

0

##### 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 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

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

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 on other sites

...snip...

...snip...

Thanks for the laugh!

0

##### 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 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 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 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 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 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 on other sites

Also, what sane language allows bool to int casting?

There, fixed that for you.

1

##### 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 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 on other sites

Which ends up in this

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

##### 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 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 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 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 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

## Create an account

Register a new account