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 «Previous 1 2
#1
05/10/2005 (2:39 pm)
I can't seem to get any code in here to explain my problem. So fuck it. I'm so annoyed that this bloody forum is not allowing me to explain myself.
#2
05/10/2005 (2:45 pm)
Http://www.garagegames.com/docs/tge/general/ch05s02.html


if you go to this page, you'll see a lot of script.

Some lines are spaced off differently. How do I type that in?

Thats what I want to know. do I TAB it or space it in.
#3
05/10/2005 (3:06 pm)
This is an example of the {code}....{/code} formatting block.

replace the { with [ and the } with ].
#4
05/10/2005 (6:52 pm)
Function OnExit()
}

   Parent::onExit();
}

ok. then see how the 3rd is over slightly. Is this important? And is this TABbed over or SPACEd over?

I'm new to this and what I've written so far, although its copied from the book, doesn't work.


EDIT: *sniff* thank you Stephen. I did click to learn how but it gave me an empty page.

This will make it a lot easier for me.
#5
05/11/2005 (2:24 am)
Tabs and spaces do not matter as far as TGE is concerned, although they do make your code more readable. Your problem looks to be having your first curly brace the wrong way round.

Function OnExit()
{ //<----- This was the wrong way 

   Parent::onExit();
}
#6
05/11/2005 (7:59 am)
Okay. I'll look for more of those.

Thanks guys
#7
05/11/2005 (8:35 am)
I have new found respect for you coders. I just learned how annoying it is to go through what you've written to find the one missing semicolon.

Argh.
#8
05/11/2005 (8:48 am)
He he he LOL... Us coders dont got it so bad. After about a week you start remembering almost every semicolon... Besides, usually the compiler tells you where it is missing (within about 3 lines) ... TGE's console does the same with it's ## markers
#9
05/11/2005 (10:44 am)
Once your eyes get better at recognising certain patters, writing code (and fixing it) becomes much easier. That and "touch typing". Actually looking at what you type as you type it is Very Helpful.

That and good coding habits. Being able to type about as fast as you can think comes in handy in a wide variety of ways. You're no longer so concerned with each and every key-press and can burn characters to improve readability without having an impact on coding time:

BEFORE:

for (int i = 0; i < n; ++i) {
}

AFTER:
for (int curVert = 0; curVert < numVerts; ++curVert) {
}

"curVert" and "numVerts" are much more informative than "i" and "n". Things like always adding {} to your 'if' statements means you'll never get bit by something like:
if (someBoolean)
  DoSomething();
  DoSomethingElse();

The above code WANTS DoSomethingElse to occur only when someBoolean is true, but that's not what actually happens. This sort of thing happens All Too Often when a loop or condition started off as a single statement that didn't need {}. Later, SomethingElse is added in that condition, but {} are often forgotten, leading to some very hard-to-spot bugs, particularly to the Untrained Eye.

Heck, I've made this sort of mistake myself, when maintaining code I didn't originally write. The above code looks remarkably like:
if (someBoolean) {
  DoSomething();
  DoSomethingElse();
}

But when DoSomethingElse() is called every time, not just when someBoolean is true, it can be difficult to figure out what went wrong. That's why I always try to step through new code at least once, it catches a lot of things that might otherwise hide out in your code for a while.
#10
05/11/2005 (10:48 am)
One thing that will help you IMMENSLY as well is tabbing properly (using large tab widths)... this makes it easy to line up code blocks to check your logic..

Just a note: this isnt to start a war over spaces and tabs... Some people use tabs, some use spaces instead...
#11
05/11/2005 (11:10 am)
@ Chris - I've been tabbing. So I guess I'll stick with it.

@ Mark - some of what you wrote still looks like latin to me! 8>0 Does i actually sub in for "curVert" and n for "numVert"? Hope that isn't a dumb question.

Cheers guys!
#12
05/11/2005 (11:13 am)
@Todd.. curVert and numVert can be treated as variables that get indexed each iteration of ht eloop... so the first iteration curVert would be 0... then 1, then 2, 3 , 4, 5... until it hits numVert - 1... You could have called them i and n and it would do the same hting... only it would be more difficult to read, especially if you came back to it in a year.
#13
05/11/2005 (11:27 pm)
I recommend tabbing for 4 reasons...

1. Efficiency - A lot of seasoned programmers use 3(some use 4) spaces. I use a single Tab, when they use 6 spaces, I use Tab Tab. They use their space bar 200% more than I use my tab button. What if you need to back up? Delete, backspace, left arrow, whatever method you use, you will have more spaces to navigate through ALWAYS, than I would Tabs to navigate. The natural solution would be to move manually with your mouse, which means you'll use your mouse to navigate far more than I will. Either way, speed is on my side.


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.


3. Standards - Some people say "We use a different OS and editor, and Tabs screw it up.", to which my response is "It's not up to the rest of the world to adapt to your OS and editor, it's up to your OS to adapt to the rest of the world, and you to use another editor. If 80% of the editors out there work with Tabs, don't ask us to conform to the ones that don't. And I drive this point home in #4.


