-
Notifications
You must be signed in to change notification settings - Fork 0
Embedded C coding style
Shiva Karthick edited this page Oct 21, 2023
·
5 revisions
- What is C coding style and do we really need one?
- When there are many coding styles, which do we follow?
- What are some of the things which we need to take note, when working with embedded systems?
- https://github.com/MaJerle/c-code-style
- https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html
- https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs, https://www.kernel.org/doc/html/latest/process/coding-style.html from the man himself, Linus Torvalds!
- Google's C++ coding standards, https://google.github.io/styleguide/cppguide.html
-
https://www.reddit.com/r/embedded/comments/t23pcs/comment/hym2dul/?utm_source=share&utm_medium=web2x&context=3
- Most of these aren't embedded specific, but good to repeat regardless:
- Keep functions small and focused. Some people say by they should never be more than 100 lines. If it starts getting to be more than that it may be time to break things out or simplify.
- Use descriptive names. Don't be afraid to rename things if you come across a bad name! Arguably if things have good names, comments become irrelevant...
- DRY (Don't Repeat Yourself). If you find yourself manually writing duplicate code, find ways to use the language to reduce/eliminate it.
- Use abstractions! Especially try to abstract/mock your hardware so you can test on regular machines. But everything should have multiple "layers" so you can think about them in simpler terms.
- Test as often and as rigorously as you can on as much of your code as possible.
- Optimize late. It's generally a good idea to get things working as early as possible, and then find the areas that need refining/tuning.
- Write for humans, not computers. Generally you should favor readable code to everything else, unless you have a good reason otherwise.
- If something is confusing/difficult to use, write some utilities around it that make it harder to use incorrectly.
- Whenever a computer/tool can do something better than a human can, use it (and make it impossible to not use). Some specific examples:
- Sanitizers (AddressSanitizer and UndefinedBehaviorSanitizer at minimum, but others are also good for special cases).
- Linters ex. clang-tidy
- Code formatting ex. clang-format
- Compile with -WError and as many warnings as you can get.
- Compile (at least hardware-abstracted unit tests) with multiple (ideally modern) compilers
- There are undoubtedly other things that I've forgotten, so please add more if you think of them!
- Macros & the preprocessor, pointers & memory management, concurrency, bit manipulation, when to use volatile and const, signed vs unsigned types, a bit of inline assembly
bitwise operations, pointers, memory management, structs, unions (unions and structs make dealing with saving to flash/eeprom a lot easier), make sure you know the size of the types for the particular mcu/cpu, static, const, volatile, extern, linking, familiarize yourself with the manuals/datasheets for everything you're using, finite state machine or a RTOS depending on resource availability, and what you need
- https://systems-encyclopedia.cs.illinois.edu/articles/bitwise-operations/
- https://www.youtube.com/watch?v=7Iru_LM3qY0
-
I keep a list of questions I've been asked during interviews. Most of these were from one coding review right out of school. https://www.reddit.com/r/embedded/comments/ubm8ut/comment/i658o31/?utm_source=share&utm_medium=web2x&context=3
- What happens to a bit that is 'shifted out'? *0b00000001 >> 1 - where does the 1 go?
- When are parens required in shifting/casting? can you make an example of an operation that doesn't do what you want? (with casting, shifting, math, etc)
- what happens when you dereference a NULL pointer?
- What do you like about c++ (they asked right after saying they only use C...)
- Read these variable declarations (complex declarations)
- when should you use 'const' in function parameters?
- What is the difference if you have the const before or after the _ in : void foo(int _ const bar)" vs "void foo(const int * bar)
- what will happen: if (x + 5 > y / 4)
- whats the difference between an array and a pointer
- what are function callbacks, their uses, and their dangers
- how to optimize a running average function?
- how much work should you do in interrupts, what happens when you call a function that generates another interrupt within an interrupt?
- Read this linker file and explain it
- edit: u/Dark_Tranquility had some good thoughts I totally missed:
- pointers, pointer arithmetic
- uint8_t * foo = 0xab; foo++; foo is what?
- uint8_t _ foo = 0xab; (uint32_t_)foo++; what is foo?
- What does volatile do? When should you use volatile?
- Quantum Leap, OOP in C uses structs and pointers magic to create OOP like structures
- Jacob Sorber
Written and edited by Shiva on October 2023