Re: [討論] 前輩們變數都怎麼命名2?
看板C_and_CPP (C/C++)作者fatalfeel2 (風在動)時間3年前 (2023/03/17 12:04)推噓32(36推 4噓 121→)留言161則, 20人參與討論串1/2 (看更多)
Linux Coding Style
https://docs.kernel.org/next/translations/zh_TW/process/coding-style.html
第3段
大括號 為何 function 與 判斷式會不同
Linus 本人給出的理由是 function 中不會包著function
即沒有巢狀式的function 而判斷式中會包著判斷式
另一點文中提到的 K&R coding style 也是function與判斷式不同
新型的K&R style function 與 判斷式的左括號統一了
https://gist.github.com/jesseschalken/0f47a2b5a738ced9c845
再來就是自C++11 之後 Function 內可以包著Function
EX: lambdas 表示式
https://stackoverflow.com/questions/4324763/can-we-have-functions-inside-functions-in-c
!!!上述原因 目前的 C++ [判斷式的左括號] 已無規範必要!!!
判斷式的左括號 在行尾或下一行皆可
///////////////////linux coding style///////////////
1) 縮進
制表符是 8 個字符,所以縮進也是 8 個字符。有些異端運動試圖將縮進變爲 4
(甚至 2!) 字符深,這幾乎相當於嘗試將圓周率的值定義爲 3。
理由:縮進的全部意義就在於清楚的定義一個控制塊起止於何處。尤其是當你盯著你的
屏幕連續看了 20 小時之後,你將會發現大一點的縮進會使你更容易分辨縮進。
2)
...略
3) 大括號和空格的放置
C 語言風格中另外一個常見問題是大括號的放置。和縮進大小不同,選擇或棄用某种放
置策略並沒有多少技術上的原因,不過首選的方式,就像 Kernighan 和 Ritchie 展示
給我們的,是把起始大括號放在行尾,而把結束大括號放在行首,所以:
if (x is true) {
we do y
}
不過,有一個例外,那就是函數:函數的起始大括號放置於下一行的開頭,所以:
int function(int x)
{
body of function
}
全世界的異議份子可能會抱怨這個不一致性是… 是的 … 不一致的,不過所有思維健全
的人 都知道
(a) K&R 是 正確的 並且 (b) K&R 是正確的。
此外,不管怎樣函數都是特 殊的 (C 函數是不能嵌套的)。
當只有一個單獨的語句的時候,不用加不必要的大括號。
if (condition)
action();
和
if (condition)
do_this();
else
do_that();
※ 引述《fatalfeel2 (風在動)》之銘言:
: 程式命名規則 與 Makefile
: 1. 查閱了 ISO 1999 C99, ISO 2011 C++, ISO 2014 C++, ISO 2020 C++,
: https://reurl.cc/gZGz6L
: https://reurl.cc/XLGlq0
: ISO都有基本的命名規則
: 另查閱 微軟 安卓 程式規範
: 微軟 的 命名規則偏向 The Hungarian Naming Convention
: 由2001 制定完整規範, prefix 如ch, sz, p
: https://idleloop.com/hungarian/
: 2. variable prefix naming convention 一定是正確的嗎?
: (a)
: 北美電網程式規範與openPDC 首席設計師 James Ritchie Carroll
: https://www.gridprotectionalliance.org/docs/GPA_Coding_Guidelines_2011_03.pdf
: Page 12 原文貼上
: Do not use Hungarian notation
: Do not abbreviate
: Do not prefix enums, classes, or delegates with any letter
: (b)
: Linux核心的創始者 開源專案Git創始者 Linus Torvalds
: https://www.kernel.org/doc/html/v4.10/process/coding-style.html
: https://slurm.schedmd.com/coding_style.pdf
: 第四章 原文貼上
: Encoding the type of a function into the name (so-called Hungarian notation)
: is brain damaged - the compiler knows the types anyway and can check those,
: and it only confuses the programmer. No wonder MicroSoft makes buggy programs.
: (注意一下這兩位大神coding在意的重點是什麼)
: 3.
: GNU MAKE
: https://www.gnu.org/software/make/manual/make.html
: #dir named with www.gnu.org/software/make/manual/make.html 4.3 16.3 16.5
: SRCDIR = ./source
: OBJDIR = ./obj
: BINDIR = ./bin
: #compile optione with www.gnu.org/software/make/manual/make.html 4.3 16.3 16.5
: $(OBJDIR)/%.o : ./$(SRCDIR)/%.cpp
: $(CXX) -c $(CXXFLAGS) $< -o $@
: #Note: CPPFLAGS at www.gnu.org/software/make/manual/make.html 10.3
: CC
: Program for compiling C programs; default ‘cc’.
: CXX
: Program for compiling C++ programs; default ‘g++’.
: CPPFLAGS
: Extra flags to give to the C preprocessor and programs that use it (the
: C and Fortran compilers).
: CXXFLAGS
: Extra flags to give to the C++ compiler.
: ※ 引述《heaviest (heaviest)》之銘言:
: : 最近開始學C,剛剛把前幾天寫的程式,打開來看
: : 發現變數一時之間完全搞不清楚
: : 明明當初有盡力的取有意義的名稱,然後照著大寫來分開字這樣打
: : 跑去問了學長,他叫我去背單字,他說變數名字取不出來是我單字被太少QQ
: : 請問各位前輩們都怎麼取有意義的名字
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.93.159 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1679025860.A.E70.html
推
03/17 15:09,
3年前
, 1F
03/17 15:09, 1F
→
03/17 15:09,
3年前
, 2F
03/17 15:09, 2F
→
03/17 15:09,
3年前
, 3F
03/17 15:09, 3F
→
03/17 15:09,
3年前
, 4F
03/17 15:09, 4F
→
03/17 16:10,
3年前
, 5F
03/17 16:10, 5F
推
03/17 16:43,
3年前
, 6F
03/17 16:43, 6F
→
03/17 16:43,
3年前
, 7F
03/17 16:43, 7F
推
03/17 17:59,
3年前
, 8F
03/17 17:59, 8F
推
03/17 21:12,
3年前
, 9F
03/17 21:12, 9F
→
03/17 21:12,
3年前
, 10F
03/17 21:12, 10F
推
03/17 22:14,
3年前
, 11F
03/17 22:14, 11F
推
03/18 07:27,
3年前
, 12F
03/18 07:27, 12F
→
03/18 07:27,
3年前
, 13F
03/18 07:27, 13F
推
03/18 10:37,
3年前
, 14F
03/18 10:37, 14F
→
03/18 19:23,
3年前
, 15F
03/18 19:23, 15F
※ 編輯: fatalfeel2 (218.187.80.32 臺灣), 03/18/2023 19:24:46
推
03/18 21:25,
3年前
, 16F
03/18 21:25, 16F
→
03/18 21:25,
3年前
, 17F
03/18 21:25, 17F
※ 編輯: fatalfeel2 (218.187.80.32 臺灣), 03/19/2023 06:27:11
推
03/19 14:00,
3年前
, 18F
03/19 14:00, 18F
→
03/19 14:00,
3年前
, 19F
03/19 14:00, 19F
→
03/22 19:00, , 20F
03/22 19:00, 20F
→
03/22 19:01, , 21F
03/22 19:01, 21F
→
03/22 19:04, , 22F
03/22 19:04, 22F
推
03/22 22:32, , 23F
03/22 22:32, 23F
→
03/22 22:32, , 24F
03/22 22:32, 24F
推
03/22 22:35, , 25F
03/22 22:35, 25F
→
03/22 22:35, , 26F
03/22 22:35, 26F
推
03/23 00:20, , 27F
03/23 00:20, 27F
→
03/24 05:40, , 28F
03/24 05:40, 28F
→
03/24 12:35, , 29F
03/24 12:35, 29F
→
03/24 12:41, , 30F
03/24 12:41, 30F
→
03/24 19:53, , 31F
03/24 19:53, 31F
推
03/25 09:13, , 32F
03/25 09:13, 32F
→
03/25 09:13, , 33F
03/25 09:13, 33F
→
03/25 09:13, , 34F
03/25 09:13, 34F
→
03/25 09:13, , 35F
03/25 09:13, 35F
→
03/25 09:13, , 36F
03/25 09:13, 36F
→
03/25 09:13, , 37F
03/25 09:13, 37F
還有 84 則推文
→
03/29 19:02, , 122F
03/29 19:02, 122F
→
03/29 19:03, , 123F
03/29 19:03, 123F
→
03/29 19:04, , 124F
03/29 19:04, 124F
→
03/29 19:04, , 125F
03/29 19:04, 125F
→
03/29 19:05, , 126F
03/29 19:05, 126F
→
03/29 19:05, , 127F
03/29 19:05, 127F
→
03/29 19:23, , 128F
03/29 19:23, 128F

