12 Replies Latest reply on Nov 12, 2014 7:18 AM by user_14962448

    Recommended C Style and Coding Standards

    user_14962448

       Recently I have been scouring the web looking for examples on a particular project (C based).

         
              
         
          Unfortunately or otherwise, I was quickly immersed in a another world,, having to understand coding technique used by a very diverse galaxy of "C" programming styles.  I got most projects to run OK, but lots of time was spent searching the web for unexpected style 'quirks' and 'methods' (my usual 'work' enviroment is Labview).   
         
              
         
          -------- some examples that I discovered, with mixed preferences   
         
              
         
          Best C Coding Practices    
         
              
         
          Coding Techniques and Programming Practices   
         
              
         
          Macro Programming Best Practices: Styles, Guidelines and Conventions Including the Rationale Behind Them   
         
              
         
          Recommended C Style and Coding Standards   
         
              
         
          c programming using globals best practices   
         
              
         
          c programming using extern best practices   
         
              
         
          How to Use C's volatile Keyword   
         
              
         
          --------   
         
              
         
          Any thoughs or comments, esp. relating to Cypress Creator with embedded devices?   
         
              
         
          Does Cypress use an "in-house" guide ?   
         
              
         
          Thanks, Michael.   
        • 1. Re: Recommended C Style and Coding Standards
          user_1377889

          As teaching my customers "Programming" I always urge them to develop a coding standard for their companies. Since there is no global or ultimative standard and the coding rules have to be accepted by all members the best will be if the rules are developed by the members.

             

          I teach that the main goal for a coding standard is to increase the readability of the code to its maximum. This measure will always reduce costs and will ensure a long project-lifetime. How this goal is enforced is always the company's selection. For each of the items a coding standard consists of there are different solutions and there might be a "best" one, but this often is a matter of taste.

             

           

             

          Bob

          • 2. Re: Recommended C Style and Coding Standards
            user_14586677

            I am not aware of any "in house guide".

               

             

               

            Generally speaking your topic is pretty broad, from programming style

               

            thru design for non stop computing.

               

             

               

            Some books cover this as footnotes, the one attached an example.

               

             

               

            Regards, Dana.

            • 3. Re: Recommended C Style and Coding Standards
              user_14962448
                      Thanks very much for the replies folks... I started this thread as I think it may be useful, hence the breadth of my first post. The "C" pdf is very useful, thanks Dana. (I believe I have a copy of the original version... somewhere) Three cheers Bob, I agree 100%. The software I have to update today I first wrote in 1980 something, fortunately I had included 'reasonable' comments and descriptions !! Now, a full 26 years later, I can justify the extra project costs at the time. So on to my query,, is the 'extern' keyword useful/needed ? I need to use a few globals (sorry, I like them) within a psoc tcp routine I am tinkering with. But looking at forum code samples I find few reference to same. Perhaps psoc creator could have a plugin that checks both the layout and design guides. Many thanks, Michael.   
              • 4. Re: Recommended C Style and Coding Standards
                user_14962448

                Thanks very much for the replies folks... 

                   

                 

                   

                 I started this thread as I think it may be useful, hence the breadth of my first post. 

                   

                 The "C" pdf is very useful, thanks Dana. (I believe I have a copy of the original version... somewhere) 

                   

                 Three cheers Bob, I agree 100%. The software I have to update today I first wrote in 1980 something, fortunately I had included 'reasonable' comments and descriptions !! 

                   

                 Now, a full 26 years later, I can justify the extra project costs at the time. 

                   

                 

                   

                 So on to my query,, is the 'extern' keyword useful/needed ? 

                   

                 I need to use a few globals (sorry, I like them) within a psoc tcp routine I am tinkering with. 

                   

                 But looking at forum code samples I find few reference to same. 

                   

                 Perhaps psoc creator could have a plugin that checks both the layout and design guides. 

                   

                 

                   

                Many thanks, Michael.

                   

                p.s. second try with formatting.

                • 5. Re: Recommended C Style and Coding Standards
                  user_14962448

                  Thanks very much for the replies folks... 

                     

                   

                     

                   I started this thread as I think it may be useful, hence the breadth of my first post. 

                     

                   The "C" pdf is very useful, thanks Dana. (I believe I have a copy of the original version... somewhere) 

                     

                   Three cheers Bob, I agree 100%. The software I have to update today I first wrote in 1980 something, fortunately I had included 'reasonable' comments and descriptions !! 

                     

                   Now, a full 26 years later, I can justify the extra project costs at the time. 

                     

                   

                     

                   So on to my query,, is the 'extern' keyword useful/needed ? 

                     

                   I need to use a few globals (sorry, I like them) within a psoc tcp routine I am tinkering with. 

                     

                   But looking at forum code samples I find few reference to same. 

                     

                   Perhaps psoc creator could have a plugin that checks both the layout and design guides. 

                     

                   

                     

                  Many thanks, Michael.

                     

                  p.s. second try with formatting.

                  • 6. Re: Recommended C Style and Coding Standards
                    user_1377889

                    Regarding the "extern" declaration of variables.

                       

                    One step in designing a program-system (project) is the "modularization".  This is the work of Isolating functional parts of the program ang writing a .h and a .c file for them. This step is essential for reducing company costs end ensuring maxmal error-free projects.  The .h-file is then #included into the project or a library is built from the .c-file. Both ensure a common single codebase that can be re-used in every project that requires it.

                       

                    Like global vars, the skope (visibility) of an extern var starts at the point of declaration and lasts to the end of the file. Of course it is possible to define functions that return the required item, but to save code and to avoid side-effects it can be easier to have an external variable defined. This should be done (of course) in the .h-file to avoid "forgotten" storage-classifiers as "volatile".

                       

                     

                       

                    Bob

                    • 7. Re: Recommended C Style and Coding Standards
                      user_1377889

                      ...and here is a paper concerning local and global vars: www.cypress.com/

                         

                       

                         

                      Bob

                      • 8. Re: Recommended C Style and Coding Standards
                        user_14586677

                        I had a project using HiTech in PSOC 1 where I was severly crippled

                           

                        and limited in code space size. Resorting to Globals and the use of

                           

                        pointers to them saved the day, yielded 4K of additional FLASH space.

                           

                        Largely associated with doubles in 8 bit processor.

                           

                         

                           

                        Point is there are two sides to every coin. And there are associated

                           

                        costs using local vs global as Bob points out. Always examine cost

                           

                        benefit of doing something, sometimes the flip side of the coin more

                           

                        beneficial.

                           

                         

                           

                        Regards, Dana.

                        • 9. Re: Recommended C Style and Coding Standards
                          user_14962448

                           Thanks again for taking the time to reply folks.

                             

                          I have noticed some "style" editors/formatters are available on the web, perhaps that could be a positive additon to PSoC Creator. (or a plugin)

                             

                          Cypress seem to have it well sorted, when one reads the 'generated source code'.

                             

                          Regards Michael.

                          • 10. Re: Recommended C Style and Coding Standards
                            user_1377889

                            A coding standard is nothing you could burden a program on, a coding standard has to be mended by us programmers

                               

                            Compare the writings of

                               

                             

                               

                            while(condition);

                               

                            while(condition) Wait();

                               

                             

                               

                            for(;;;)

                               

                            while(forever)

                               

                             

                               

                            a = x1/xx1 * d;

                               

                            Vout = Vmeasure / Vref * TotalGain;

                               

                             

                               

                            No software can do that!

                               

                             

                               

                            Bob

                            • 11. Re: Recommended C Style and Coding Standards
                              peter.taylor

                               Interesting topic and something I've given a lot of thought to over the years as well.  I've gone back and forth a lot.  Currently, in the case of global variables, I use them in very limited situations.  For example, I have some custom code that I added to the UART RX interrupt to set some global flags.  I wouldn't want to be calling functions from an interrupt just to set a flag.  In this case, I didn't even define the extern in the .h, but rather only in the UART's INT.c file.  Even in this case, where it is reasonable to use global variables, it still sticks in my craw a little.

                                 

                              I get a bit obsessed sometimes about formatting.  Just yesterday I caught myself "fixing" the brace after an if statement by moving it up to the same line as the if.  Then I realised that it was Creator generated code that I was "fixing".  Unfortunately, I don't have company-wide rules to fall back on.  I try to be consistent, but it can be hard when you are a "maximizer".

                              • 12. Re: Recommended C Style and Coding Standards
                                user_14962448

                                 I have also been playing with UniversalIndentGUI, an interesting program...