jesusfv/Comparison-Programming-Languages-Economics — pull request #10 by MattPD (2014-06-20 17:21:33+00:00)
Title: Update RBC_CPP.cpp

- (bug fix) uninitialized variables (default-initialized locals, which results in indeterminate values), values of which are relied upon in the subsequent calculations, are now correctly initialized (value-initialized).

Essentially, the fix amounts to the adherence to the rules EXP33-CPP (Do not reference uninitialized memory) and DCL19-CPP (Initialize automatic local variables on declaration) in the following:
https://www.securecoding.cert.org/confluence/display/cplusplus/EXP33-CPP.+Do+not+reference+uninitialized+memory
https://www.securecoding.cert.org/confluence/display/cplusplus/DCL19-CPP.+Initialize+automatic+local+variables+on+declaration

See also: http://stackoverflow.com/questions/18295302/default-initialization-of-stdarray

- (code simplification, robustness) [related to the above bug fix] fix violations of DCL07-CPP (Minimize the scope of variables and methods)

The scope was broader than necessary; this is more error-prone, as it increases the risk of uninitialized variables (it may also hinder understandability, since it requires a deep understanding of the potential variable interaction / reuse in a broader context than the one it's actually used in):
https://www.securecoding.cert.org/confluence/display/cplusplus/DCL07-CPP.+Minimize+the+scope+of+variables+and+methods
// note: adherence to the rule DCL07-CPP helps with the rules EXP33-CPP and DCL19-CPP as a side benefit

- (code simplification, portability, standard-compliance): replace `get_cpu_time` (relying on platform-specific functionality) with the standard `` library: http://en.cppreference.com/w/cpp/chrono

- (code simplification, readability): replace low-level C-style arrays with `Vector` and `Matrix` where appropriate

Rationale: to aid with the conceptual understanding of the role the respective variables play in the model corresponding to the code. In order to remain compatible with the existing design relying on (compile-time) fixed-size C-style arrays, both `Vector` and `Matrix` are defined in terms of (also compile-time fixed-size) `std::array`.

- (code simplification, portability, standard-compliance): replace `int` with `std::size_t` where appropriate.

This improves the understandability, since it makes it clear that we have a concept corresponding to sample-size in statistics / econometrics -- it may be easier to understand and relate to, esp. for the audience coming from these fields. In particular, only non-negative values make sense in these contexts, which would not be (self-)documented nor ensured by `int`. This improves portability, since `int` is not guaranteed to be wide enough to be used for indexes/sizes. This also complies with what the standard core language & standard libraries do (both C and C++ expect and use `(std::)size_t` for sizes -- see `sizeof` operator, `operator[]` for containers like `std::array` and `std::vector`, etc.).

See:
http://stackoverflow.com/questions/7689288/less-verbose-way-to-declare-multidimensional-stdarray
http://stackoverflow.com/questions/18295302/default-initialization-of-stdarray
http://en.cppreference.com/w/cpp/types/size_t
http://www.viva64.com/en/a/0050/

- (code simplification, robustness) replace magic number `-100000.0` with `-std::numeric_limits::infinity();`

Rationale: to clarify / express the intent of having a value that's always smaller than any other finite value on comparison.

- (code simplification, readability) improve const-correctness

Rationale: to ease the reasoning regarding the variables intended to be modified -- and separate from the `const` values; see:
http://en.wikipedia.org/wiki/Const-correctness
https://isocpp.org/wiki/faq/const-correctness
http://www.possibility.com/Cpp/const.html

- (code robustness) follow the recommended practice (Standard C++ Foundation) on the use of using-directive: http://isocpp.org/wiki/faq/coding-standards#using-namespace-std