コンテンツにスキップ

字下げスタイル

出典: フリー百科事典『ウィキペディア(Wikipedia)』

: indent styleC使

[]


使PythonOccam

[]


4
括弧の配置 スタイル
while (x == y)
{
    something();
    somethingelse();
}
オールマン
while (x == y) {
    something();
    somethingelse();
}
K&R
while (x == y)
  {
    something ();
    somethingelse ();
  }
GNU
while (x == y)
    {
    something();
    somethingelse();
    }
ホワイトスミス
while (x == y)
{   something();
    somethingelse();
}
Horstmann
while (x == y)
  { something()
  ; somethingelse()
  ;
  }
Haskell
while (x == y)
{   something();
    somethingelse(); }
Pico
while (x == y) {
    something();
    somethingelse();
    }
ラトリフ
while (x == y)
  { something();
    somethingelse(); }
Lisp

タブ、スペース、文字数[編集]


使UNIX8macOS4

[]


調UNIX indent 

[]

K&R[]


K&R K&RC使C使C11  ISO/IEC 9899:2011 UNIXLinuxObjective-CC++ ANSIK&R
int main(argc, argv)
    int argc;
    char *argv[];
{
    ...
    while (x == y) {
        something();
        somethingelse();
        if (some_error)
            do_correct(); // K&Rでは単独の文しかないブロックには中括弧を付けていない。
        else
            continue_as_usual();
    }
    finalthing();
    ...
}

C 使[ 1]

:

使 Brian W. Kernighan and Dennis M. RitchieC

1C

C ()
/* ISO標準化以前の関数プロトタイプ宣言がなかった時代の、オリジナルのC言語で使われていたスタイル */
int main(argc, argv)
    int   argc;
    char  *argv[];
{
    ...
}

1TBS(OTBS)[編集]


"The One True Brace Style"()1TBS  OTBS[1]K&R

if/else  do/while 
if (x < 0) {
    printf("Negative");
    negative(x);
} else {
    printf("Positive");
    positive(x);
}



241if...else if...[etc.]...else 

AppleXcode使

Linux[]


LinuxK&RLinux使[2]K&R

使8switch (1) (ifwhiledo-while) if-else180100

Linux使
int power(int x, int y)
{
        int result;

        if (y < 0) {
                result = 0;
        } else {
                result = 1;
                while (y-- > 0)
                        result *= x;
        }
        return result;
}

強制括弧[編集]


11ifelsewhile()

1

Java/ECMAScript[]


Java19951999420Code Conventions for the Java Programming Language[3]沿K&RActionScriptJavaScriptECMAScriptJava
class Test {
    public static void main(String[] args) {
        if (args.length > 0) {
        } else {
        }
    }
}

ストロヴストルップ[編集]


C++使K&R C++The C++ Programming Language使[4]

cuddled else (else}{) 使[4]
    if (x < 0) {
        puts("Negative");
        negative(x);
    }
    else {
        puts("Non-negative");
        nonnegative(x);
    }

ストロヴストルップはK&Rスタイルをクラスに拡張しており、次のように表記される。

    class Vector {
    public:
        Vector(int s) :elem(new double[s]), sz(s) { }   // Vectorのコンストラクタ
        double& operator[](int i) { return elem[i]; }   // インデクサ
        int size() { return sz; }
    private:
        double * elem;    // 要素へのポインタ
        int sz;           // 要素数
    };

public:private:

1EmacsC++K&R使C++ Core Guidelines[5]

BSD/KNF[]


Kernel Normal Form BSD使使使Unix67K&R[6]

SunOS使[6]KNFAT&TNormal Form[7]SunOS1996ANSI Ccstyle調[6][7][8]

vits8visw4

使1使

Cifwhiledoswitchreturn return 使

