Wayback Machine
MAR Oct JUL
Previous capture 27 Next capture
2005 2006 2007
9 captures
9 May 03 - 15 Jul 07
sparklines
Close Help
  home · browse · search · game entities · user directory · message board · IRC | register

October 27, 2006, 4:28 pm PDT
username  
password  
forgot password?

Popular Resources
  • Half-Life 2 Mod FAQ
  • Valve Hammer Editor
  • Hammer 3.5 beta test
  • Half-Life Utilities
  • game data files
  • ZHLT 2.5.3 custom build
  • Half-Life SDK
  • Feedback
    If you've got any feedback, suggestions, or bugs to report regarding the Collective website, go here!

  • Feedback (301)
  • Newsletter
     
    Enter your email address in the above form to add yourself to the email newsletter list. Click here for more info.

    Hosted Sites
  • Valve ERC
  • Collective
  • TFMapped
  • Spirit of Half-Life
  • Selective Design
  • Pixel Reviews
  • recent articles

    NPC and Item Placement Theory
    17/03/05 11:35pm PST
    Non-Player Character (NPC) and item placement can influence both the gameflow and immersion of a level. This article aims to give some pointers on how to properly place them.
    - Hugh 'Hugh' Lloyd

    Got Props?
    13/03/05 08:32am PST
    A common problem in HL2 mapping is props not showing up in game. This article explains why and offers solutions.
    - Jeff 'Yesukai' Pritchard

    Simulating Randomness
    18/12/04 11:29pm PST
    This article focuses on how to properly simulate random events that should occur at a certain average frequency, or within a certain probability per period of time.
    - Skyler 'Zipster' York

    Adding Single-Player Weapons to Half-Life 2
    15/12/04 06:52pm PST
    Covers the process behind adding weapons to a single-player Half-Life 2 modification.
    - Skyler 'Zipster' York

    Your world in HL2
    06/12/04 12:17am PST
    This article gives tips and advice to anyone wanting to make custom photorealistic textures to be used in Half-Life 2.
    - Oksid

    Hiding in Shadow
    21/08/04 01:11pm PDT
    Describes how to create a function that has monsters disregard you if you are hiding in a certain level of "darkness," which can be set from within map properties.
    - Anders [Wolf] Jenbo (NoBody)

    XSI EXP for Half-Life 2 Tutorial - Camera Control
    23/09/04 12:43am PDT
    A SOFTIMAGE|XSI tutorial explaining all of the camera controls available to you in XSI!
    - Josh Enes

    Bump Mapping in Half-Life
    08/08/04 11:58am PDT
    Details a method of achieving real-time bump mapping in Half-Life, and provides an implementation of the algorithm.
    - Francis 'DeathWish' Woodhouse

    Real-Time "TRON 2.0" Glow For Low-Spec Hardware
    19/06/04 02:06pm PDT
    A sequel to the original "Real-Time 'TRON 2.0' Glow" article, this describes how to implement real-time glow that works on low-spec graphics cards.
    - Francis 'DeathWish' Woodhouse

    Hitboxes and Code
    05/06/04 06:25pm PDT
    How do I make only one part of a monster take damage? Learn about the relationship between model hitboxes and what you can do with them in a characters code.
    - Jonathan 'Teh_Freak' Smith

    Coding Style
    [Fri Nov 15, 2002 / 12:12pm PST] Philip 'Cecil' Nilsson - comments (9) comments enabled

    Have you ever seen anything like this?

    class AbC {
    protected:
    private:
    public:  AbC();
    int A(int); };
    AbC::AbC() {return;}
    int AbC::A(int c){
    int b;
    for(int a=0;a<c;a++) b++;
    return b;
    }
    
    Not that easy to understand as something like this, eh?
    class CAbc
    {
        public:
            CAbc();
            int A(int count);
    };
    
    CAbc::CAbc()
    {
        return;
    }
    
    int CAbc::A(int count)
    {
        int b;
        for (int a = 0; a < count; a++)
        {
            b++;
        }
        return b;
    }
    
    Ask yourself now: how can I write code as nice as that?

    There are a few things to think about to make your code look nicer and to make it more easy to read:

  • Indenting

    Indenting looks really nice and also helps alot when trying to find missing brackets ;) The most standard indention I can think of is four spaces. Tabs are not that good as they are different on most systems. Tabs are ok though, but I'd rather stick to spaces. Take a look at this:

    int a,b;
    if (a == 0){
    b = 2;
    for (int c = 0; c < b; c++){
    b++;
    }
    } else if (a == 2) {
    b = 3;
    }
    versus
    if (a == 0)
    {
        b = 2;
        for (int c = 0; c < b; c++)
        {
            b++;
        }
    }
    else if (a == 2)
    {
        b = 3;
    }
    

  • Spacing

    By spacing I mean adding spaces between variables, operators, constants and wherever else you can add some spaces in to improve readabilty. Take a look at this example:

    int a=3;
    printf("a%sasd",string);
    int a=2+3.4-4;
    
    and the good one ;)
    int a = 3;
    printf ("a%sasd", string);
    int a = 2 + 3.4 - 4;
    

    As you can see, I added some spaces around the = operator, some spaces should be added around all operators, by the way, as I did to the expression underneath ;) I also added some spaces between the function and it's arguments. That looks good but doesn't really help -- you can skip it if you wish. Adding a space after every comma (",") is smart as it keeps the different arguments apart so you won't miss one.

  • Variable names

    Naming your variables right really helps during devlopment. For example, if you have a variable somewhere in the code called fubar, you'll have no idea what it could be. If it instead was called iPlayerCount, you would immediately see that it was an integer that counts the players. The question is: How do I name my variables in a good way? One of the more popular naming conventions is Hungarian Notation. Learn more about it here.

  • Comments

    Commenting your code is useful if other people are working on the project. It also helps if you're going back to an old project you don't remember much about. You should put comments everywhere that there are big ugly chunks of code that were hacked together. Most functions should also have a little description. Having a description of the file at the top of all files is good for people new to your project. The function descriptions will also help them. Commenting ugly code is good, but it's better to rewrite it so that it's more understandable. It might not be possible in all cases, so commenting is good :)

    Relevant Links:
    Some Hungarian Notation over at MSDN
    Some Brief Hungarian Notation
    PEAR Coding Standards(PHP)

    This article was written with C++ in mind, but all languages that look like C will probably work. I also had PHP in mind while writing it.

  • article created on Thu Nov 14, 2002 / 12:45pm PST
    this item has been viewed 2377 times
    [general / coding]

    Only registered users can post comments. Have you registered yet?

    user comments

    displaying comments of normal or higher rating

    1.

    Ryan "Professional Victim" Desgroseilliers
    Fri Nov 15, 2002 / 08:58pm PST

      Tabs are not that good as they are different on most systems.

    Gotta disagree there -- the visual tab length might be different depending on the system, but indent-spacing is quite a menace if your code editing software doesn't support it natively (or you're trying to modify someone else's code, or you're not using your preferred editor, or whatever). Having to delete three to eight spaces per line to drop the indent level by one is a very inefficient, time-wasting process.

    This is one of those Holy War subjects, I guess; I hate to bring this age-old debate up again but the article did that already.

    2.

    Chris 'autolycus' Bokitch
    Fri Nov 15, 2002 / 09:06pm PST

      I have to agree -- the first thing I do with code that has space indenting is convert everything to tab indents.

    3.

    Philip 'Cecil' Nilsson
    Sat Nov 16, 2002 / 03:46am PST

      Well.. I like spaces, I always convert all tabs to spaces. One of my text editors support space-tabs too. I guess that spaces take four times more space in the source, but what does it matter in the finished code? I know that you can set the tab length in most progams too. I use spaces because it will allow me to make multi-line expressions look better, with tabs I can't be sure as how it will look. Having the tab width set to eight or so doesn't look good either.
    Have a look at this:(I like examples :p)


        if (this | that | another | one | oops | changeline |
    more | stuff | bleh)
    ...

    I guess that doesn't look good without PRE ... but I'll explain instead.
    In that case I want to have the rest of the stuff that doesn't fit on the line as it will be too long, long lines are not good. I want it to continue right after th ( on the above line, in this case it is at position 8, just a coincidence. If I used tabs it might start at position 16 or even further away if there are a lot of tabs...
    Hmm just thought about another reason as to why not use tabs, you can't use tabs in text forms(at least in iexplore and as far as I know).

    Sorry for bringing this up ;) I guess it is up to your personal preference.
    comment modified on Sat Nov 16, 2002 / 08:45am PST

    4.

    Chris 'autolycus' Bokitch
    Sat Nov 16, 2002 / 06:50am PST

      So, you space-indent to just beyond the opening ( in a structure? See, that seems to go against the rest of your formatting advice as it's a non standard thing. In multiple-line expressions, I generally tab one tab inward, regardless. For example:

    if ( ( something ) ||
         ( something else ) ||
         ( something else )
         )
    {
         ...
    }

    elseif ( ( something ) ||
         ( something else ) ||
         ( something else )
         )
    {
         ...
    }

    else
    {
         ...
    }

    I prefer the above method as everything tends to line up correctly. (The formatting has converted my tabs to space equivalents, but each indent is one tab length.)

    (note:
    * tabs can be pasted into forms
    * i added the pre tags into your post -- i don't know why i didn't make those available before. one thing to note about them, long lines within a pre tag will break the site format
    * don't be sorry - debate is good. :)
    )
    comment modified on Sat Nov 16, 2002 / 06:51am PST

    5.

    Philip 'Cecil' Nilsson
    Sat Nov 16, 2002 / 09:01am PST

      I would be happy to know if there is a 'standard' for these cases. They do not happen that frequently anyways, the conditions would most of the time be broken into multiple ifs to handle all cases. Your style has some advantages, if you are already far to the right, something like 40 characters(10 ifs ;)?) then it would probably be smarter to just do one indention.

    By the way, I had a bad tab experience of the third grade today. I had been using MSVC++ for one of my PHP files. When I opened it in notepad(Note to self: Get the other program I use on my crappy computer over to this one) it didn't look really good at all ;) luckily MSVC++ only transforms spaces to tabs where there are if-blocks and such. When I looked for an alternate solution I noticed that you could not change the tab length in notepad(microsoft :p), it was stuck at 8, just the one I don't like :( I guess there are a few good things about notepad though ... not that I can rember them now though :/ It might be that you can hold shift while pressing delete, a really good thing.

    6.

    Chris 'autolycus' Bokitch
    Sat Nov 16, 2002 / 09:27am PST

      I would say the standard would be using tabs rather than spaces, just going by the fact that when you get MSVC++ to auto-format your code (Edit > Advanced > Format Selection) it uses tabs rather than spaces. All of the SDK code is also tabbed, I believe. This may simply be Valve standard, but still ...

    And ... notepad!!?? =:o ... hehe, have you used EditPlus? As far as text editors go, I haven't found anything I like better. For Perl and PHP editing, it's spectacular. It has built in syntax highlighting, and you can hook the PHP .chm help file (available from php.net) right into it -- I can get context sensitive PHP help simply by pressing F2 now. I love it! :)

    7.

    Caleb 'Ghoul' Delnay
    Sat Nov 16, 2002 / 10:23am PST

      I'd have to do with tabs on this too. For one, MSVC++ automattically uses tabs as far as I know, and it has auto format to tab whatever I need tabbed. And pushing delete once to move back a block is a lot easier than pushing it 4 times as far as I'm concerned. Overall I just think it looks nicer.

    8.

    Skyler 'Zipster' York
    Sat Nov 16, 2002 / 12:04pm PST

      For those who might happen to frequent the GameDev.net forums, coding style posts always turn into heated debates, primarily becase so many people have so many different opinions, and of course, everyone thinks they're right and everyone else is stupid :) At least that's what I noticed at GDNet. People here are a lot more civilized ;)

    So being Just Another Programmer, I feel obliged to contribute. Joy.

    Indenting
    I really don't know many people who would prefer using pure spaces over tabs. Tabs are fast and easy, and they allow you to easily format your document, because all tabs on every line are aligned inherently. Now, if you need that extra space to make a bunch of conditional statements line up in an if statement, then by all means go ahead. But using spaces purely instead of tabs is a bit of a hassle. That, and MSVC automatically tabs for you when needed :)

    Spacing
    Always a Good Thing(TM). Some people prefer single-spacing, others prefer tab spacing. I believe that tab spacing creates a little too much space between stuff, so I personally use spaces.

    Variable Names
    Oh boy. This is where the heart of the argument usually lies. A lot of people use Hungarian notation because they can easily recognize the type of their variables. Others argue that this only obfuscates your code - 1) descriptive variable names should be indicitive of type, 2) MSVC can automatically tell you the type of a variable just by waving your mouse over it, 3) It goes against object-oriented design and encapsulation (don't ask). I myself use limited Hungarian notation for things such as class members and global variables... you'd have to talk to someone who hates Hungarian notation to get a better picture. Just remember that there are many experienced programmers on both sides of the line.

    Comments
    Another Good Thing(TM), if used in moderation. Commenting as shown below:
    // Assigned 4 to the integer variable myVar
    int myVar = 4;

    Is usually not appreciated, because it actually makes the code harder to read. Things such as simple variable declaration, assignments, incrementing/decrementing, usually don't need comments unless they are key in understanding the code or algorithm as a whole. On the other hand, something like this:
    int value = (++i & (q + 17)) / (5 * ++h);

    Without comments will surely get you killed.
    comment modified on Sat Nov 16, 2002 / 12:06pm PST

    9.

    Snarkman
    Mon Dec 02, 2002 / 06:16am PST

      I can only code in MS Visual Basic for Applications (which means I could probably have a fair stab at full VB), so a lot of the conventions are forced upon me. However, I will add my preferences to the debate:

    Tabs for indentation = good. Simple to use, very clear. Interesting, VBA's tab actualy places 4 spaces by default, rather than 1 tab caracter, so I guess it pleases everyone. VBA also has a line continuation "character": "space + underscore"; the following line can be indented with TAB - I don't know whether regular text editors support this, but it's a useful feature.

    Spacing is essential. It just makes the code so clear. Cecil is absolutely right.

    Naming variables usefully = good. And it's something I am woeful at. My code tends to fill up with references to "n" (for every loop) and "a" (for every response from a dialog box. When I come back to an application to debug, improve or copy the code, I kick myself every time for not having made the variables clearer.

    Thank God for comments. With my terrible variable names, comments are a lifesaver. I 'm pretty good at this, and it so useful to have each chunk of code explained, even "headings" like Document formatting begins here... Sometimes you don't want/need to read the code, you just want to skim through and follow the general process, or find a particular part of it. Just commenting an if-then statement to make it clear that if fubar = 1 is true then it means that a certain option is selected, is invaluable.

    VERC © 2004. All content copyright its respective owner, all rights reserved.
    script execution time: 0.289890766144 seconds