Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411978 Posts in 69438 Topics- by 58486 Members - Latest Member: Fuimus

June 15, 2024, 06:35:13 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Coding styles
Pages: 1 ... 3 4 [5] 6 7
Print
Author Topic: Coding styles  (Read 18963 times)
shrimp
Level 5
*****


View Profile WWW
« Reply #80 on: March 02, 2010, 06:08:48 AM »

This can be annoying, but I've also seen people take this way too far in the other direction, basically trying to never use a raw value.  Sometimes a number really is just a number.

True, #define NINETY 90 and that sort of thing. Seems to crop up fairly often on www.thedailywtf.com Smiley
The example code was something like a UI state machine, since it wasn't obvious.
Logged

Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #81 on: March 02, 2010, 06:28:08 AM »

Lots of good stuff in this thread! The coding standards that are designed to avoid errors are the only ones that really matter; I suspect there will never be a language where these are not necessary any more.
Logged

Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #82 on: March 02, 2010, 07:16:31 AM »

The coding standards that are designed to avoid errors are the only ones that really matter
I'd like to amend that by saying "code standards that are designed to avoid errors and promote code pedagogics are the only ones that matter".

I know of a place where the programmers were forced to centre-align their class member declarations around the return type and function name, something like this:
Code:
class MyClass
{
                                    void method1():
                            unsigned int method2( int, int, char );
                             std::string methodThatReturnsString();
};
Apparently their employee turn-over rate was quite high.
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Kekskiller
Guest
« Reply #83 on: March 02, 2010, 07:32:11 AM »

I know of a place where the programmers were forced to centre-align their class member declarations around the return type and function name, something like this:
Code:
class MyClass
{
                                    void method1():
                            unsigned int method2( int, int, char );
                             std::string methodThatReturnsString();
};
Apparently their employee turn-over rate was quite high.

A better variant is:
Code:
class MyClass
{
  void         method1():
  unsigned int method2( int, int, char );
  std::string  methodThatReturnsString();
};
It's often useful to align your code words, but centre-aligning is just silly.
Logged
st33d
Guest
« Reply #84 on: March 02, 2010, 09:47:57 AM »

I went for a scripter's role once for Jagex on a RuneScape spinoff they were working on (and still haven't released 4 years later).

The RuneScape in-house scripting language couldn't handle white space, had no "else" keyword and would not accept more than one test in the "if" parameter call. And that was just for starters.

I was so glad I failed to get the job.

How hard is it to write a decent fucking parser for chrisakes?
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #85 on: March 02, 2010, 12:46:49 PM »

The RuneScape in-house scripting language couldn't handle white space, had no "else" keyword and would not accept more than one test in the "if" parameter call. And that was just for starters.
This is what happens when someone needs a parser, and things regexes would be a good tool for the job.
Logged
increpare
Guest
« Reply #86 on: March 02, 2010, 12:56:06 PM »

The RuneScape in-house scripting language couldn't handle white space, had no "else" keyword and would not accept more than one test in the "if" parameter call. And that was just for starters.
This is what happens when someone needs a parser, and things regexes would be a good tool for the job.
oi now! none of that - regexs can handle whitespace just fine  Wink
Logged
shrimp
Level 5
*****


View Profile WWW
« Reply #87 on: March 02, 2010, 12:58:52 PM »

It's also the actual implementation of the language, not just the parsing.

But really you've got to wonder why they didn't just integrate Lua or something similar. Well, you'd wonder that these days. I think homegrown (and crap) scripting languages had their Golden Age about 4 or more years ago.
Logged

st33d
Guest
« Reply #88 on: March 02, 2010, 02:23:37 PM »

The RuneScape in-house scripting language couldn't handle white space, had no "else" keyword and would not accept more than one test in the "if" parameter call. And that was just for starters.
This is what happens when someone needs a parser, and things regexes would be a good tool for the job.

I've actually managed to write a half decent scripting language myself using regex. The trick is not to use it all the time. It was even capable of nested parameters in a Lisp kind of fashion. Regex cannot parse nested matches. However, by grabbing the deepest nest and replacing it with a token, working outwards in this fashion and loading the contents of each nest into a stack, you can filter and parse nested matches.

The result was that this script for the monsters was capable of containing other monsters, and so on. One could even trick the parser into behaving recursively by predicting the nesting behaviour.

I'm tempted to believe that RuneScript drove me to write it.
Logged
Epitaph64
Level 1
*



View Profile WWW
« Reply #89 on: March 02, 2010, 02:45:32 PM »

Haha, my coding style is pretty standard but I definitely fall victim to hacking in code. An example from a nefarious project of mine:

