What is your coding style? - Programming On Unix

Users browsing this thread: 1 Guest(s)
ckester
Nixers
I confess, I've always been sloppy when it comes to coding style. But I recently added an autocommand to my vimrc to reformat my code before saving it, using astyle(1):

Code:
function! Format()
    set formatprg=astyle\ --mode=c
    silent! execute 'norm! gggqG'
    set formatprg=
endfunction

augroup RunAstyle
     autocmd!
     autocmd BufwritePre *.c silent! call Format()
augroup end

In turn my astylerc enforces rules loosely based on the NetBSD style guidelines, but is still evolving:

Code:
--align-pointer=name
--attach-closing-while
--attach-return-type-decl
--break-blocks
--break-return-type
--indent-col1-comments
--indent=force-tab
--indent-labels
--indent-preproc-block
--indent=tab=8
--max-code-length=80
--pad-header
--pad-oper
--pad-paren-in
--remove-braces
--style=kr
--unpad-paren

Explanation of these astyle settings can be found here.

Names: the shorter the better, but I agree with the consensus that wider scope calls for a longer name. I occasionally lapse into CamelCase or the use of underscores to separate words in a name, but I don't really like either style. When I worked at Microsoft, Hungarian notation was de rigeur, but I absolutely loathe it and don't see how it can achieve its goals without compiler support.

I like the NetBSD rules for ordering #includes. Again, this is something where I've been sloppy in the past.

I still have old-school tendencies when it comes to declaring local variables, putting them at the beginning of the function and sorting by size. But I don't usually bother to sort variables of the same type alphabetically (maybe I should). I understand the value of declaring variables with even narrower scope, but I'm working on breaking old habits.

One thing that interested me in phillbush's styleguide was alphabetically sorting the cases in a switch statement. I've tended to arrange them according to logical sub-groups or in order of likeliness instead. But now I wonder what difference (if any) it makes in the generated code. Something to explore on the next rainy day!


Messages In This Thread
What is your coding style? - by seninha - 25-05-2021, 11:43 AM
RE: What is your coding style? - by venam - 25-05-2021, 12:47 PM
RE: What is your coding style? - by opfez - 25-05-2021, 01:34 PM
RE: What is your coding style? - by jkl - 25-05-2021, 01:41 PM
RE: What is your coding style? - by ckester - 25-05-2021, 02:49 PM
RE: What is your coding style? - by seninha - 25-05-2021, 09:58 PM
RE: What is your coding style? - by s0kx - 26-05-2021, 02:34 AM
RE: What is your coding style? - by venam - 26-05-2021, 09:40 AM
RE: What is your coding style? - by jkl - 26-05-2021, 09:55 AM
RE: What is your coding style? - by opfez - 27-05-2021, 08:27 AM