Jump to content

  • Log In with Google      Sign In   
  • Create Account


My OLD Syntax

  • You cannot reply to this topic
83 replies to this topic

#41 slicer4ever   Crossbones+   -  Reputation: 3518

Like
0Likes
Like

Posted 06 August 2013 - 02:02 PM

 

 

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, 06 August 2013 - 02:56 PM.

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

Sponsor:

#42 swiftcoder   Senior Moderators   -  Reputation: 9860

Like
1Likes
Like

Posted 06 August 2013 - 02:10 PM


Also, what sane language allows bool to int casting?

There, fixed that for you.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#43 slicer4ever   Crossbones+   -  Reputation: 3518

Like
0Likes
Like

Posted 06 August 2013 - 03:00 PM

 


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.

#44 Ravyne   Crossbones+   -  Reputation: 7120

Like
2Likes
Like

Posted 06 August 2013 - 03:29 PM

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.



#45 TheChubu   Crossbones+   -  Reputation: 4118

Like
0Likes
Like

Posted 06 August 2013 - 05:37 PM

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


#46 swiftcoder   Senior Moderators   -  Reputation: 9860

Like
0Likes
Like

Posted 06 August 2013 - 05:48 PM

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 - Software Engineer @Amazon - [swiftcoding]


#47 TheChubu   Crossbones+   -  Reputation: 4118

Like
0Likes
Like

Posted 06 August 2013 - 06:55 PM

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


#48 swiftcoder   Senior Moderators   -  Reputation: 9860

Like
0Likes
Like

Posted 06 August 2013 - 07:06 PM

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 - Software Engineer @Amazon - [swiftcoding]


#49 Sik_the_hedgehog   Crossbones+   -  Reputation: 1608

Like
0Likes
Like

Posted 06 August 2013 - 07:37 PM

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.

#50 Felix Ungman   Members   -  Reputation: 1005

Like
0Likes
Like

Posted 07 August 2013 - 01:16 AM

 

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


#51 patrrr   Members   -  Reputation: 986

Like
0Likes
Like

Posted 07 August 2013 - 01:55 AM

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

 

Though, without assignment as a value you wouldn't have this beautiful pattern:

while (char *line = read(...)) {
   ...
}

It's pretty much poor man's Maybe and I sorely miss it in Java.


Edited by patrrr, 07 August 2013 - 02:00 AM.


#52 Luckless   Crossbones+   -  Reputation: 1766

Like
0Likes
Like

Posted 13 August 2013 - 11:42 AM

 

If I'm going to use curly braces, then I want the open and closed characters nicely lined up vertically. Makes things nice and neat by grouping things in blocks.

 

Well, if you insist:

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

if (keyboard_check(vk_right)) {
     x += 5;
                              }

if (keyboard_check(vk_up)) {
     y -= 5;
                           }

if (keyboard_check(vk_down)) {
     y += 5;
                             }

laugh.png

 

Okay, this is scary, but I think that actually might work and be rather readable... Assuming you could set it up to automatically add white-space so the open/close for the outer level is always to the right of the open/close of the nested inner set.


Old Username: Talroth
If your signature on a web forum takes up more space than your average post, then you are doing things wrong.

#53 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 13 August 2013 - 11:49 AM

 

 

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

That's not the same thing, though. You're talking about code before the preprocessor runs, and we're talking about code after the preprocessor runs (or stays the same after the preprocessor runs).


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#54 DemonRad   Members   -  Reputation: 290

Like
0Likes
Like

Posted 16 August 2013 - 05:00 AM

 

and mine:

#define IF if(
#define THEN )
#define DO {
#define ENDIF }
#define ELSE }else
 
IF keyboard_check(vk_left) THEN
DO
        x -= 5;
ELSE IF keyboard_check(vk_right) THEN
DO
        x += 5;
ENDIF

 

do not undervaluate the power of this. You get automatic error-correction for mistypings and wrong capital letters

#define IF if(
#define iF if(
#define Iff if(
#define If if(
//...
IFFF condidion ISTRue   //expands to "if(condition==true){"

sleep.png

 

I'm guessing if something like that is allowed:

#define while for

Edited by DemonRad, 16 August 2013 - 05:01 AM.

Peace and love, now I understand really what it means! Guardian Angels exist! Thanks!


#55 wodinoneeye   Members   -  Reputation: 753

Like
0Likes
Like

Posted 19 August 2013 - 06:47 AM


The one I dislike is the start btrace at the end of the if test and the 
other hanging out by itself at the end of the block




if (x & y & funcz(a)) {
// do shirt here
}
My usual syntax Ive used :


if (x & y & funcz(a))
   {
   //do shirt here   --- note triple blank indent
   }

when its not a single statement in the if-true code block



I will do    if ()     plus single statement (and align them).... when they are simple


if      (x < 0 )        x = 0;
else if (x > maxx)      x = maxx;
if      (y < 0 )        y = 0;
else if ( y > maxy)     y = maxy;



I also add extra blanks  in many places to make the code less of a run together clustercluck and even split and align if clauses
(funny I usually dont do some for simple 'for'  control clauses)


if ((test1() == 3)    &&
    (test2() == 4)    &&
    (test3() == 7))
   {
   // do shirt
   }


Im also liberal adding blank lines particularly around For loops to accentuate their groupings


comments I dont mind being on the same line as code


for (o=0; o<maxi; o++)     // loop thru all objects
   {
   object[o].x = tx;
   object[o].y = ty;
   // etc....
   }


Ive rarely ever use the   ? :     statement form as they disappear in clutter making it hard to visually scan code 


//-------------------------------------------------------------------------

and I use lines like this between EVERY function definition to help spot the startpoints (again speeds up visually scanning code )

Edited by wodinoneeye, 19 August 2013 - 07:01 AM.

--------------------------------------------Ratings are Opinion, not Fact

#56 swiftcoder   Senior Moderators   -  Reputation: 9860

Like
1Likes
Like

Posted 19 August 2013 - 07:17 AM

if (x & y & funcz(a)) {
// do shirt here
}

That conditional almost certainly doesn't do what you meant it to do tongue.png

Hint: bitwise & is not the same as logical &&.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#57 Sik_the_hedgehog   Crossbones+   -  Reputation: 1608

Like
0Likes
Like

Posted 19 August 2013 - 09:35 PM

For the record, I know at least one language where it is indeed the case (it only pays attention to the lowest bit instead of the whole value).


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.

#58 Bacterius   Crossbones+   -  Reputation: 8559

Like
0Likes
Like

Posted 19 August 2013 - 10:30 PM


For the record, I know at least one language where it is indeed the case (it only pays attention to the lowest bit instead of the whole value).

 

But it wouldn't be bitwise anymore, then, right?


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#59 TheChubu   Crossbones+   -  Reputation: 4118

Like
2Likes
Like

Posted 20 August 2013 - 01:05 AM

It would be a "single bit bitwise" operator :P


"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


#60 swiftcoder   Senior Moderators   -  Reputation: 9860

Like
0Likes
Like

Posted 20 August 2013 - 04:25 AM


For the record, I know at least one language where it is indeed the case (it only pays attention to the lowest bit instead of the whole value).

I mean, provided that the binary representation of true and false are exactly 0x1 and 0x0, then they will produce the same result.

 

Unfortunately, C/C++ are a little flexible in their definition of what constitutes a boolean value...


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]






PARTNERS