Game Development Community

Help me write code

by Todd McCullough · in Technical Issues · 05/10/2005 (2:22 pm) · 34 replies

How do I type this line in correctly? ( I can't seem to enter code here without getting a forbidden page.

line of code
line of code
line of code

Am I supposed to hit TAB for lower lines of code or do I press space bar a certain number of times. Does this matter at all?

Does each .cs file require the Header info for it to work correctly? Such as date, author and so on?
Page«First 1 2 Next»
#21
05/12/2005 (8:46 am)
Thanks guys.

Tab it is.

Gonzo I'm a little confused with your if/else stuff. I'm afraid I really am a noob when it comes to this sort of thing. I've never taken any kind of computer programming course, not even in High School. And I've just started to read the book I mentioned.
If I am going to have an if argument in an else argument I should always keep it on the same line?

Not sure if I should be calling them arguments. Please correct me anyone.

Thanks for all the help everyone.
#22
05/12/2005 (8:57 am)
@Todd

What gonzo is stating with the if/else information is that when using the "else if" statement you should not put the if on another line. Look at the last example code block he posted for usage of "else if"
#23
05/12/2005 (9:36 am)
If you use tabs I suggest keeping in mind what Gonzo said

Quote:4. Better options - My editor can convert all leading Tabs into spaces.

That can allow you to use tabs and still not screw up any teammates lol
#24
05/12/2005 (10:05 am)
I'm a huge fan of making similar things be in columns.
- maybe because i did a lot of assembly in my formative years,
and i really wish the rest of the world were too.

it makes it so much easier to just visually glance at a block of code
and see if it's correct without actually having to read it.

try quickly looking for the update function in each of these examples
and determining what parameters get passed to it.

eg

// bad:
int RenderInit();
void RenderGo();
unsigned char* RenderUpdate();
int RenderDeinit();

// better:
int             RenderInit();
void            RenderGo();
unsigned char*  RenderUpdate();
int             RenderDeinit();

// best:
int             RenderInit    ();
void            RenderGo      ();
unsigned char*  RenderUpdate  ();
int             RenderDeinit  ();

i've noticed that microsoft has started formatting directX example code this way, hooray.
#25
05/12/2005 (10:10 am)
LOL, I was just about to type "@ Todd - " when I realized there were two of you, so...


@ Todd D. - That is correct. And about the Editor, mine does all the Auto formatting as well. Many people still do not use fancy editors though. In fact, here are some vets who still use NotePad..

Pat Wilson ~ Employee

Chris Gatterbauer says "Real Men Use Notepad"

Jeff Tunnel (recommending NotePad)

Jeff Tunnel (recommending NotePad Again)

Devon Ly ~ Associate

So you can assume that if experienced coders are still using NotePad that a LOT of newbies are using it, or something as basic as it, as well. And you and I would run circles around them. About moving an entire word using CTRL. How do you move exactly to the center of 16 spaces? I use one arrow right or left depending on the direction. If it takes you more than one key or your mouse, it's costing you more time and effort.
#26
05/12/2005 (10:11 am)
@ Todd M. - If you think it's confusing in my example, imaging you're trying to debug a large section of code that has one of those thrown in. Let me set another couple of examples...


The following examples SHOULD operate exactly the same...

Example A:
if(someBoolean)
    DoSomething();
else
    DoSomethingElse();

 
 
if(someBoolean)
{
    DoSomething();
}
else
{
    DoSomethingElse();
}


And these examples should perform the same(BUT Differently than Example A)
Example B:
if(someBoolean)
    DoSomething();
else if(someFoo)
    DoSomethingElse();
else
    DoThisInstead();
 
 
if(someBoolean)
{
    DoSomething();
}
else if(someFoo)
{
    DoSomethingElse();
}
else
{
    DothisInstead();
}


Every "if" does NOT have to have an "else" or an "else if". So knowing which an author intended when he uses...

if(someBoolean)
    DoSomething();
else
    if(someFoo)
        DoSomethingElse();
    else
        DoThisInstead();


Can be a real nightmare. Can you tell which example the above would perform like? Another one I hate is...

if(someBoolean)
    if(someFoo)
        if(someOtherFoo)
            if(ThatFooToo)
                DoSomethingElse();
    else
    {
        DoThisInstead();
        ThenDoThis();
    }
#27
05/12/2005 (10:19 am)
Lol gonzo -

i think you cut-n-pasted wrong, examples A and examples B are identical !
edit: naturally you edited it before my post. (or i'm an idiot.) (or both.)


i think there's nothing happier in the world
than a programmer telling how to format code.

well maybe a programmer being noticed by a girl.
#28
05/12/2005 (10:29 am)
Though I don't fall under the category of "vet"... I'm one of those notepad coders...

good ole notepad/wordpad is great :)
#29
05/12/2005 (12:43 pm)
@ Orion - your best example is a lot easier to read. I like it.



@ Gonzo - I'll try not to be a dumbass and write bad code. :)

After looking at the examples am I safe in assuming that sometimes { and } are used simply to make things easier to read?
#30
05/12/2005 (9:43 pm)
If everybody lied to you and said "Every 'if' and 'else' MUST have { and } to encapsulate their statements", that would be great. So why wont they? Because if you are only declaring one statement you do not have to have { and }, the next statement and ONLY the next statement will be assumed.

In the engine you'll see a lot of...

if(!obj)
        return false;

for example. Those should almost always be on the same line as a matter of good habit. I prefer this way...

if(!obj){ return false; }


but would be satisfied with...

if(!obj) return false;


Just to throw a curveball, 'for' loops are the same way. So you are likely to see something similar to this....

if(someBoolean)
    if(someFoo)
        for(int i = 0; i < n; ++i)
            if(ThisFoo[i])
                DoSomethingElse(ThisFoo[i]);
    else
    {
        DoThisInstead();
        ThenDoThis();
    }

Naturally I do not recommend coding this way.
#31
05/12/2005 (9:47 pm)
Quote:good ole notepad/wordpad is great

I could think of a few hundred reasons you should be using TextPad4. Download the free trial, see how long the free trial lasts(Hint - Longer than you will live).
#32
05/12/2005 (9:48 pm)
I wrote a manifesto a few years back,
if anybody wants to plumb the depths of hubris.
elenzil.com/orion/progs/bombastic.html

.. i don't agree with some of it any more, but 90%, yeah.
#33
05/13/2005 (9:57 am)
Now look what I did. Sheesh.

Lemme strap on my crusader's gear, draw my text-blade, and wade into the holy war that is Coding Style:

Everyone has their own coding style. Some of us have lived through several different coding styles inflicted on us at work. Having done that very thing, I've learned something:

What your coding style requires isn't nearly as important as THAT IT'S THERE (and actually used). tabs or spaces, where the {} goes, all that... A lot of it boils down to personal preference of who ever was working on the project when it got off the ground, and everyone after was "encouraged" to stick to their style.

Once you have a consistent coding style in place, your eye becomes trained to read more efficiently in that style. That's the real benefit. Most of the rest of it is just fluff.


Not to say that there are certain habits that can reduce bugs (like always using {} so code added later stays inside the block where it belongs). Some of these are less valuable than others. I think always using {} is a good idea, but others may disagree (and have in this thread, though I personally DESPISE "if(x){foo();}" in line).


One other thing someone mentioned:
Quote:
Gonzo T. Clown (great name BTW) said:
2. Neatness - Tabs want to line up naturally. Spaces have to be lined up manually and they don't always line up because of a font's variable width. A lot of us like to "format" our code depending on the block of statements being used. So if you are a formatter, Tabs are the way to go.

There's a great argument against NOTEPAD right there. Code should ALWAYS be in a fixed-width font. Using an editor CAPABLE of doing otherwise strikes me as a Very Bad Idea.

Tabs vs Spaces: I'm a spaces guy. You will be too once you get someone's code from another editor into your own. Tabs in some apps go to specific columns, tabs in others are a fixed number of spaces (2 to 8, VASTLY DIFFERENT). With spaces, and a fixed-width font, your code looks the same to everyone, and everyone can merrily type away at their editor of choice.

Doesn't matter for a single person working on their own... makes a huge difference on a team, particularly when the code standard asks for things all lined up nice-n-neat. "looks great" with "tab=4 spaces" looks GAWDAWFUL when "tab = 2 inches from the left".



Where The {} Go:
There's a bunch of options:
A:
if (x) { // what I do at work
  foo();
}

A2:
if (x) {
  foo();
  }

B: 
if( x ) // personal fave
  {
  foo();
  }

C:
if(x)
{
  foo();
}

Z:
if(x){foo();bar();baz();} // <-- shoot this person on sight

All this whitespace stuff is just so you and your fellow human beings can better understand what the code does. To the compiler, 'Z' is just as readable as the others. To your fellow programmers (and to YOURSELF in 3 months when you've forgot what it was you were thinking when you wrote this mess) all that whitespace [and comments and descriptive variable names and all the rest] ARE CRITICAL to their understanding the code in a reasonable amount of time.

Anyone can sit down with some code and a copy of the language spec (given enough time and patience) and figure out What Is Going On. But doing so with ones sanity (and hair) intact requires some degree of Readable Code.
#34
05/13/2005 (2:10 pm)
Thanks Mark. I see I kind of reopened maybe an ages old flame war. :).
I hope I understand this stuff in 3 months.
I just bought JavaScript For Dummies. mainly for Web pages and After Effects but I would think it could be useful here too.

Cheers Everyone!
Page«First 1 2 Next»