Fixing C
embedded.com
Fixing C
1–10 of 123 posts
Re: Fixing C
#2How is this a problem with editors/IDEs doing it all for you? If your editor/IDE does not do this, then.. why don't you have this in your setup?
Re: Fixing C
#3Regarding the curly brackets thing vs. end statements and indentation: How is this a problem with editors/IDEs doing it all for you? If your editor/IDE does not do this, then.. why don't you have this in your setup?
To make matters worse, you actually need to compile the program in order to determine which macro gets expanded into what amount of braces...
Re: Fixing C
#4Re: Fixing C
#5Regarding the curly brackets thing vs. end statements and indentation: How is this a problem with editors/IDEs doing it all for you? If your editor/IDE does not do this, then.. why don't you have this in your setup?
You can do auto-indent in Java, PHP, Perl... but not in C/C++, where you're free to embed curly braces etc. in macros, and it's really common to do so. To make matters worse, you actually need to compile the program in order to determine which macro gets expanded into what amount of braces...
Re: Fixing C
#6Re: Fixing C
#7 > The answer to that is that if you need
> more than 3 levels of indentation, you're screwed anyway,
> and should fix your program.
Breaking the code into manageable chunks is a huge part in writing clear, maintainable software. A high amount of curly braces implies deep nested loops and/or branches, which also implies a high complexity (measured by cyclomatic complexity).Also, IDEs can auto insert braces and indent the code. Unless you're using notepad, I don't see the problem.
Re: Fixing C
#8Maybe Rust or something new will come along sometime soon to fix all the issues that exist with C.
Oh, and for those who haven't seen it, there was a cool guide to writing neat, modern C on here a few months ago. https://matt.sh/howto-c
Worth a read in my opinion.
Re: Fixing C
#9Re: Fixing C
#10 #define begin {
#define end }
and many other Pascalisms. As a result, his code did not look like C at all. IIRC, the code had gone unmaintained since the late 80's, which is not surprising in embedded systems. The code I had to fix was truly cringe-worthy.I vehemently disagree with Ganssle's article. Curly braces are the way to go. I am quite comfortable with pythonic indentation now, but remove one 'if' in complex code, and we have to change all the code below it manually. Cut-paste some code, and we have to manually take care of indentations. It's a pain.
Instead of his proposal, I'd want cleaner syntax for bit-wise addressing, which is currently handled via cumbersome unions or mask macros.