Craftd:Coding Style: Difference between revisions

From wiki.vg
Jump to navigation Jump to search
imported>Meh.
No edit summary
imported>Meh.
No edit summary
 
(21 intermediate revisions by the same user not shown)
Line 9: Line 9:
== Code Formatting ==
== Code Formatting ==
* Use LF endings. If you're contributing from Windows, get a text editor capable of outputting UNIX-style line endings.
* Use LF endings. If you're contributing from Windows, get a text editor capable of outputting UNIX-style line endings.
* Each indent should be two spaces. No tab characters.
* Each indent should be four (4) spaces. No tab characters.
* As a general rule, limit C code to 80-column lines.
* As a general rule, limit C code to 80/100-column lines.
* Put brackets on a new line, like so:
* Avoid incomprensible names with abbreviations, use extended names for everything.
* Instead of pointers to void* use CDPointer, and remember to explicitly typecast to CDPointer or the other way around.
* To state that a function didn't work well return false and set errno to the value saying what happened, DO NOT use 0 and -1 to state errors, please.
* Put brackets on the same line separated by a space except for function definitions, like so:


<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
if (foo)
int bar (int foo)
{
{
  //This is good
    if (foo) {
        // code
    }
}
</syntaxhighlight>
 
== Style and naming convention ==
 
<syntaxhighlight lang="C">
 
typedef struct _CDThing {
    char wat;
} CDThing;
 
void CD_FooBar (int bar); // This is a public function
 
const int cd_BarFoo (int foo); // This is private
 
static
void
CD_FooBar (int bar, char* pointer)
{
    int* wildBeast;  // each declaration on its own line
    int  anger;
 
    if (omg) {
        call("omg", 3);
 
        while (true) {
            CDThing* thing = CD_CreateThing();
        }
    }
}
}


if (foo) {
extern
  //This is bad
const int
cd_BarFoo (int foo)
{
 
}
}
</syntaxhighlight>
</syntaxhighlight>


== Internal Documentation ==
== Internal Documentation ==
Line 33: Line 72:
  *
  *
  * @remarks Scope: public
  * @remarks Scope: public
  *  
  *
  * @param rectangle pointer to a rectangle struct
  * @param rectangle pointer to a rectangle struct
  * @param x the x coordinate
  * @param x the x coordinate
  * @param y the y coordinate
  * @param y the y coordinate
*
  * @return true if the point is within the rectange, false otherwise
  * @return true if the point is within the rectange, false otherwise
  */
  */
Line 43: Line 83:
== stdbool ==
== stdbool ==
Use of the C99 ''bool'' type in <stdbool.h> is encouraged for internal boolean values within craftd.  Platforms that do not define this can be worked around with Autoconf and preprocessor.
Use of the C99 ''bool'' type in <stdbool.h> is encouraged for internal boolean values within craftd.  Platforms that do not define this can be worked around with Autoconf and preprocessor.
===NOTE:===
When sending over the network, first recast to an int8_t to ensure that it only occupies '''1 byte''' as sizeof(bool) differs from compiler to compiler.


== Logging ==
== Logging ==
Line 50: Line 88:


=== Available functions ===
=== Available functions ===
* '''LOG'''(int priority, const char *format, ...) - The basic abstract logging function used throughout craftd.
Tries logging with the CDMainServer or CDDefaultLogger
* '''LOG_setlogmask'''(int maskbits) - use with '''LOG_MASK()''' macro from syslog to mask message types.
* '''LOG'''(int priority, const char* format, ...) - The basic abstract logging function used throughout craftd.
* '''LOGT'''(int priority, const char *format, ...) - Only compiled with -DTRACE
* '''DEBUG'''(const char* format, ...) - Debugging logging
* '''ERR'''(const char *format, ...) - Fatal conditions that cause exit
* '''ERR'''(const char* format, ...) - Fatal conditions
* '''PERR'''(const char *msg) - perror() exit conditions during startup.
 
=== Examples ===
<syntaxhighlight lang="C">
/* Usually set by main to console or syslog depending on daemonize and startup status */
LOG = &log_console;
LOG_setlogmask = &log_console_setlogmask;


LOG_setlogmask(LOG_MASK(LOG_DEBUG)); // Don't print debugging messages
Use CDConsoleLogger
* '''CLOG'''(int priority, const char* format, ...) - The basic abstract logging function used throughout craftd.
* '''CDEBUG'''(const char* format, ...) - Debugging logging
* '''CERR'''(const char* format, ...) - Fatal conditions


LOG(LOG_DEBUG, "This message wont appear");
Use the given server and its logger
LOG(LOG_INFO, "An informative message %s", passedInStringVar);
* '''SLOG'''(CDServer* server, int priority, const char* format, ...) - The basic abstract logging function used throughout craftd.
 
* '''SDEBUG'''(CDServer* server, const char* format, ...) - Debugging logging
/* Only appears when compiled with -DTRACE */
* '''SERR'''(CDServer* server, const char* format, ...) - Fatal conditions
LOGT(LOG_INFO, "A trace message");
 
/* An error that should cause an exit. Logs with LOG. */
ERR("Fatal error caused by %s", aString);
 
