विषय पर बढ़ें

अंतिम अद्यतन:2026-10-12

libcurl C++ development notes

Code style का एकरूप होना team collaboration और code review में मदद करता है, industry में google की style ज़्यादा उपयोग होती है, इस section में कुछ अलग देखते हैं, हमारे रोज़ ज़्यादा उपयोग होने वाले tool curl की team की code guidelines कैसी हैं।

curl के बारे में संक्षेप में, यह command line या scripts में data transfer के लिए बहुत उपयोगी है, आपने भी इस्तेमाल किया ही होगा, इसका source implementation github पर देखा जा सकता है

https://github.com/curl/curl

नीचे हम कुछ पहलुओं से curl की C language development guidelines देखते हैं।

Naming

नए functions और variables के नाम logical और समझने योग्य होने चाहिए, और उनके उपयोग के अनुसार नाम रखे जाने चाहिए।

File-local functions को static declare किया जाना चाहिए, lowercase नाम की सलाह दी जाती है।

Indentation

Indentation के लिए केवल spaces उपयोग करें, tabs नहीं। हर नए opening brace के लिए दो spaces।

if(something_is_true) {
  while(second_statement == fine) {
    moo();
  }
}

Comments

चूंकि C89 code लिखा जाता है, // comments की अनुमति नहीं है। वे C99 standard में ही introduce हुए थे। केवल /* comment */ उपयोग करें।

/* 这是一个注释 */

Long lines

curl का source code कभी भी 79 columns से लंबा नहीं होना चाहिए, भले ही modern बड़ी screens और high resolution के दौर में, इसे बनाए रखने के दो कारण आज भी हैं:

  1. संकरे columns चौड़े columns से पढ़ने में आसान होते हैं। एक कारण यह है कि अख़बारों ने दशकों यहाँ तक कि सदियों से columns का उपयोग किया है।
  2. संकरे columns developers को अलग-अलग windows में कई code snippets side-by-side दिखाने में आसानी देते हैं। इससे एक ही screen पर दो या तीन source code windows — साथ में कई terminal और debugging windows — side-by-side रखना संभव होता है।

Braces

if/while/do/for expressions में, हम opening brace को keyword वाली line में लिखते हैं, और closing brace को initial keyword के समान indentation level वाली line पर रखते हैं। इस तरह:

if(age < 40) {
  /* 显然是年轻人 */
}

अगर इनमें केवल एक single-line statement है, तो braces छोड़े जा सकते हैं:

if(!x)
  continue;

Functions के लिए, opening brace अलग line पर लिखा जाना चाहिए:

int main(int argc, char **argv)
{
  return 1;
}

'else' अगली line पर

Braces के साथ conditional expression में else clause जोड़ते समय, हम उसे closing brace के बाद नई line पर जोड़ते हैं। इस तरह:

if(age < 40) {
  /* 显然是年轻人 */
}
else {
  /* 可能是脾气暴躁的 */
}

Opening parenthesis से पहले space नहीं

if/while/do/for expressions का उपयोग करते समय, keyword और opening parenthesis के बीच space नहीं होना चाहिए। इस तरह:

while(1) {
  /* 永远循环 */
}

Boolean conditions का उपयोग

if/while conditions में, हम condition की value को boolean की तरह TRUE या FALSE से, pointers को NULL या != NULL से, और integers को zero या non-zero से test करना पसंद करते हैं, न कि इस तरह:

result = do_something();
if(!result) {
  /* 出现了问题 */
  return result;
}

Conditions में assignment नहीं

Readability बढ़ाने और conditions की जटिलता घटाने के लिए, if/while conditions में variable assignment से बचें। इस style को पसंद नहीं किया जाता:

if((ptr = malloc(100)) == NULL)
  return NULL;

बल्कि ज़्यादा साफ़ अभिव्यक्ति को प्रोत्साहन दिया जाता है:

ptr = malloc(100);
if(!ptr)
  return NULL;

नया block नई line पर

एक ही line पर कभी भी कई statements नहीं लिखें, छोटे if() conditions भी इसका अपवाद नहीं हैं।

if(a)
  return TRUE;
else if(b)
  return FALSE;