:
while (x == y) {
        something();
        somethingelse();
}
finalthing();
if (data != NULL && res > 0) {
        if (!JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(
            JS_NewStringCopyN(cx, data, res)), NULL, NULL,
            JSPROP_ENUMERATE)) {
                QUEUE_EXCEPTION("Internal error!");
                goto err;
        }
        PQfreemem(data);
} else {
        if (!JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL),
            NULL, NULL, JSPROP_ENUMERATE)) {
                QUEUE_EXCEPTION("Internal error!");
                goto err;
        }
}
static JSBool
pgresult_constructor(JSContext *cx, JSObject *obj, uintN argc,
    jsval *argv, jsval *rval)
{
        QUEUE_EXCEPTION("PGresult class not user-instanciable");

        return JS_FALSE;
}

BSD/オールマンのスタイル[編集]


BSD/ (Allman style) 使BSD (BSD KNF)

[9]
while (x == y)
{
    something();
    somethingelse();
}
finalthing();

これはPascalTransact-SQLの標準的な字下げに似ていて、中括弧が "begin" や "end" といったキーワードと同じように扱われている。

(* オールマン式の字下げをPascalで記述した場合の例 *)
procedure dosomething(x, y: Integer);
begin
    while x = y do
    begin
        something();
        somethingelse();
    end;
end;




// while (x == y)
{
    something();
    somethingelse();
}

これも同様。

// for (int i=0; i < x; i++)
// while (x == y)
if (x == y)
{
    something();
    somethingelse();
}

さらに条件付きコンパイルでも使える。

    int c;
#ifdef HAS_GETCH
    while ((c = getch()) != EOF)
#else
    while ((c = getchar()) != EOF)
#endif
    {
        do_something(c);
    }



Microsoft Visual StudioMicrosoft Visual C++使

Allman-8[]


Allman-8K&RLinux808使使

C#[]


C#C# [10]BSD/
if ((val1 > val2) && (val1 > val3))
{
    something();
}

ホワイトスミスのスタイル[編集]

ホワイトスミス・スタイルはあまり有名ではない。ウィッシャート・スタイルとも呼ばれ、もともとは世界初の商用Cコンパイラであるホワイトスミス・コンパイラで、ドキュメントに使われていた。またこれは初期のWindows業界でもよく使われ、Programmer's Guide to Windows (Durant、Carlson、Yao共著)、Programming Windows (Petzold著)、Windows 3.0 Power Programming Techniques (Norton、Yao共著)などのような人気のWindowsプログラミング書籍でも使われた。

このスタイルでは制御文の後の中括弧を次の行に置くが、このときに字下げする。ブロック内の文は中括弧と同じ字下げレベルに書かれる。

while (x == y)
    {
    something();
    somethingelse();
    }
finalthing();

BSD/1調 while 調

1
if (data != NULL && res > 0)
    {
    if (!JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)), NULL, NULL, JSPROP_ENUMERATE))
        {
        QUEUE_EXCEPTION("Internal error!");
        goto err;
        }
    PQfreemem(data);
    }
else if (!JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL), NULL, NULL, JSPROP_ENUMERATE))
    {
    QUEUE_EXCEPTION("Internal error!");
    goto err;
    }

else if#elif

GNU[]


BSD/GNU22[11]

LISP[12]Lisp (progn) 調C1960 - 1970ALGOLXPL[13][14]GNU
static char *
concat (char *s1, char *s2)
{
  while (x == y)
    {
      something ();
      somethingelse ();
    }
  finalthing ();
}

BSD/

GNU EmacsGNUindentGNU使GNU使

GNU Emacs 使使

[]


1997SComputing Concepts with C++ Essentials使Pascal User Manual and Report[15]使
while (x == y)
{   something();
    somethingelse();
    //...
    if (x < 0)
    {   printf("Negative");
        negative(x);
    }
    else
    {   printf("Non-negative");
        nonnegative(x);
    }
}
finalthing();

K&R12003[16]

Pico[]


Pico使Pico  return 使[17]:
stuff(n):
{ x: 3 * n;
  y: doStuff(x);
  y + x }

K&RK&R

[]


CProgrammers at Work[18]1TBSdBase-II-III4GLDEC[19]K&R
 // Cの例
 for (i = 0; i < 10; i++) {
     if (i % 2 == 0) {
         doSomething(i);
         }
     else {
         doSomethingElse(i);
         }
     }

