Menu

Fashion Trendy
  • Drop Down

    • Abstract
    • Model
    • Techo
    • Options
  • Photography Pictures Product

    Drop Menu

    • Crystal
    • Digital
    • Graphs
    • Settings
  • Menu

    Stack Now | C, C++, Linux, Design Patterns etc

    • Home
    • Digital Art
      • Pics
        • SEO 1
        • SEO 2
      • CSS
        • CSS 1
        • CSS 2
        • CSS 3
        • CSS 4
        • CSS 5
      • Jquery
        • Jquery 1
        • Jquery 2
    • Fashion
      • Product 1
        • Sub Item
        • Sub Item
      • Product 2
        • Sub Item
        • Sub Item
    • Photography
    • Design
    Go
    Home » C » C++ » Programming Tips » How to Write Better Programs in C/C++?

    How to Write Better Programs in C/C++?

    Writing Better Programs in C/C++
    I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with which you can start writing better programs in C/C++.

    Learning New APIs
    You can improve the programming skills day by day by learning about new APIs.

    For example, in c++ if you know about count API in <algorithm>, you no need to write extra piece of code for getting the number of elements that have same value in the given array.

    By knowing about this type of APIs, you can write effective code and also reduce your LOC(Lines of Code). C++ is so vast and you can read about an API each day and use it in day-to-day programming.

    Using checkpatch Tool
    If you are a C programmer and need to check your code, you can run checkpatch tool.

    checkpatch.pl is a script in the kernel tree that facilitates better C code, and can be used to check many coding style rules.

    You can run the checkpatch tool as below:

    checkpatch.pl --terse --file --no-tree <File Name>

    Writing Comments for Doxygen
    While writing code you need to write proper comments, so that you can generate documentation later using doxygen.

    You can check doxygen documentation for knowing how to write comments in your source code. You need to put extra * after comments for doxygen to read.

    For example for a function declaration, you can write comment as

    /**
    Description of function
    @pre Init function should be called.
    @param [in] win Window object
    @return void
    */
    void add_text(struct window *win);


    Preventing Memory Leaks - Valgrind
    While writing code, you need to have clear understanding about memory allocation and deallocation with which you can prevent memory leaks.

    The basic rule is that for every new there should be delete in C++ whereas for every malloc/calloc there should be free in C.

    When you finish writing your code, you can further know the memory leaks with valgrind. You can run valgrind by using below command

    valgrind --tool=memcheck --leak-check=full <outputfile>

    You will get the detail logs including the function names and line numbers.

    Using cppcheck tool
    Cppcheck is a static analysis tool for your C/C++ code. The features of cppcheck tool are as follows:
    • Automatic variable checking
    • Bounds checking for array overruns
    • Classes checking (e.g. unused functions, variable initialization and memory duplication)
    • Usage of deprecated or superseded functions according to Open Group[13]
    • Exception safety checking, for example usage of memory allocation and destructor checks
    • Memory leaks, e.g. due to lost scope without deallocation
    • Resource leaks, e.g. due to forgetting to close a file handler
    • Invalid usage of Standard Template Library functions and idioms
    • Miscellaneous stylistic and performance errors
    Using Design Patterns
    Design patterns define a solution to the common software design issues. There are many design patterns like creational patterns, structural patterns, behavioral patterns.

    By using these patterns, you can further improve your code structure and reduce design issues

    Exception Handling
    You need to take care of exceptions while writing your code.

    Exceptions provide a way to react to exceptional circumstances like runtime errors(divide by zero) in programs by transferring control to special functions called handlers.

    This is popular in C++ rather than in C as C++ provides special keywords try, catch, throw for handling exceptions.

    I will add more programming tips soon. Check out the space.
    yash
    Add Comment
    C C++ Programming Tips
    Tuesday, March 11, 2014

    facebook

    twitter

    google+

    fb share

    About yash

    Related Posts
    < Previous Post

    Menu
    • Home
    • C
    • C++
    • Linux
    • Design Patterns
    • Programming Tips

    Popular Posts

    • 8 C++ String Functions You Must Know!
      In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string c...
    • How to Print Time in Particular Format in C/C++?
      You can print time in any format using strftime function in C/C++. The function declaration is as below: size_t strftime (char* buf, i...
    • 2-Minute Guide for Patching and Generating a Patch File
      Many times in the project, you need to generate a patch file or patch the file to your project. This 2 minute guide will help you how to g...
    • How to Write Better Programs in C/C++?
      I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with w...
    • Singleton Design Pattern - My View
      I had used singleton design pattern in one of my projects. And in this post I will express my views about it. Singleton design pattern i...

    Social Share

    Weekly Posts

    • 8 C++ String Functions You Must Know!
      In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string c...
    • How to Print Time in Particular Format in C/C++?
      You can print time in any format using strftime function in C/C++. The function declaration is as below: size_t strftime (char* buf, i...
    • 2-Minute Guide for Patching and Generating a Patch File
      Many times in the project, you need to generate a patch file or patch the file to your project. This 2 minute guide will help you how to g...
    • How to Write Better Programs in C/C++?
      I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with w...
    • Singleton Design Pattern - My View
      I had used singleton design pattern in one of my projects. And in this post I will express my views about it. Singleton design pattern i...

    Like us On Facebook

    Labels

    • C
    • C++
    • Design Patterns
    • linux
    • Programming Tips

    Copyright Stack Now | C, C++, Linux, Design Patterns etc 2014 . Template Created by