Coding standards

Posted by – December 23, 2010

I’m currently working on a C++ project and the coding standard problem raised again. And again, nobody in the team agree about the style/naming format to adapt. C++ sounds somewhat cursed on this topic. After so much years of existence, no precise coding rules has emerged and everyone is almost authorized to code as he wants to, making the whole project source code quite very heterogeneous and obviously hard to maintain. Java has a clean coding rules and almost everybody is following it without arguing. C# seems to have also a common coding standard, but it doesn’t seems to be that followed. That could be my very small experience in C#, I stand to be corrected. Back to C++. You may think: ‘what a big deal, just pick some rules and stick to it’. That’s the ultimate goal, thanks for your input ! But then, you have to please everyone habit and it’s not that easy. So you’re going to compromize on many thing and at the end, nobody is really happy with the result.

But wait, it’s a PureBasic blog, no ? That’s right and I wondered if we needed strict PureBasic coding standard, but after some thoughts it doesn’t seem to be needed and i will explain why:

- PureBasic comes with a big command set, which implicitly set the the name rules for the function/procedure naming: CamelCase. One down. In C++ you have to choose how to name your functions, your methods and your class. Candidate are numberous: myMethod(), MyMethod(), my_method(), mymethod() and so on.

- The constants are prefixed by a ‘#’ in PB, so it can’t clash with variables or anything else. PureBasic comes with a lot of predefined constants, all in CamelCase as well, setting a de facto standard. Again in C++ you have many way to name them: MyConstant, MY_CONSTANT, kMyConstant etc.

- PureBasic help and demo examples are very consistent and always share the same syntax rules which helps to guide the newcomers to format the code the same way.

- There is no “{}” in PureBasic, so the flamewar between K&R and GNU style is avoided, thanks !

When looking at the code snippet pasted on the forums.purebasic.com, I feel there is a common style and it’s very cool to see that. It doesn’t take long to get a grip on someone else code, and that’s a very important part of a programming tool.

Merry Christmas !

5 Comments on Coding standards

  1. flaith says:

    Always a pleasure to read this blog and very constructive
    Merry Christmas Fred, Freak, the Team …

  2. netmaestro says:

    Some good observations, and quite relevant in collaborative projects. As most projects of much size and popularity are collaborations, it’s important that everyone be comfortable with the code others are producing. There are some posters on the forums whose coding style I don’t like (nothing against them – their code is usually worth reading) and it is quite irritating to read through. Here’s hoping you guys can find a happy compromise.

    Thanks for the blog post!

  3. IdeasVacuum says:

    I think the absolute most important thing is good code commenting. I curse myself from time to time when I find I have not commented my own code enough for me to easily modify it!

    In terms of style, some of the PB forum contributors seem to be trying to ‘save ink’ by squashing-up lines of code together and using only tiny indentation. Any code is much more readable if it is laid-out with care. For example, when Gadgets are defined, having their XY posn and XY size columns lined-up makes it easier to spot mistakes (overlaps, identical gadgets etc). There is no need for anyone to be in fear of wearing out the space-bar on their keyboard :-) .

    It would be nice if code posted to the forum could be colour coded as it is in the IDE.

    Fred, I have seen C code (from a large commercial team project) that was so poorly presented that you could be forgiven for thinking that it had been encrypted! Some programmers out there, for some strange reason, take pride in doing this, yet it is often the root of some serious bugs. The Universities could do more – spaghetti code should not be accepted from students….

  4. akee says:

    If we could rename the huge function() names by type, would it help coding faster?

    Example if we used…

    str_Left() instead of Left()
    str_Right() instead of Right()
    fil_Open() instead of Open()
    fil_Close() instead of Close()
    gdt_ListView() instead of ListViewGadget()
    gdt_Option() instead of OptionGadget()

    we could type the type and AutoComplete will help find the function for us. Just my thoughts… :)

  5. langinagel says:

    Sorry to be picky…

    What you refer to are coding s t y l e s, not standards.

    The coding style supports better reading.

    A coding standard supports a coding way to avoid potential errors.
    And Fred: there are coding standards for C++. One example from the Safety area:
    JSF++ (http://www.stroustrup.com/JSF-AV-rules.pdf) for example. In the world of (embedded) C there is the MISRA coding standard (many derived examples, google for yourself).

    I like to push that because a few rules of JSF++ and MISRA are already fulfilled by default BASIC compiling rules. A defined rest could also be standardized, maybe leading to a tool such as lint that might fulfill some of the feature requests like e.g. checking for unused variables.
    What would be the benefit of such a standard & tool?
    From my point of view Purebasic would get the glance of a very professional Programming language. There are not that much programming languages which do have such a lint tool with a coding standard. Error detction in codes might be shorter.
    If other kinds of Assemblers might be used, maybe Purebasic could approach the embedded world.

    I know this is not what “professional” application software developpers might need…because they like features like compilers that foresee self-evaporating code (x = 0, a = x; for i = 0 to a ….next) and smart compilers. We had the discussion already in one thread.

    So far just for clarification.
    Greetings from a picky guy

Leave a Reply