C _Coding_Style - RicoJia/notes GitHub Wiki

Cosing Style

  • Always initialize a variable.

     int var;  //NOT INITIALIZED, BAD
     if (cond)      // some non-trivial condition
     	Set(&var);
     else if (cond2 || !cond3) {
     	var = Set2(3.14);
     }
     // use var
  • Always use {} initializer syntax

    • applicable
       int arr[] = {1,2,3}
       std::map<std::string, int> mymap {{"scott", 1997}}; 
      const float* pData= new const float[3]{1.1,2.2,3.3};	//TODO
      

    class MyClass{ public: int x; double y; }; Class myClass{2011,3.14}; MyClass myClass1= {2011,3.14}; ```

    • avoid narrowing??
    • Keep in mind that {} initializer and {} will give you std::initializer_list in c++14, not inC++17
  • Always use a smart pointer to hold a raw pointer

    • it's as efficient as a raw pointer
    • manages resources for you
  • Cases

    1. Camel
    2. Snake
⚠️ **GitHub.com Fallback** ⚠️