My OLD Syntax

Started by
79 comments, last by 21st Century Moose 10 years, 6 months ago

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;
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
Advertisement


Also, what sane language allows bool to int casting?

There, fixed that for you.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


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?

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

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.

throw table_exception("(? ???)? ? ???");

Which ends up in this

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

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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";
}

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

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) { /*...*/ }

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

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) { … }

openwar - the real-time tactical war-game platform

This topic is closed to new replies.

Advertisement