कभी ऐसा न करें:

if(a) return TRUE;
else if(b) return FALSE;

Operators के चारों ओर spaces

C expressions में operators के दोनों ओर spaces उपयोग करें। Postfix (), [], ->, ., ++, -- और unary +, -, !, ~, & operators को छोड़कर, इनमें space नहीं होना चाहिए।

उदाहरण:

bla = func();
who = name[0];
age += 1;
true = !false;
size += -2 + 3 *

 (a + b);
ptr->member = a++;
struct.field = b--;
ptr = &address;
contents = *pointer;
complement = ~bits;
empty = (!*string) ? TRUE : FALSE;

Return values में parentheses नहीं

'return' statement में अतिरिक्त parentheses न लगाएँ:

int works(void)
{
  return TRUE;
}

sizeof के argument में parentheses

Code में sizeof operator उपयोग करते समय, हम उसके argument के चारों ओर parentheses रखना पसंद करते हैं:

int size = sizeof(int);

Column alignment

कुछ statements एक single line में पूरे नहीं होते, क्योंकि line बहुत लंबी हो, statement पढ़ने में कठिन हो, या ऊपर बताई गई अन्य style guidelines के कारण। ऐसी स्थिति में statement कई lines में फैलता है।

अगर कोई continuation line किसी expression या subexpression का हिस्सा है, तो आपको उसे उपयुक्त column पर align करना चाहिए, ताकि यह साफ़ पता चले कि वह statement का कौन सा हिस्सा है। Operators, continuation lines की शुरुआत में नहीं होने चाहिए। अन्य मामलों में, 2 spaces के indentation guideline का पालन करें। यहाँ libcurl से कुछ उदाहरण हैं:

if(Curl_pipeline_wanted(handle->multi, CURLPIPE_HTTP1) &&
   (handle->set.httpversion != CURL_HTTP_VERSION_1_0) &&
   (handle->set.httpreq == HTTPREQ_GET ||
    handle->set.httpreq == HTTPREQ_HEAD))
  /* 没有要求 HTTP/1.0 并且是 GET 或 HEAD 请求 */
  return TRUE;

अगर parentheses नहीं हैं, तो default indentation उपयोग करें:

data->set.http_disable_hostname_check_before_authentication =
  (0 != va_arg(param, long)) ? TRUE : FALSE;

Function call में opening parenthesis उपयोग करें:

if(option) {
  result = parse_login_details(option, strlen(option),
                               (userp ? &user : NULL),
                               (passwdp ? &passwd : NULL),
                               NULL);
}

"current open" parenthesis के साथ align करें:

DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
             "server response left\n",
             (int)clipamount));

Conditional code के लिए #ifdef HAVE_FEATURE उपयोग करें। हम #ifdef lines में किसी विशेष operating system या hardware की जांच से बचते हैं। HAVE_FEATURE को configure script द्वारा Unix जैसे systems के लिए generate किया जाना चाहिए, और वे अन्य systems की config-[system].h files में hardcode होते हैं।

हम यह भी प्रोत्साहन देते हैं कि libcurl build के समय, ऐसे macros/functions उपयोग किए जाएँ जो खाली या constant define हो सकते हैं, ताकि code seamless रहे। इस उदाहरण की तरह, जहाँ magic() function build समय की conditions के अनुसार अलग काम करता है:

#ifdef HAVE_MAGIC
void magic(int a)
{
  return a + 2;
}
#else
#define magic(x) 1
#endif

int content = magic(3);

Structs को typedef न करें

भले ही structs उपयोग किए जा सकते हैं, लेकिन उन्हें typedef न करें। उन्हें पहचानने के लिए struct name का तरीका उपयोग करें:

struct something {
   void *valid;
   size_t way_to_write;
};
struct something instance;

ऐसा न करें:

typedef struct {
   void *wrong;
   size_t way_to_write;
} something;
something instance;

यह लेख यहाँ से translate किया गया है: https://curl.se/dev/code-style.html

नोट

यह लेख development notes है, official libcurl documentation और अपने project के error codes की जांच के साथ मिलाकर पढ़ने की सलाह है।

संबंधित विषय

टिप्पणियाँ