24 Patterns for Clean Code - Techniques for Faster, Safer Code with Minimal Debugging.pdf

(4684 KB) Pobierz
24 Patterns for Clean Code
Techniques for Faster, Safer Code with
Minimal Debugging
By Robert Beisert
Published January 2016
This work protected under U.S. Copyright Law
Introduction
Document as you go
Treat your documentation as a contract
Use VI operations to keep .h and .c files consistent
Comments - your path back through history
Leave the standards alone - avoid changing defaults (aliases and/or
variables)
2. Debugging and Maintenance
C “classes” using naming conventions
Employ Meaningful Return Types
Use Meaningful Names
Always compile with -g
Log Messages - the more content, the better
Build around Errors, not Successes
Employ the if-else chain
Check the boundaries (including nonsense)
3. Code Conventions
Create in order, Destroy in Reverse Order, Mutex in same order every
time
Yin and Yang - For every alloc, there is a free (and for every {, a } )
Use const to prevent functions from changing the inputs
Don’t mangle your iterators (iterators are used only to iterate)
Headers should not create object code
Prototypes and Optimization - only convert when ready
4. Thought Patterns
One Sentence
Employ the 3-10 rule
Logical Unity of Form and Function
Design: Everything You Need, and Only What You Need
Object Oriented C
Conclusion
Introduction
When I was a child, I thought I could do any number of things. Before I could
talk, I fancied that I was really good at running. In kindergarten, I was proud
of my penmanship and math skills. In elementary school, I honestly thought I
was an excellent video gamer.
However, I was not that good at any of those things.
It’s not like it was exactly my fault. After all, I was still new at these activities.
I had not received the training and the merciless beatings that life throws at us
to make us better.
The same rule applies to programming.
Early on, I became aware that there had to be better ways to do things. My
code was unruly and difficult to read, much less debug. I didn’t know how
some relatively simple things worked. I made the same mistakes again and
again. But I learned.
This book contains some relatively basic approaches for programming in
languages such as C. In fact, although I preferentially work in C, these patterns
demonstrate how to accomplish most tasks faster and easier in any language.
I strongly suggest that you skim this work once, then go back to study the
details. None of these are difficult, but all are essential for optimal
programming.
Zgłoś jeśli naruszono regulamin