Advertisement
konalisp

Caesura C++ Coding Standard v1

Oct 25th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. //Caesura Coding Standard, version 1.
  2. //Copyright (C) 2014 Caesura Industries(R)
  3.  
  4. #include <iostream>
  5. #include <functional>
  6. #include <conio.h> //Always include conio.h. Don't know what it does but it's probably important.
  7.  
  8. //Spaces only, 4 spaces per tab.
  9.  
  10. //Do not use "namespace std", or any namespace.
  11. //Just do this, if you really need it. Don't
  12. //put everything here, only things you'll use
  13. //often, like cout and string. And of course,
  14. //using this is optional, in the sense that,
  15. //if you're fine with it, just put std:: in
  16. //front of everything. And don't typedef it
  17. //either, that's just unreadable.
  18. using std::cout;
  19. using std::string;
  20.  
  21. namespace spc {
  22.  
  23.     //Yes, this is how we define functions.
  24.     //Just pretend "auto" is "def" or "defun"
  25.     //and it feels a bit more lispy. It's also
  26.     //more consistent with lambdas.
  27.     auto myfunc (int, int) -> int;
  28.     template<typename T>
  29.     auto newfunc (T) -> T;
  30.    
  31.     class MyClass {
  32.     //public/private don't get tabbed
  33.     //class name is always uppercase,
  34.     //functions and namespaces are lowercase.
  35.     public:
  36.         MyClass();
  37.        ~MyClass(); //Yes the tilde in the destructor goes back a space.
  38.         auto func() -> string;
  39.         auto geta() -> int&;
  40.     private:
  41.         int a;
  42.     };
  43.  
  44. } //Namespace ends here, everything gets defined outside.
  45.  
  46. spc::MyClass::MyClass() : a(3) {
  47.     //always use initializer lists.
  48.     cout << "I'm awake\n";
  49. }
  50.  
  51. spc::MyClass::~MyClass() {
  52.     cout << "I'm done\n";
  53. }
  54.  
  55. auto spc::MyClass::func() -> string {
  56.     return "a is " + std::to_string(a) + "\n";
  57. }
  58.  
  59. auto spc::MyClass::geta() -> int& {
  60.     //Write setters/getters like this
  61.     //for the love of god don't make two
  62.     //separate functions just return
  63.     //a reference.
  64.    
  65.     //By the way, setters are good to
  66.     //control what goes in a variable.
  67.     //Even if you don't need this control,
  68.     //it's good to be consistent.
  69.     return a;
  70. }
  71.  
  72. auto spc::myfunc (int x, int y) -> int {
  73.     //Open brackets don't get a new line, because
  74.     //that's ugly as sin, especially in a world
  75.     //where screens are wider than they are longer.
  76.    
  77.     //each "else" gets a new line.
  78.     if (x == y) {
  79.         return 1;
  80.     }
  81.     else if (x <= y) {
  82.         return y;
  83.     }
  84.     else {
  85.         return x;
  86.     }
  87. }
  88.  
  89. //Of course, templates get their own line.
  90. template<typename T>
  91. auto spc::newfunc (T x) -> T {
  92.     return x;
  93. }
  94.  
  95. auto main(int argc, char **argv) -> int {
  96.     spc::MyClass c;
  97.     cout << c.func();
  98.     c.geta() = 5;
  99.     cout << c.func();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement