Indentation

Give a precise definition of the concept ``well-indented C++ program''. Check whether your definition correctly classifies the following snippets of code. In the case of programs that are not well-indented, find the reason why they are not.

// 1. this is a well-indented C++ program
#include<iostream>
int main(int argc, char** argv) {
  int ret = 0;
  std::cout << "Hello world" << std::endl;
  return ret;
}

// 2. this is a well-indented C++ program
#include<iostream>
const int theGlobalValue = 0;
int main(int argc, char** argv) {
  theGlobalValue = 1;
  std::cout << theGlobalValue << std::endl;
  return 0;
}

// 3. this is NOT a well-indented C++ program (why?)
#include<iostream>
int main(int argc, char** argv) {
  if (argc < 2) {
    std::cerr << "error" << std::endl;
    } else {
      std::cout << argv[1] << std::endl;
  }
}

// 3. this is NOT a well-indented C++ program (why?)
#include<iostream>
  const int theGlobalValue = 0;
  bool isArgcGood(int argc) {
    if (argc < 2) { 
      std::cout << "error" << std::endl; 
      return false; }
    return true;
  }
  int main(int argc, char** argv) {
  if (isArgcGood(argc)) {
    std::cout << argv[1] << std::endl;
  }
  return 0;
}

// 4. this is NOT a well-indented C++ program (why?)
#include<iostream>
int main(int argc, char** argv) {
  int i = 0;
  while(argv[i]) i++;
  std::cout << i << std::endl;
}



Subsections

Leo Liberti 2008-01-12