Saturday, January 8, 2022

Best practices while coding in 'C' language.

1. how do you optimize 'c' code? Ans: 1. Reduce large 'if' or 'else'blocks.If you return anyway from 'if' condition, don't use 'else'. 2. don't use 'static' variables in embedded code.because they occupy ram for whole life time of system. 3.use 'switch' 'case' instead of if else whenever possible. I cannot give exact explanation.But it reduces unforeseen erros in implementation. 2.Some best practices in 'c' Ans: 1. Always implement proper error handling mechanisms.It will make program fail-safe. 2. Make sure to assign deallocated pointer to NULL after free. 3. put debug logs in failure conditions with error code logging. 4. don't use too many 'if','else' conditions. switch statements are better when handling multiple condition checks. 5. make sure to free allocated memory. 6. don't use magic numbers. use MACROS instead. All the Best.

No comments:

The Village-VIII