マークアップ言語では次のようになる。

{|
|-
| lots of stuff...
      more stuff
      || alternative for short lines || etc. 
|}

{|
|-
... etc
|}

Lispスタイル[編集]


LispLispPythonPythonPython
for(i = 0; i < 10; i++) {
    if(i % 2 == 0) {
        doSomething(i); }
    else {
        doSomethingElse(i); } }


// Cの例
for (i = 0; i < 10; i++)
    {if (i % 2 == 0)
        {doSomething(i);}
     else
        {doSomethingElse(i);
         doThirdThing(i);}}

 

# Pythonの例
for i in range(10):
    if i % 2 == 0:
        do_something(i)
    else:
        do_something_else(i)
        do_third_thing(i)

 

;; Lispの例
(dotimes (i 10)
    (if (= (rem i 2) 0)
        (do-something i)
        (progn
            (do-something-else i)
            (do-third-thing i))))

Haskellスタイル[編集]

Haskellでは中括弧とセミコロンを利用でき、中括弧の配置は任意である[20]。以下の2つの例はいずれもコンパイラに同一のコードとして扱われる。

braceless = do
  text <- getContents
  let
    firstWord = head $ words text
    bigWord = map toUpper firstWord
  putStrLn bigWord

braceful = do
  { text <- getContents
  ; let
      { firstWord = head $ words text
      ; bigWord = map toUpper firstWord
      }
  ; putStrLn bigWord
  }

その他[編集]




K&RK&R

for8使Linux K&R使

:
for ( int i = 0 ; i < total ; i++ ) {
    foo(bar);
} //for ( i )

if (x < 0) {
    bar(foo);
} //if (x < 0)

2

使使

参考文献[編集]

脚注[編集]

注釈[編集]

  1. ^ 日本語版ではそうなっていないが、同書の原書はレイアウトに気が配られている。

出典[編集]



(一)^ 1TBS. 2022415

(二)^ kernel.org.

(三)^ Code Conventions for the Java Programming Language: Contents

(四)^ abStroustrup, Bjarne (20109). PPP Style Guide. 2022415

(五)^ C++ Core Guidelines. GitHub. 2018113

(六)^ abcShannon, Bill (1996819). C Style and Coding Standards for SunOS. Sun Microsystems, Inc.. https://www.cis.upenn.edu/~lee/06cse480/data/cstyle.ms.pdf 2019615 

(七)^ abDTraceToolkit Style Guide. 201526

(八)^ cstyle.pl. illumos-gate.  Sun Microsystems, Inc. (199899). 201526

(九)^ indent style. The on-line hacker Jargon File (20031229). 2022320

(十)^ C#  - C#   | Microsoft Docs

(11)^ Formatting Your Source Code. GNU Coding Standards. 201666

(12)^ Stallman, Richard (20021028). My Lisp Experiences and the Development of GNU Emacs (Transcript of speech at the International Lisp Conference). 201666

(13)^ R. Baumann, M. Feliciano, F. L. Bauer, and K. Samuelson, Introduction to Algol, 1964, https://archive.org/details/introductiontoal00baum

(14)^ W. M. McKeeman, J. J. Horning, and D. B. Wortman, A Compiler Generator, 1970, https://archive.org/details/compilergenerato00mcke

(15)^ Jensen, Kathleen; Wirth, Niklaus (1974). PASCAL User Manual and Report. Springer-Verlag 

(16)^ Horstmann Style Guide

(17)^ Ohno, Asako (2013). . 2013 IEEE Frontiers in Education Conference (FIE): 19081910. doi:10.1109/fie.2013.6685167. ISBN 9781467352611. 

(18)^ Lammers, Susan (1986). Programmers at Work. Microsoft Press. ISBN 978-0-914845-71-3. https://archive.org/details/programmersatwor00lamm_0 

(19)^ Artistic Style 2.05 Documentation. Artistic Style. 2015424

(20)^ The Haskell 98 Report. haskell.org. 201633

関連項目[編集]