/* perror() and exit failure for startup problems (e.g. socket binding) */
PERR("Can't bind to socket.");
 
</syntaxhighlight>
 
Notes about this implementation can also be found here: http://www.kev009.com/wp/2010/12/no-nonsense-logging-in-c-and-cpp/


== Unknown State ==
== Unknown State ==
Fail early, kick hard.  Do not try to massage the data stream or recover.  Use of ''goto'' is recommended, especially for breaking out of large loop and conditional chains.  Free all allocated resources and close the bufferevent.
Fail early, kick hard.  Do not try to massage the data stream or recover.  Use of ''goto'' is recommended, especially for breaking out of large loop and conditional chains.  Free all allocated resources and close the bufferevent.


 
== vim config ==
== Style and naming convention proposal ==
<syntaxhighlight lang="vim">
 
set autoindent
<syntaxhighlight lang="C">
set smartindent
 
set smarttab
typedef struct _Thing {
set smartcase
    char wat;
set shiftwidth=4
} Thing;
set ts=4
 
set expandtab
void CD_FooBar (int bar); // This is a public function
void cd_BarFoo (int foo); // This is private
 
void
CD_FooBar (int bar, char* pointer)
{
    int* wildBeast;  // each declaration on its own line
    int  anger;
 
    if (omg) {
        call("omg", 3);
 
        while (true) {
            Thing thing;
        }
    }
}
 
void
cd_BarFoo (int foo)
{
 
}
 
</syntaxhighlight>
</syntaxhighlight>


[[Category:Craftd]]
[[Category:Craftd]]

Latest revision as of 01:11, 1 March 2011

Commit Messages

Read this: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html

assert

Use of assertions is advised whenever undefined or dangerous state is encountered with internal constructions and declarations. This includes internal string constructions, thread starting, NULL pointer checks, etc.

Runtime states that come from the network or user should instead use the error handling functions and attempt to punt the client.

Code Formatting

  • Use LF endings. If you're contributing from Windows, get a text editor capable of outputting UNIX-style line endings.
  • Each indent should be four (4) spaces. No tab characters.
  • As a general rule, limit C code to 80/100-column lines.
  • Avoid incomprensible names with abbreviations, use extended names for everything.
  • Instead of pointers to void* use CDPointer, and remember to explicitly typecast to CDPointer or the other way around.
  • To state that a function didn't work well return false and set errno to the value saying what happened, DO NOT use 0 and -1 to state errors, please.
  • Put brackets on the same line separated by a space except for function definitions, like so:

<syntaxhighlight lang="C"> int bar (int foo) {

   if (foo) {
       // code
   }

} </syntaxhighlight>

Style and naming convention

<syntaxhighlight lang="C">

typedef struct _CDThing {

   char wat;

} CDThing;

void CD_FooBar (int bar); // This is a public function

const int cd_BarFoo (int foo); // This is private

static void CD_FooBar (int bar, char* pointer) {

   int* wildBeast;  // each declaration on its own line
   int  anger;
   if (omg) {
       call("omg", 3);
       while (true) {
           CDThing* thing = CD_CreateThing();
       }
   }

}

extern const int cd_BarFoo (int foo) {

} </syntaxhighlight>


Internal Documentation

Methods, structs, and unions should have Doxygen/JavaDoc style documentation.

e.g. <syntaxhighlight lang="cpp"> /**

* Check if a coordinate pair exists within a rectangle struct.
*
* @remarks Scope: public
*
* @param rectangle pointer to a rectangle struct
* @param x the x coordinate
* @param y the y coordinate
*
* @return true if the point is within the rectange, false otherwise
*/

</syntaxhighlight>

stdbool

Use of the C99 bool type in <stdbool.h> is encouraged for internal boolean values within craftd. Platforms that do not define this can be worked around with Autoconf and preprocessor.

Logging

Logging is handled via a function pointer that enables runtime switching of logging between stdout and syslog. The function signature is identical to syslog(3) and all POSIX levels are supported.

Available functions

Tries logging with the CDMainServer or CDDefaultLogger

  • LOG(int priority, const char* format, ...) - The basic abstract logging function used throughout craftd.
  • DEBUG(const char* format, ...) - Debugging logging
  • ERR(const char* format, ...) - Fatal conditions

Use CDConsoleLogger

  • CLOG(int priority, const char* format, ...) - The basic abstract logging function used throughout craftd.
  • CDEBUG(const char* format, ...) - Debugging logging
  • CERR(const char* format, ...) - Fatal conditions

Use the given server and its logger

  • SLOG(CDServer* server, int priority, const char* format, ...) - The basic abstract logging function used throughout craftd.
  • SDEBUG(CDServer* server, const char* format, ...) - Debugging logging
  • SERR(CDServer* server, const char* format, ...) - Fatal conditions

Unknown State

Fail early, kick hard. Do not try to massage the data stream or recover. Use of goto is recommended, especially for breaking out of large loop and conditional chains. Free all allocated resources and close the bufferevent.

vim config

<syntaxhighlight lang="vim"> set autoindent set smartindent set smarttab set smartcase set shiftwidth=4 set ts=4 set expandtab </syntaxhighlight>