Code:
for (int x = 0; x < mapWidth; x++) {
            for (int y = 0; y < mapHeight; y++) {
                if (map[x][y] == 1) {
                    if (x > 0 && y > 0 && x < mapWidth - 1 && y < mapHeight - 1) {
                        if (map[x - 1][y] != 0 && map[x - 1][y - 1] != 0 && map[x][y - 1] != 0 && map[x + 1][y - 1] != 0) {
                            if (map[x + 1][y] != 0 && map[x + 1][y + 1] != 0 && map[x][y + 1] != 0 && map[x - 1][y + 1] != 0) {
                                map[x][y] = 3;
                            }
                        }
                    } else {
                        map[x][y] = 3;
                    }
                }
            }
        }

        //Remove alienated walls
        for (int x = 0; x < mapWidth; x++) {
            for (int y = 0; y < mapHeight; y++) {
                if (map[x][y] == 1) {
                    if (map[x - 1][y] != 3 && map[x - 1][y - 1] != 3 && map[x][y - 1] != 3 && map[x + 1][y - 1] != 3) {
                        if (map[x + 1][y] != 3 && map[x + 1][y + 1] != 3 && map[x][y + 1] != 3 && map[x - 1][y + 1] != 3) {
                            map[x][y] = 0;
                        }
                    }
                }
            }
        }
Logged

Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #90 on: March 02, 2010, 02:48:02 PM »

The RuneScape in-house scripting language couldn't handle white space, had no "else" keyword and would not accept more than one test in the "if" parameter call. And that was just for starters.
This is what happens when someone needs a parser, and things regexes would be a good tool for the job.

I've actually managed to write a half decent scripting language myself using regex. The trick is not to use it all the time. It was even capable of nested parameters in a Lisp kind of fashion. Regex cannot parse nested matches. However, by grabbing the deepest nest and replacing it with a token, working outwards in this fashion and loading the contents of each nest into a stack, you can filter and parse nested matches.

The result was that this script for the monsters was capable of containing other monsters, and so on. One could even trick the parser into behaving recursively by predicting the nesting behaviour.

I'm tempted to believe that RuneScript drove me to write it.

You're on the right track. When you write compilers, which computer-scientifically is what we're talking about here, you generally go through the code in two phases: lexxing and parsing, before compiling. In the lexxing phase you, generally by using hierarchical regexes, transform the code into a stream of tokens. This where you can detect some invalid input. In parsing, you, again usually using regexes, process the token stream into a node tree of structs reflecting the legal expressions of the language. This is where you detect syntax errors. Further steps entails processing the node tree to build state tables and stacks to validate scoping, symbol existence and consistency etc--that is, detecting semantic validity. The next step is translating this now verified node tree into an intermediate language tree, which is then translated to computer instructions (generally assembler, which of course is then compiled in turn).

What the above languages apparently, from what I'm reading, did wrong was trying to apply regexes to the source code itself, and not to the lexxed tokens. That will only lead to a world of pain, and it is nigh on impossible to maintain a compiler written that way for even a reasonably advanced language.
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Skofo
Level 10
*****



View Profile
« Reply #91 on: March 02, 2010, 02:55:10 PM »

Quote
How hard is it to write a decent fucking parser for chrisakes?
A better question is why not use Java or one of the billions of languages with an interpreter written in Java? Addicted
Logged

If you wish to make a video game from scratch, you must first invent the universe.
LemonScented
Level 7
**



View Profile
« Reply #92 on: March 02, 2010, 05:57:34 PM »

Quote
How hard is it to write a decent fucking parser for chrisakes?
A better question is why not use Java or one of the billions of languages with an interpreter written in Java? Addicted

There are a lot of reasons not to use Java, but I daresay that's a Holy War for another thread, since this one has seemed pleasantly language-agnostic so far Wink

Whilst there are good reasons to limit the scope and power of a scripting language under some circumstances, I'd say that a parser that can't cope with whitespace, and that can do "if" but not "else" is pretty inexcusable.
Logged

st33d
Guest
« Reply #93 on: March 03, 2010, 01:38:14 AM »

Quote
How hard is it to write a decent fucking parser for chrisakes?
A better question is why not use Java or one of the billions of languages with an interpreter written in Java? Addicted

lol

Their script was being fucking parsed by Java

 WTF
Logged
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #94 on: March 03, 2010, 03:11:55 AM »

Quote
How hard is it to write a decent fucking parser for chrisakes?
A better question is why not use Java or one of the billions of languages with an interpreter written in Java? Addicted

lol

Their script was being fucking parsed by Java

 WTF

So, back to my informative post, above, that everyone seems dead-set on ignoring. Even though I consider Java to be a criminal language to implement a compiler in, Java has even much less an excuse since it has the JavaCC module: a compiler compiler package that replaces LEX and YACC and allows you to easily automate parsing of any language. Therefore the original guys must have been newbies or hacks, and there is provably no RealWTF(tm), only ignorant beginners that knows just enough to get themselves into deep muck.
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Radnom
Level 8
***


