[Nut-upsdev] coding style

Peter Selinger selinger at mathstat.dal.ca
Thu Feb 8 22:43:15 CET 2007


Dear developers, 

I would like to make the following additions to the section on coding
style to developers.txt. Any objections / comments ?

-- Peter

Index: docs/developers.txt
===================================================================
--- docs/developers.txt	(revision 799)
+++ docs/developers.txt	(working copy)
@@ -154,6 +154,10 @@
 off the right margin of the screen, expect them to meet the byte
 chainsaw sooner or later.
 
+All types defined with typedef should end in "_t", because this is
+easier to read, and it enables tools (such as indent and emacs) to
+format the source code correctly.
+
 Indenting with tabs vs. spaces
 ------------------------------
 
@@ -175,6 +179,59 @@
 kernel - Documentation/CodingStyle.  He's done a far better job of
 explaining this.
 
+Line breaks
+-----------
+
+It is better to have lines that are longer than 80 characters than to
+wrap lines in random places. This makes it easier to work with tools
+such as "grep", and it also lets each developer resize their editor
+window to their own taste, and have their own tab-width, rather than
+being stuck with one particular design. 
+
+Of course, this does not mean that lines should be made unnecessarily
+long when there is a better alternative (see the note on
+pretentiousVariableNamingSchemes above).  Certainly there should not
+be more than one statement per line. Please do not use
+
+	if (condition) break;
+
+but use the following:
+
+	if (condition) {
+		break;
+	}
+
+Coding style helpers
+--------------------
+
+If you are using emacs, you can automatically adjust the tab-width
+setting to (say) 3 spaces for all NUT sources by putting something
+like this into your .emacs:
+
+ ;; NUT style                                                                    
+ (defun nut-c-mode ()
+  "C mode with adjusted defaults for use with the NUT sources."
+  (interactive)
+  (c-mode)
+  (c-set-style "K&R")
+  (setq c-basic-offset 3)
+  (setq tab-width 3))
+
+ ;; apply NUT style to all C source files in all subdirectories of nut/:
+
+ (setq auto-mode-alist (cons '(".*/nut/.*\\.[ch]$". nut-c-mode)
+                        auto-mode-alist))
+
+You can go a long way towards converting your source code to the NUT
+coding style by piping it through the following command:
+
+ indent -kr -i8 -T FILE -l1000 -nhnl
+
+The following does a reasonable job of converting most C++ style
+comments (but not URLs and DOCTYPE strings):
+
+ sed 's#\(^\|[ \t]\)//[ \t]*\(.*\)[ \t]*#/* \2 */#'
+
 Finishing touches
 -----------------
 



More information about the Nut-upsdev mailing list