Results 1 to 10 of 24

Thread: Article: Why Code Readability Matters

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    PGDCE Developer
    Join Date
    Jun 2010
    Location
    California, USA
    Posts
    25
    Quote Originally Posted by ?ëu?±o Mart??nez View Post
    And I never, never, NEVER use hungarian notation. That's true annoying. What if you change the type of a largely used variable? BTW, the proposal of the variable itself should be enough to know the type it is (i.e. a variable named "FileName" will never be of type "INTEGER", will it?)
    In code I have written no, FileName will never be Integer however it might be something like TBESEString or widestring, or any other format that it could fit. Also, I have used many libraries where a variable or type is actually an int or cardinal or a word but named as though it contained data and not an index for the data.

    And for changing the type of a largely used variable. Well My standard is I don't use variables outside of one unit if possible. So, normally I only have to change its name in a single file. And I love GREP and Search and Replace.

    Its not any standard notation I follow. I just use the structure that I find easiest to read. As I said, I don't like the way I coded 5 years ago. It's possible I would like the way I code now, in 5 years. It all comes down to preferences, there is no de facto standard. And even if there is, who cares. Follow the "Shop Standards" and if you are so lucky maybe you are making the "Shop Standards" wherever you are working.

    I really think it is a waste of time when you get with other programmers and no one codes alike. I would rather hate the formatting of the code and have to write code like that, then to have 5 different formatting standards on one project.

  2. #2
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    I tend to vary my notation depending on context for example, for a large number of type declerations that get used often, I'll shorten the terms but also include information pertaining to that type ie :

    SInt8 - Signed 8bit Integer
    UInt8 - Unsigned 8bit Integer etc
    SInt16
    UInt16
    SInt32
    UInt32
    Float32
    Float64

    So this is similar to a lot of C stuff, OpenGL etc. I don't use classic hungarian notation simply because it's an old technique that isn't needed with modern code editors.

    For class definitions and methods I tend to use the most descriptive terms (in CamelCase) and group together classes by name whenever it seems appropriate :

    TJSpatialRegion
    TJSpatialEntity
    TJSpatialEntityController
    TJSpatialEntityControllerAnnotation

    I always use the prefix 'A' on variables in method declerations so there can be no confusion between private variables (F). class Properties exposed to end-users use no prefix.

    FVelocityVector : TJVec3;
    FSpatialController : TJSpatialController;

    procedure TJSpatialEntity.SetVelocityVector(AVelocityVector : TJVec3);
    procedure TJSpatialEntity.SetSpatialController(AspatialContr oller : TJSpatialController);
    procedure TJSpatialRegion.AddSpatialEntity(ASpatialEntity : TJSpatialEntity);

    property VelocityVector : TVec3 read GetVelocityVector write SetVelocityVector;

    I think that naming conventions are important for either :

    A) Adoption Speed - If you intend your code to be accessed by a large number of people, you'll want it to be as descriptive as possible.

    B) Coding Speed - If it's just you or a small team and the source is closed then use whatever shorthand is appropriate in order to save on time writing/reading the code.

    Very large code-bases require all coders to use the same system for notation if only to avoid adding additional complexity into already complex systems.


    P.S I would not recommend using layout rules with the intention of making the language syntax easier to read! that's why we have syntax highlighters etc. If you're doing a whole bunch of keystrokes to bring code-completion in line with your own layout, you're just wasting time. Use the layout defined in your editors snippets. Questions of notation should be about understanding what your code does not how the language works.
    Last edited by phibermon; 21-06-2011 at 09:14 PM.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  3. #3
    Quote Originally Posted by phibermon View Post
    (...)
    I like your style.
    No signature provided yet.

  4. #4
    Note that this is written before reading actual article. Here are my thoughts on the subject.

    I always try to keep my code readable, simply because I often "jump between projects" - I have three Pascal projects right now (game engine, Super Heli Land and DCPU-16-like virtual processor) and two RPG Maker ones (plus one on hiatus until I'll get more skilled with RM because of magnitude of it) and when I get bored with one project I am going to other. So often I have few month breaks from one project.

    Because of it, I've learned to write easy to read code. For my identifiers (variables, procedures and functions) I'm using CamelCase (with first letter upper case), for constants it is all uppercase. Sometimes for procedures/functions I'm using underscore_notation, but only when code readability suffers from usage of CamelCase or when I'm writing some library and want to visually separate its name (example: DarkhogGui_DrawButton).

    I also do lots of commenting. Even most insignificant and obvious line should be IMO commented, because after few months you won't know what it does.

  5. #5
    Quote Originally Posted by Darkhog View Post
    I also do lots of commenting. Even most insignificant and obvious line should be IMO commented, because after few months you won't know what it does.
    Commenting is verry important but I keep forgetting to coment my code myself

  6. #6
    Quote Originally Posted by SilverWarior View Post
    Commenting is verry important but I keep forgetting to coment my code myself
    Don't feel like an alien, it happens to many programmers, myself included. But well writen code is a good documentation in itself if you name your functions/procedures and types/const/variables/classes/properties with meaning names.

  7. #7
    Quote Originally Posted by pitfiend View Post
    Don't feel like an alien, it happens to many programmers, myself included.
    It happens to me too. I usually put comments after I'm certain code is doing what is supposed to do.

    Quote Originally Posted by pitfiend View Post
    But well writen code is a good documentation in itself if you name your functions/procedures and types/const/variables/classes/properties with meaning names.
    Yeah, about that... I'm not big fan of "self documenting code". I had many times read code that was supposed to be "self documenting one" but wasn't easy to grasp (though easier than if variables were called "Variable1","Variable2", etc. and functions "Function1","Function2",...). And it was both Pascal and other language code (which includes so easy, at least for me, languages like C# or Java).

    In my opinion nothing will help more than properly documented code. Even comments like "this function is supposed to do..." before function body are good.

  8. #8
    PGDCE Developer Carver413's Avatar
    Join Date
    Jun 2010
    Location
    Spokane,WA,Usa
    Posts
    206
    I hate comments in the code,it just makes it harder to visualize the actual code. I really hate people who indent 8 char at a time, 2 is enough.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •