1 / 10

1.3 一貫性と慣用句

1.3 一貫性と慣用句. 慣3 フク. インデント. C 言語の場合 Emacs スタイル スペース 2 個 K&R スタイル スペース 4 個 Linux Kernel スタイル スペース 8 個 ポイント:インデントを統一しよう. インデント (2). インデントにより誤解 (C の場合 ). if (month == FEB) { if (year%4 == 0) if (day > 29) legal = FALSE; else if (day > 28) legal = FALSE; }. if (month == FEB) {

adriel
Download Presentation

1.3 一貫性と慣用句

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1.3 一貫性と慣用句 慣3 フク

  2. インデント C言語の場合 • Emacsスタイル • スペース2個 • K&Rスタイル • スペース4個 • Linux Kernelスタイル • スペース8個 ポイント:インデントを統一しよう

  3. インデント(2) • インデントにより誤解 (Cの場合) if (month == FEB) { if (year%4 == 0) if (day > 29) legal = FALSE; else if (day > 28) legal = FALSE; } if (month == FEB) { if (year%4 == 0) if (day > 29) legal = FALSE; else if (day > 28) legal = FALSE; } Pythonは例外

  4. インデント(3) • さらに改良 – 重複部分を消す if (month == FEB) { int nday; nday = 28; if (year%4 == 0) nday = 29; if (day > nday) legal = FALSE; } if (month == FEB) { if (year%4 == 0) { if (day > 29) legal = FALSE; } else { if (day > 28) legal = FALSE; } }

  5. 慣用句-idiom • ループ • 配列: • for (i = 0; i < n; i++) • array[i] = 1.0; • 配列の先頭インデックスはゼロ • 範囲N:array[0]...array[N-1] • リスト: • for (p = list; p != NULL; p = p->next) • ... • リストの最後はNULL

  6. 慣用句-idiom(2) • 無限ループ for (;;) ... while (1) ...

  7. 慣用句-idiom(3) • 安全性 gets() fgets() strcpy() strncpy() strcat() strncat() strcmp() strncmp() 脆弱性:バッファーオーバーフロー!

  8. 慣用句-idiom(4) • 文字列処理 char str[] = “hello”; strlen(str); // 5 sizeof(str); // 6 str[5]; // ‘\0’終端記号  慣用句(コピーの場合) p = malloc(strlen(buf) + 1); Javaは大丈夫

  9. 慣用句-idiom(5) • if – else 文 if (arg < 2) { fprintf(stderr, “HOGE!”); exit(1); } ... // メイン、if から抜け出した ... ... if (argc > 1) { ... // メイン ... ... } else { fprintf(stderr, “HOGE!”); exit(1); }

  10. 慣用句-idiom(6) • switch文 switch (c) { case ‘-’: sign = -1; /* fall through */ case ‘+’: c = getchar(); break; } switch (c) { case ‘-’: sign = -1; case ‘+’: c = getchar(); case ‘.’: break; }

More Related