From: "Randy.Dunlap" <rdunlap@xenotime.net>
To: Michael Buesch <mb@bu3sch.de>
Cc: linux-kernel@vger.kernel.org, akpm@osdl.org
Subject: Re: [PATCH] CodingStyle: add typedefs chapter
Date: Mon, 1 May 2006 08:34:26 -0700 [thread overview]
Message-ID: <20060501083426.277cb237.rdunlap@xenotime.net> (raw)
In-Reply-To: <200605010928.33773.mb@bu3sch.de>
On Mon, 1 May 2006 09:28:33 +0200 Michael Buesch wrote:
> On Monday 01 May 2006 02:44, you wrote:
> > + NOTE! Again - there needs to be a _reason_ for this. If something is
> > + "unsigned long", then there's no reason to do
> > +
> > + typedef long myflags_t;
>
> typedef unsigned long myflags_t;
You mean that Linus's original email contained a thinko?
Fixed, thanks.
---
From: Randy Dunlap <rdunlap@xenotime.net>
Add a chapter on typedefs, copied from an email from Linus
to lkml on Feb. 3, 2006.
(Subject: Re: [RFC][PATCH 1/5] Virtualization/containers: startup)
Signed-off-by: Randy Dunlap <rdunlap@xenotime.net>
---
Documentation/CodingStyle | 77 ++++++++++++++++++++++++++++++++++++++--------
1 files changed, 65 insertions(+), 12 deletions(-)
--- linux-2617-rc3.orig/Documentation/CodingStyle
+++ linux-2617-rc3/Documentation/CodingStyle
@@ -155,7 +155,60 @@ problem, which is called the function-gr
See next chapter.
- Chapter 5: Functions
+ Chapter 5: Typedefs
+
+Please don't use things like "vps_t".
+
+It's a _mistake_ to use typedef for structures and pointers. When you see a
+
+ vps_t a;
+
+in the source, what does it mean?
+
+In contrast, if it says
+
+ struct virtual_container *a;
+
+you can actually tell what "a" is.
+
+Lots of people think that typedefs "help readability". Not so. They are
+useful only for:
+
+ (a) totally opaque objects (where the typedef is actively used to _hide_
+ what the object is).
+
+ Example: "pte_t" etc. opaque objects that you can only access using
+ the proper accessor functions.
+
+ NOTE! Opaqueness and "accessor functions" are not good in themselves.
+ The reason we have them for things like pte_t etc. is that there
+ really is absolutely _zero_ portably accessible information there.
+
+ (b) Clear integer types, where the abstraction _helps_ avoid confusion
+ whether it is "int" or "long".
+
+ u8/u16/u32 are perfectly fine typedefs.
+
+ NOTE! Again - there needs to be a _reason_ for this. If something is
+ "unsigned long", then there's no reason to do
+
+ typedef unsigned long myflags_t;
+
+ but if there is a clear reason for why it under certain circumstances
+ might be an "unsigned int" and under other configurations might be
+ "unsigned long", then by all means go ahead and use a typedef.
+
+ (c) when you use sparse to literally create a _new_ type for
+ type-checking.
+
+Maybe there are other cases too, but the rule should basically be to NEVER
+EVER use a typedef unless you can clearly match one of those rules.
+
+In general, a pointer, or a struct that has elements that can reasonably
+be directly accessed should _never_ be a typedef.
+
+
+ Chapter 6: Functions
Functions should be short and sweet, and do just one thing. They should
fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
@@ -183,7 +236,7 @@ and it gets confused. You know you're b
to understand what you did 2 weeks from now.
- Chapter 6: Centralized exiting of functions
+ Chapter 7: Centralized exiting of functions
Albeit deprecated by some people, the equivalent of the goto statement is
used frequently by compilers in form of the unconditional jump instruction.
@@ -220,7 +273,7 @@ out:
return result;
}
- Chapter 7: Commenting
+ Chapter 8: Commenting
Comments are good, but there is also a danger of over-commenting. NEVER
try to explain HOW your code works in a comment: it's much better to
@@ -240,7 +293,7 @@ When commenting the kernel API functions
See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc
for details.
- Chapter 8: You've made a mess of it
+ Chapter 9: You've made a mess of it
That's OK, we all do. You've probably been told by your long-time Unix
user helper that "GNU emacs" automatically formats the C sources for
@@ -288,7 +341,7 @@ re-formatting you may want to take a loo
remember: "indent" is not a fix for bad programming.
- Chapter 9: Configuration-files
+ Chapter 10: Configuration-files
For configuration options (arch/xxx/Kconfig, and all the Kconfig files),
somewhat different indentation is used.
@@ -313,7 +366,7 @@ support for file-systems, for instance)
experimental options should be denoted (EXPERIMENTAL).
- Chapter 10: Data structures
+ Chapter 11: Data structures
Data structures that have visibility outside the single-threaded
environment they are created and destroyed in should always have
@@ -344,7 +397,7 @@ Remember: if another thread can find you
have a reference count on it, you almost certainly have a bug.
- Chapter 11: Macros, Enums and RTL
+ Chapter 12: Macros, Enums and RTL
Names of macros defining constants and labels in enums are capitalized.
@@ -399,7 +452,7 @@ The cpp manual deals with macros exhaust
covers RTL which is used frequently with assembly language in the kernel.
- Chapter 12: Printing kernel messages
+ Chapter 13: Printing kernel messages
Kernel developers like to be seen as literate. Do mind the spelling
of kernel messages to make a good impression. Do not use crippled
@@ -410,7 +463,7 @@ Kernel messages do not have to be termin
Printing numbers in parentheses (%d) adds no value and should be avoided.
- Chapter 13: Allocating memory
+ Chapter 14: Allocating memory
The kernel provides the following general purpose memory allocators:
kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API
@@ -429,7 +482,7 @@ from void pointer to any other pointer t
language.
- Chapter 14: The inline disease
+ Chapter 15: The inline disease
There appears to be a common misperception that gcc has a magic "make me
faster" speedup option called "inline". While the use of inlines can be
@@ -457,7 +510,7 @@ something it would have done anyway.
- Chapter 15: References
+ Appendix I: References
The C Programming Language, Second Edition
by Brian W. Kernighan and Dennis M. Ritchie.
@@ -481,4 +534,4 @@ Kernel CodingStyle, by greg@kroah.com at
http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/
--
-Last updated on 30 December 2005 by a community effort on LKML.
+Last updated on 30 April 2006.
next prev parent reply other threads:[~2006-05-01 15:32 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-05-01 0:44 Randy.Dunlap
2006-05-01 7:28 ` Michael Buesch
2006-05-01 15:34 ` Randy.Dunlap [this message]
2006-05-01 14:00 ` Jan Engelhardt
2006-05-01 14:19 ` Alexey Dobriyan
2006-05-01 14:46 ` linux-os (Dick Johnson)
2006-05-01 15:01 ` Jiri Slaby
2006-05-01 15:33 ` Artem B. Bityutskiy
2006-05-01 16:58 ` David Woodhouse
2006-05-01 20:20 ` Jan Engelhardt
2006-05-01 20:45 ` Jiri Slaby
2006-05-01 21:01 ` Jan Engelhardt
2006-05-01 21:07 ` David Woodhouse
2006-05-01 21:38 ` Alexey Dobriyan
2006-05-02 10:40 ` Jörn Engel
2006-05-02 13:17 ` Jes Sorensen
2006-05-01 17:06 ` David Woodhouse
2006-05-01 20:48 ` Randy.Dunlap
2006-05-02 0:37 ` Johannes Stezenbach
2006-05-02 13:28 ` David Woodhouse
2006-05-02 14:20 ` Johannes Stezenbach
2006-05-02 14:31 ` David Woodhouse
2006-05-02 17:11 ` Randy.Dunlap
2006-05-02 18:41 ` Linus Torvalds
2006-05-02 18:50 ` David Woodhouse
2006-05-02 19:07 ` Linus Torvalds
2006-05-02 19:20 ` Marcel Siegert
2006-05-02 19:44 ` Linus Torvalds
2006-05-02 23:22 ` David Woodhouse
2006-05-03 19:41 ` Randy.Dunlap
2006-05-03 21:52 ` David Woodhouse
2006-05-03 22:09 ` Randy.Dunlap
2006-05-03 22:10 ` David Woodhouse
2006-05-01 21:23 ` Daniel Barkalow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20060501083426.277cb237.rdunlap@xenotime.net \
--to=rdunlap@xenotime.net \
--cc=akpm@osdl.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mb@bu3sch.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®