BANNED


View Profile
« Reply #95 on: March 04, 2010, 09:55:26 PM »

I worked with a guy once who used arbitrary numbers for switch cases, e.g.

switch(x)
{
 case 2:
    // something
    break;
 case 3:
    // something
    break;
 case 75:
    // something
    break;
 case 1010101101:
    // something
    break;
}

With the accompanying x = 75, x = 10100101101 etc through the code.

Dear God!! Has he never heard of enums!?

I've had a huge pleasure reading this topic so far, especially Average Software's posts  Smiley I'll post my coding styles some time.
Logged

terloon
Level 0
**


View Profile WWW
« Reply #96 on: March 04, 2010, 10:20:15 PM »

I actually worked with several thousands lines of code for several games where they maintained game states with floating point numbers.
Code:
if(state == 1.1) {
   // hundreds of lines of code
} else if(state = 1.15) {
   // tons of code
    if(substate == 0.5) {
        // just to make things more interestings
    } else if(substate == 0.6) {
    }
} else {
}
Logged
Radnom
Level 8
***


BANNED


View Profile
« Reply #97 on: March 04, 2010, 11:47:14 PM »

AUGH!! the floating point precision errors!!!  WTF

AUGH
Logged

Radnom
Level 8
***


BANNED


View Profile
« Reply #98 on: March 05, 2010, 12:14:23 AM »

Here's an example of a header file (note that I'm going to stop using the double underscores from now on!):

Code:
#pragma once

#ifndef __SPRITE_H__
#define __SPRITE_H__

//   Library Includes
#include "hge.h" //Haaf's Game Enngine
#include "hgesprite.h"

//   Local Includes
#include "vector2.h"
#include "types.h"

//   Types
typedef enum _EBlendMode
{
   BM_MIN = -1,
   BM_ADD,
   BM_SUBTRACT,
   BM_MAX
} EBlendMode;

//   Constants

//   Prototypes
class CThing;

class CSprite
{
// Member Functions
public:
CSprite();
~CSprite();

void Process(const f32 _kfdt);
void Draw();

static void SetHGE(HGE* _pHGE);

inline CVector2& GetPosition();
inline void SetPosition(CVector2& _vecPos);

protected:

private:
CSprite(const CSprite& _kr);
CSprite& operator=(const CSprite& _kr);

// Member Variables
public:

protected:

private:
    static HGE*   sm_pHGE;       // Static Haaf's Game Engine pointer

    CVector2      m_vecPos;      // The position of the sprite.
    f32           m_fDepth;      // The draw depth

    u32           m_uiWidth;     // Sprite's pixel width

    CThing*       m_pThing;      // Some thing
};

#include "sprite.inl"

#endif // __SPRITE_H__


and the cpp Implementation file:

Code:
//   Library Includes
#include <string>

//   Local Includes
#include "iniparser.h"

//   This Include
#include "sprite.h"

//   Static Variables
HGE* CSprite::sm_pHGE = 0;

/**
*
* Constructs the object.
* @author Sean Flannigan
*
**/
CSprite::CSprite()
: m_fDepth(0.0f)
, m_vecPos(0.0f,0.0f)
{
// Constructor.
}

/**
*
* Processes the object.
* @author Sean Flannigan
* @param _kfdt This is the delta tick.
* @return void
*
**/
void
CSprite::Process(const f32 _kfdt)
{
    // Process.
   
    // Random example code:
    for( u32 i = 0; i < 16; ++i)
    {
         // Call a function.
         Function(i);
    }
}

/**
*
* Initialises the object.
* @author Sean Flannigan
* @param _pIniFilename The INI filename to load the sprite info from.
* @return bool True if successful.
*
**/
bool
CSprite::Initialise(const c8* _pIniFilename)
{
   bool bSuccess = false;

   // LOGIC HERE!

   return (bSuccess);
}


Notes:
Normally I have headers at the top of each file explaining their use.
I don't tend to use structs, nor globals. If I did use a global it would begin with S.
I typedefine f32 as float, s32 as signed int, etc. It's useful for remembering how big everything is and it's useful in calculations.
I always use ++i in loops as it is slightly faster.
Return values are enclosed in brackets.
I normally can't be bothered rearranging member data by size to be more efficient.


Note there might be a few errors above! I just copied bits of code from a file I have and added and removed a bunch of lines for example's sake.
Logged

increpare
Guest
« Reply #99 on: March 05, 2010, 01:31:42 AM »

AUGH!! the floating point precision errors!!!  WTF

AUGH
Well presumably if one just sticks to assignment rather than addition/multiplication, and the numbers aren't too close together, this should work okay...
Logged
Pages: 1 ... 3 4 [5] 6 7
Print
Jump to:  

Theme orange-lt created by panic