Python coding standards & DirectX: Part IV

Today we got an open file dialog working so that we could load a mesh .x file and also created a coloursetting dialog to change the colour of the ambient and point lights. I made a very stupid mistake today while programming: I was using a variable named OpenFile, and I in my haste, declared it as Openfile [notice the lack of a capital ‘F’]. My program refused to conpile, and I spent close to an hour debugging, only to find my spelling mistake.

C++ errors are nowhere near as specific as C# ones are.

hmm, maybe this blog won’t die. I seem to be making a blog post almost every day, so it isn’t all bad.

Now a rant about Python coding standards, because I have to rewrite almost all of the ALCUGS stuff because some people didn’t research proper coding techniques.

My example of a Python class looks like this (hsKeyedObject):

class hsKeyedObject:
    """Plasma type 0x0002 :: hsKeyedObject"""
    
    __classIndex = 0x0002
    __className = "hsKeyedObject"
    version = None
    name = None
    type = None
    key = None
    
    def __init__(self, name = "KeyedObject", type = 0x2, version = 5):
        """
        Creates an instance of hsKeyedObject and initializes preliminary variables.
        """
        self.version = version
        self.name = name
        self.type = type
        self.key = plKey()

    def Read(self, stream, manager):
        """
        hsKeyedObject::Read(hsStream* stream, hsResMgr* manager)
        
        Reads the hsKeyedObject format.
        """
        self.key.Read(stream, self.version)

    def Write(self, stream, manager):
        """
        hsKeyedObject::Write(hsStream* stream, hsResMgr* manager)
        
        Writes the hsKeyedObject format.
        """
        self.key.Write(stream, version)

    def Debug(self):
        """
        Prints out detailed debugging info.
        """
        print "HeadSpin Keyed Object header:"
        print " -> Type: [%04X]" % self.key.type
        print " -> Name: %s" % self.key.name

    def ClassName(self):
        """Returns the class name (hsKeyedObject)"""
        return self.__className #Returns 'hsKeyedObject'
    
    def ClassIndex(self):
        """Returns the class index/type (0x0002)"""
        return self.__classIndex #Returns '0x0002'

Guidelines:
All private variables start with two underscores (__variableName).
All variables start with lowercase letters and do not contain underscores in the variable name.
All classes and fuctions have a docstring describing their function.
All functions start with uppercase letters.
All Plasma data types must have the version passed to them.

It’s not hard to follow, people just don’t because they think it’s easier to write messy code. (Actually, I don’t blame them if they didn’t research the coding standards, but if they know any Python, there is no excuse).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s