Angband.oook.cz
Angband.oook.cz
AboutVariantsLadderForumCompetitionComicScreenshotsFunniesLinks

Go Back   Angband Forums > Angband > Development

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old April 22, 2016, 04:21   #1
calris
Adept
 
Join Date: Mar 2016
Posts: 194
calris is on a distinguished road
Angband Coding Style

Looking at the coding style for Angband, I notice it's pretty much identical to the Linux coding style except for two minor differences:
  • Angband uses 4 tabs, Linux uses 8
  • Angband permits 'break' and 'continue' to be placed on the same line as conditional statements

Personally, I prefer 8 tabs over 4, but I'm happy to work with 4

Allowing 'break' and 'continue' to share the same line as a conditional statement, however, just feels wrong. From the example in the coding guidelines:
Code:
            /* Only print even numbers */
            if (i % 2) continue;

            /* Be clever */
            printf("Aha!");
I've actually been caught out by code like this - thinking that the printf would get called when (i % 2) was non-zero.

To me, this:

Code:
            /* Only print even numbers */
            if (i % 2)
                    continue;

            /* Be clever */
            printf("Aha!");
is much clearer. Also, when the if condition is long and complex, it's easy to miss that there is actually a statement dangling off the end. Add to this that this 'exception' has been abused and there is code other than 'break' and 'continue' that has crept in, I think we should drop the exception entirely.

The only other 'issue' I have is breaking function parameters over multiple lines. When a function declaration or call hits 80 columns, we need to split the parameters over multiple lines. The Linux coding style does not cover specifically how this is handled - Angband's coding style does.

Personally, I prefer this:

Code:
         foo(buf,
             sizeof(buf),
             FLAG_UNUSED,
             FLAG_TIMED,
             FLAG_DEAD);
Over this (which is the current Angband coding style):

Code:
         foo(buf, sizeof(buf), FLAG_UNUSED,
              FLAG_TIMED, FLAG_DEAD);
Partly because if we add another parameter, we end up with:

Code:
         foo(buf,
             sizeof(buf),
             new_parameter,
             FLAG_UNUSED,
             FLAG_TIMED,
             FLAG_DEAD);
Which, when you look at the patch, the addition of the new parameter is clear and precise

Compared to this:

Code:
         foo(buf, sizeof(buf), new_parameter,
              FLAG_UNUSED, FLAG_TIMED, FLAG_DEAD);
Which looks plain ugly in a patch.

Lastly, I'm wondering what you all think of the following:

Code:
        if (some_variable)
                do_something();
        else
                do_something_else();
So far, so good - but what about when we add a comment? Do we do this:

Code:
        if (some_variable)
                /* Ideally we want this to happen */
                do_something();
        else
                /* But we have a fallback strategy */
                do_something_else();
Or this?

Code:
        if (some_variable) {
                /* Ideally we want this to happen */
                do_something();
        } else {
                /* But we have a fallback strategy */
                do_something_else();
        }
To be honest, I find both options a bit ugly - single-statement if/then results should be so obvious as to never need a comment
calris is offline   Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sil: 'forge' key in Angband-style keyset? Therem Harth Sil 3 October 24, 2012 17:29
Skyrim, angband style BlackKite Idle chatter 8 June 27, 2012 02:28
some more questions about coding Malak Darkhunter Vanilla 16 January 3, 2012 04:52
more variant coding woes will_asher Variants 1 February 10, 2011 05:18
Angband coding question will_asher Variants 10 September 3, 2008 22:03


All times are GMT +1. The time now is 10:37.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.