4. Better options - My editor can convert all leading Tabs into spaces. As many spaces as I want. So if I have to send a file to a guy that is using the editor that gets screwed by Tabs, I can tell my editor to convert the leading tabs to spaces and in a split second the file is now compatible with those other editors and OSs. I have not yet seen an editor that will convert those leading spaces to Tabs, and if one does, it better do all the other cool stuff mine does or it's not helping. Also, I can specify in my editor how many spaces wide I would like my tabs to be. I suspect many of the others do this as well. Maybe you like your Tabs 4 spaces wide, maybe you like them 6, I like mine to be 8. Point is, I send you a Tab formatted file, and it looks the way you like it to look, you send one to me, and it looks the way I want it to look. Send us both one with spaces, and we both see it the way it was written and neither of us likes it.
#14
05/11/2005 (11:42 pm)
One formatting habit I hate is this...


if(something == something else)
{
      //  Do stuff here...
{
else
   if(this == that)
   {
           //  Do stuff here...
    }


if(that + that == bool)
{
    //  More stuff done
}


There is a BIG difference between "if/else" and "if/else if" and it can extremely difficult sometimes to tell which the author intended using the above drop line method. Especially if someone else has come along and altered it later. And most especially if you are learning, that could confuse the crap out of you. This is directly related to the same issue that Mark covered and IMO is a very bad formatting habit.

else and if should ALWAYS be either...

if(foo)
{

}
else
{
      if(footwo)
}


or


if(foo)
{

}
else if(footwo)
{

}
#15
05/12/2005 (4:19 am)
Gonzo... I agree with TABs... but not everyone does... Let him (Todd) choose what he likes... Dont want to highjack the thread with a SPACE / TAB war...
#16
05/12/2005 (7:27 am)
Side note for Gonzo:

Ultraedit can convert leading spaces to tabs and back, as well as auto-trimming trailing spaces when you save. It also does a lot of other "cool stuff", so it might fit your bill.

Todd:

Most compilers give you errors with a line numbers, so you don't have to scan through the whole code to find your missing semicolons.
#17
05/12/2005 (7:35 am)
Quote:1. Efficiency - A lot of seasoned programmers use 3(some use 4) spaces. I use a single Tab, when they use 6 spaces, I use Tab Tab. They use their space bar 200% more than I use my tab button. What if you need to back up? Delete, backspace, left arrow, whatever method you use, you will have more spaces to navigate through ALWAYS, than I would Tabs to navigate. The natural solution would be to move manually with your mouse, which means you'll use your mouse to navigate far more than I will. Either way, speed is on my side.

Thats would be a pretty crappy editor if that is how it worked. I use spaces for formatting and dont do any of what you have stated. My editor automatically uses spaces as tabs, so a single tab equals however many spaces I specify.

You mention backing up, if you are referring to reformatting chunks of text, a good amount of editors out there allow you to select text and then indent/unindent. Thats what I do and it take me all of one keystroke.

If you are talking about general navigation, if you are moving around via the arrow keys character by character you are wasting a ton of time. Again, most editors supports ctrl+arrow moving to move by words. This is what I mostly use while navigating lines and it is exactly as efficient whether you use tabs or spaces since they are both whitespace.

@Mark
I couldn't agree more with blocking off all if/else statements with brackets. It just makes the code a lot clearer.

One thing I have not seen mentioned that took me a while to get in the habit of but is of great use is setting up your equality tests so that you put your constant first if you have one and then put variable second. For example, instead of this:
if( some_variable == 0 )

Use this:
if( 0 == some_variable )

This solves the dreaded if( some_variable = 0 ) issue where you have just assigned your variable to 0. When you put the constant first, if you accidentally type if( 0 = some_variable) the compiler will throw an error since you are trying to change the value of a constant. I think most compilers catch this as a warning nowadays though.
#18
05/12/2005 (8:25 am)
I agree, tabs are your friend...

lol I'm a code neat freak, much like you are Gonzo. I have to have it well structured, well tabbed or it drives me nuts. It also saves huge ammounts of time later when trying to find bugs, re-read code, or picking up old code.
#19
05/12/2005 (8:35 am)
(using tabs)--and as soon as you add a new member to your project that works on a different platform, all those pretty tab defined whitespace formatting goes completely out the window, since their editor most probably doesn't view tabs the same way yours does.

Just about any experienced developer is going to agree that whitespace formatting is very important to code readability. However, the arguement over if you should use (hard) tabs or spaces (soft tabs) is one that will never end. IMO, since we are all using TGE, it would be best to adopt your own personal standard to that of the engine itself, which is soft tabs/spaces to construct your formatting.
#20
05/12/2005 (8:38 am)
@Todd: Nice point. Took me a second to realize you were talking about the problem of typing one = instead of two, but that's a great point.
Page «Previous 1 2