→
03/29 20:03, , 129F
03/29 20:03, 129F
→
03/29 20:03, , 130F
03/29 20:03, 130F
→
03/29 20:03, , 131F
03/29 20:03, 131F
→
03/29 20:03, , 132F
03/29 20:03, 132F
推
03/29 21:52, , 133F
03/29 21:52, 133F
→
03/29 21:54, , 134F
03/29 21:54, 134F
→
03/29 21:59, , 135F
03/29 21:59, 135F
推
03/30 23:20, , 136F
03/30 23:20, 136F
→
03/30 23:20, , 137F
03/30 23:20, 137F
推
03/30 23:27, , 138F
03/30 23:27, 138F
→
03/30 23:28, , 139F
03/30 23:28, 139F
推
03/30 23:30, , 140F
03/30 23:30, 140F
→
03/30 23:30, , 141F
03/30 23:30, 141F
推
04/01 22:22, , 142F
04/01 22:22, 142F
→
04/01 22:23, , 143F
04/01 22:23, 143F
→
04/01 22:24, , 144F
04/01 22:24, 144F
推
04/01 23:45, , 145F
04/01 23:45, 145F
→
04/01 23:45, , 146F
04/01 23:45, 146F
推
04/01 23:48, , 147F
04/01 23:48, 147F
→
04/01 23:48, , 148F
04/01 23:48, 148F
→
04/01 23:48, , 149F
04/01 23:48, 149F
→
04/01 23:48, , 150F
04/01 23:48, 150F
→
04/02 04:50, , 151F
04/02 04:50, 151F
→
04/02 04:51, , 152F
04/02 04:51, 152F
推
04/02 22:47, , 153F
04/02 22:47, 153F
→
04/02 22:48, , 154F
04/02 22:48, 154F
推
04/03 04:05, , 155F
04/03 04:05, 155F
→
04/03 04:05, , 156F
04/03 04:05, 156F
→
04/03 04:05, , 157F
04/03 04:05, 157F
→
04/03 04:05, , 158F
04/03 04:05, 158F
推
04/03 12:34, , 159F
04/03 12:34, 159F
推
04/05 13:22, , 160F
04/05 13:22, 160F
推
04/24 02:21, , 161F
04/24 02:21, 161F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
32
161
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章