Migrate to C++17 and refactor

Change-Id: I53407266939258990a1c3a9363c3ebe9ea113fd2
diff --git a/src/ca-module.cpp b/src/ca-module.cpp
index 8b9ba5c..ef41efc 100644
--- a/src/ca-module.cpp
+++ b/src/ca-module.cpp
@@ -35,8 +35,7 @@
 #include <ndn-cxx/util/random.hpp>
 #include <ndn-cxx/util/string-helper.hpp>
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
 const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
@@ -282,11 +281,11 @@
 
   if (requestType == RequestType::NEW) {
     // check the validity period
-    auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
+    auto [notBefore, notAfter] = clientCert->getValidityPeriod().getPeriod();
     auto currentTime = time::system_clock::now();
-    if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
-        expectedPeriod.second > currentTime + m_config.caProfile.maxValidityPeriod ||
-        expectedPeriod.second <= expectedPeriod.first) {
+    if (notBefore < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
+        notAfter > currentTime + m_config.caProfile.maxValidityPeriod ||
+        notAfter <= notBefore) {
       NDN_LOG_ERROR("An invalid validity period is being requested.");
       m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
                                          "An invalid validity period is being requested."));
@@ -472,8 +471,7 @@
 Certificate
 CaModule::issueCertificate(const RequestState& requestState)
 {
-  auto expectedPeriod = requestState.cert.getValidityPeriod().getPeriod();
-  ndn::security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
+  auto period = requestState.cert.getValidityPeriod();
   Certificate newCert;
 
   Name certName = requestState.cert.getKeyName();
@@ -530,5 +528,4 @@
   return result;
 }
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/ca-module.hpp b/src/ca-module.hpp
index 88af9f1..719c3f3 100644
--- a/src/ca-module.hpp
+++ b/src/ca-module.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -28,8 +28,7 @@
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 /**
  * @brief The function would be invoked whenever the certificate request status is updated.
@@ -56,7 +55,7 @@
   }
 
   const std::unique_ptr<CaStorage>&
-  getCaStorage()
+  getCaStorage() const
   {
     return m_storage;
   }
@@ -111,7 +110,6 @@
   std::list<ndn::InterestFilterHandle> m_interestFilterHandles;
 };
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
 
 #endif // NDNCERT_CA_MODULE_HPP
diff --git a/src/challenge/challenge-module.cpp b/src/challenge/challenge-module.cpp
index bf3b966..73594c0 100644
--- a/src/challenge/challenge-module.cpp
+++ b/src/challenge/challenge-module.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -19,6 +19,7 @@
  */
 
 #include "challenge/challenge-module.hpp"
+
 #include <ndn-cxx/util/random.hpp>
 
 namespace ndncert {
@@ -35,15 +36,15 @@
 bool
 ChallengeModule::isChallengeSupported(const std::string& challengeType)
 {
-  ChallengeFactory& factory = getFactory();
+  auto& factory = getFactory();
   auto i = factory.find(challengeType);
-  return i == factory.end() ? false : true;
+  return i != factory.end();
 }
 
 std::unique_ptr<ChallengeModule>
 ChallengeModule::createChallengeModule(const std::string& challengeType)
 {
-  ChallengeFactory& factory = getFactory();
+  auto& factory = getFactory();
   auto i = factory.find(challengeType);
   return i == factory.end() ? nullptr : i->second();
 }
@@ -51,7 +52,7 @@
 ChallengeModule::ChallengeFactory&
 ChallengeModule::getFactory()
 {
-  static ChallengeModule::ChallengeFactory factory;
+  static ChallengeFactory factory;
   return factory;
 }
 
@@ -61,10 +62,9 @@
   uint32_t securityCode = 0;
   do {
     securityCode = ndn::random::generateSecureWord32();
-  }
-  while (securityCode >= 4294000000);
+  } while (securityCode >= 4294000000);
   securityCode /= 4294;
-  std::string result = ndn::to_string(securityCode);
+  std::string result = std::to_string(securityCode);
   while (result.length() < 6) {
     result = "0" + result;
   }
@@ -76,8 +76,8 @@
 {
   request.status = Status::FAILURE;
   request.challengeType = "";
-  request.challengeState = nullopt;
-  return std::make_tuple(errorCode, std::move(errorInfo));
+  request.challengeState = std::nullopt;
+  return {errorCode, std::move(errorInfo)};
 }
 
 std::tuple<ErrorCode, std::string>
@@ -89,7 +89,7 @@
   request.challengeType = CHALLENGE_TYPE;
   request.challengeState = ca::ChallengeState(challengeStatus, time::system_clock::now(), remainingTries,
                                               remainingTime, std::move(challengeSecret));
-  return std::make_tuple(ErrorCode::NO_ERROR, "");
+  return {ErrorCode::NO_ERROR, ""};
 }
 
 std::tuple<ErrorCode, std::string>
@@ -97,8 +97,8 @@
 {
   request.status = Status::PENDING;
   request.challengeType = CHALLENGE_TYPE;
-  request.challengeState = nullopt;
-  return std::make_tuple(ErrorCode::NO_ERROR, "");
+  request.challengeState = std::nullopt;
+  return {ErrorCode::NO_ERROR, ""};
 }
 
 } // namespace ndncert
diff --git a/src/challenge/challenge-module.hpp b/src/challenge/challenge-module.hpp
index 1e33bfc..a0c7b60 100644
--- a/src/challenge/challenge-module.hpp
+++ b/src/challenge/challenge-module.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,32 +23,18 @@
 
 #include "detail/ca-request-state.hpp"
 
+#include <map>
+
 namespace ndncert {
 
 class ChallengeModule : boost::noncopyable
 {
 public:
-  explicit
   ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
 
   virtual
   ~ChallengeModule() = default;
 
-  template <class ChallengeType>
-  static void
-  registerChallengeModule(const std::string& typeName)
-  {
-    ChallengeFactory& factory = getFactory();
-    BOOST_ASSERT(factory.count(typeName) == 0);
-    factory[typeName] = [] { return std::make_unique<ChallengeType>(); };
-  }
-
-  static bool
-  isChallengeSupported(const std::string& challengeType);
-
-  static std::unique_ptr<ChallengeModule>
-  createChallengeModule(const std::string& challengeType);
-
   // For CA
   virtual std::tuple<ErrorCode, std::string>
   handleChallengeRequest(const Block& params, ca::RequestState& request) = 0;
@@ -61,13 +47,27 @@
   genChallengeRequestTLV(Status status, const std::string& challengeStatus,
                          const std::multimap<std::string, std::string>& params) = 0;
 
-  // helpers
+public: // factory
+  template<class ChallengeType>
+  static void
+  registerChallengeModule(const std::string& type)
+  {
+    auto& factory = getFactory();
+    BOOST_ASSERT(factory.count(type) == 0);
+    factory[type] = [] { return std::make_unique<ChallengeType>(); };
+  }
+
+  static bool
+  isChallengeSupported(const std::string& challengeType);
+
+  static std::unique_ptr<ChallengeModule>
+  createChallengeModule(const std::string& challengeType);
+
+protected: // helpers used by concrete challenge modules
   static std::string
   generateSecretCode();
 
-protected:
-  // used by challenge modules
-  std::tuple<ErrorCode, std::string>
+  static std::tuple<ErrorCode, std::string>
   returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string errorInfo);
 
   std::tuple<ErrorCode, std::string>
@@ -79,27 +79,29 @@
 
 public:
   const std::string CHALLENGE_TYPE;
+
+protected:
   const size_t m_maxAttemptTimes;
   const time::seconds m_secretLifetime;
 
 private:
-  typedef std::function<std::unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
-  typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
+  using CreateFunc = std::function<std::unique_ptr<ChallengeModule>()>;
+  using ChallengeFactory = std::map<std::string, CreateFunc>;
 
   static ChallengeFactory&
   getFactory();
 };
 
-#define NDNCERT_REGISTER_CHALLENGE(C, T)                              \
-  static class NdnCert##C##ChallengeRegistrationClass                 \
-  {                                                                   \
-  public:                                                             \
-    NdnCert##C##ChallengeRegistrationClass()                          \
-    {                                                                 \
-      ::ndncert::ChallengeModule::registerChallengeModule<C>(T);      \
-    }                                                                 \
-  } g_NdnCert##C##ChallengeRegistrationVariable
-
 } // namespace ndncert
 
+#define NDNCERT_REGISTER_CHALLENGE(C, T)                            \
+static class NdnCert##C##ChallengeRegistrationClass                 \
+{                                                                   \
+public:                                                             \
+  NdnCert##C##ChallengeRegistrationClass()                          \
+  {                                                                 \
+    ::ndncert::ChallengeModule::registerChallengeModule<C>(T);      \
+  }                                                                 \
+} g_NdnCert##C##ChallengeRegistrationVariable
+
 #endif // NDNCERT_CHALLENGE_MODULE_HPP
diff --git a/src/challenge/challenge-possession.cpp b/src/challenge/challenge-possession.cpp
index d4c8185..2299a51 100644
--- a/src/challenge/challenge-possession.cpp
+++ b/src/challenge/challenge-possession.cpp
@@ -57,7 +57,7 @@
   }
   catch (const boost::property_tree::file_parser_error& error) {
     NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + ": " +
-                                 error.message() + " on line " + ndn::to_string(error.line())));
+                                 error.message() + " on line " + std::to_string(error.line())));
   }
 
   if (config.begin() == config.end()) {
@@ -243,12 +243,12 @@
   const auto& issuedCertTlv = issuedCert.wireEncode();
   auto signature = keyChain.getTpm().sign({nonce}, keyName, ndn::DigestAlgorithm::SHA256);
 
-  for (auto& item : params) {
-    if (item.first == PARAMETER_KEY_CREDENTIAL_CERT) {
-      item.second = std::string(reinterpret_cast<const char*>(issuedCertTlv.wire()), issuedCertTlv.size());
+  for (auto& [key, val] : params) {
+    if (key == PARAMETER_KEY_CREDENTIAL_CERT) {
+      val = std::string(reinterpret_cast<const char*>(issuedCertTlv.wire()), issuedCertTlv.size());
     }
-    else if (item.first == PARAMETER_KEY_PROOF) {
-      item.second = std::string(signature->get<char>(), signature->size());
+    else if (key == PARAMETER_KEY_PROOF) {
+      val = std::string(signature->get<char>(), signature->size());
     }
   }
 }
diff --git a/src/detail/ca-configuration.cpp b/src/detail/ca-configuration.cpp
index 55b8a7c..667d98b 100644
--- a/src/detail/ca-configuration.cpp
+++ b/src/detail/ca-configuration.cpp
@@ -25,8 +25,7 @@
 #include <boost/filesystem.hpp>
 #include <boost/property_tree/json_parser.hpp>
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 void
 CaConfig::load(const std::string& fileName)
@@ -38,33 +37,35 @@
   catch (const std::exception& error) {
     NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
   }
+
   if (configJson.begin() == configJson.end()) {
     NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName));
   }
   caProfile = CaProfile::fromJson(configJson);
-  if (caProfile.supportedChallenges.size() == 0) {
+  if (caProfile.supportedChallenges.empty()) {
     NDN_THROW(std::runtime_error("At least one challenge should be specified."));
   }
-  // parse redirection section if appears
+
+  // parse redirection section if present
   redirection.clear();
   auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION);
   if (redirectionItems) {
     for (const auto& item : *redirectionItems) {
       auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, "");
       auto caCertStr = item.second.get(CONFIG_CERTIFICATE, "");
-      if (caCertStr == "") {
+      if (caCertStr.empty()) {
         NDN_THROW(std::runtime_error("Redirect-to item's certificate cannot be empty."));
       }
       std::istringstream ss(caCertStr);
       auto caCert = ndn::io::load<Certificate>(ss);
-      if (caPrefixStr != "" && Name(caPrefixStr) != caCert->getIdentity()) {
+      if (!caPrefixStr.empty() && Name(caPrefixStr) != caCert->getIdentity()) {
         NDN_THROW(std::runtime_error("Redirect-to item's prefix and certificate does not match."));
       }
 
       auto policyType = item.second.get(CONFIG_REDIRECTION_POLICY_TYPE, "");
       auto policyParam = item.second.get(CONFIG_REDIRECTION_POLICY_PARAM, "");
       if (policyType.empty()) {
-          NDN_THROW(std::runtime_error("Redirect-to policy type expected but not provided."));
+        NDN_THROW(std::runtime_error("Redirect-to policy type expected but not provided."));
       }
       auto policy = RedirectionPolicy::createPolicyFunc(policyType, policyParam);
       if (policy == nullptr) {
@@ -73,12 +74,13 @@
       redirection.emplace_back(caCert, std::move(policy));
     }
   }
-  // parse name assignment if appears
+
+  // parse name assignment if present
   nameAssignmentFuncs.clear();
   auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT);
   if (nameAssignmentItems) {
-    for (const auto& item : *nameAssignmentItems) {
-      auto func = NameAssignmentFunc::createNameAssignmentFunc(item.first, item.second.data());
+    for (const auto& [key, val] : *nameAssignmentItems) {
+      auto func = NameAssignmentFunc::createNameAssignmentFunc(key, val.data());
       if (func == nullptr) {
         NDN_THROW(std::runtime_error("Error on creating name assignment function"));
       }
@@ -87,5 +89,4 @@
   }
 }
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-configuration.hpp b/src/detail/ca-configuration.hpp
index 0a02777..5f39887 100644
--- a/src/detail/ca-configuration.hpp
+++ b/src/detail/ca-configuration.hpp
@@ -21,12 +21,11 @@
 #ifndef NDNCERT_DETAIL_CA_CONFIGURATION_HPP
 #define NDNCERT_DETAIL_CA_CONFIGURATION_HPP
 
+#include "ca-profile.hpp"
 #include "name-assignment/assignment-func.hpp"
 #include "redirection/redirection-policy.hpp"
-#include "ca-profile.hpp"
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 /**
  * @brief CA's configuration on NDNCERT.
@@ -74,7 +73,6 @@
   std::vector<std::unique_ptr<NameAssignmentFunc>> nameAssignmentFuncs;
 };
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
 
 #endif // NDNCERT_DETAIL_CA_CONFIGURATION_HPP
diff --git a/src/detail/ca-memory.cpp b/src/detail/ca-memory.cpp
index b3c5efd..cab365b 100644
--- a/src/detail/ca-memory.cpp
+++ b/src/detail/ca-memory.cpp
@@ -20,13 +20,12 @@
 
 #include "detail/ca-memory.hpp"
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 const std::string CaMemory::STORAGE_TYPE = "ca-storage-memory";
 NDNCERT_REGISTER_CA_STORAGE(CaMemory);
 
-CaMemory::CaMemory(const Name& caName, const std::string& path)
+CaMemory::CaMemory(const Name&, const std::string&)
   : CaStorage()
 {
 }
@@ -34,21 +33,18 @@
 RequestState
 CaMemory::getRequest(const RequestId& requestId)
 {
-  auto search = m_requests.find(requestId);
-  if (search == m_requests.end()) {
-    NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId) + " does not exists"));
+  auto it = m_requests.find(requestId);
+  if (it == m_requests.end()) {
+    NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId) + " does not exist"));
   }
-  return search->second;
+  return it->second;
 }
 
 void
 CaMemory::addRequest(const RequestState& request)
 {
-  auto search = m_requests.find(request.requestId);
-  if (search == m_requests.end()) {
-    m_requests.insert(std::make_pair(request.requestId, request));
-  }
-  else {
+  auto result = m_requests.insert({request.requestId, request});
+  if (!result.second) {
     NDN_THROW(std::runtime_error("Request " + ndn::toHex(request.requestId) + " already exists"));
   }
 }
@@ -56,23 +52,13 @@
 void
 CaMemory::updateRequest(const RequestState& request)
 {
-  auto search = m_requests.find(request.requestId);
-  if (search == m_requests.end()) {
-    m_requests.insert(std::make_pair(request.requestId, request));
-  }
-  else {
-    search->second = request;
-  }
+  m_requests.insert_or_assign(request.requestId, request);
 }
 
 void
 CaMemory::deleteRequest(const RequestId& requestId)
 {
-  auto search = m_requests.find(requestId);
-  auto keyName = search->second.cert.getKeyName();
-  if (search != m_requests.end()) {
-    m_requests.erase(search);
-  }
+  m_requests.erase(requestId);
 }
 
 std::list<RequestState>
@@ -97,5 +83,4 @@
   return result;
 }
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-memory.hpp b/src/detail/ca-memory.hpp
index 6d8b115..2c78997 100644
--- a/src/detail/ca-memory.hpp
+++ b/src/detail/ca-memory.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,25 +23,20 @@
 
 #include "detail/ca-storage.hpp"
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 class CaMemory : public CaStorage
 {
 public:
-  CaMemory(const Name& caName = Name(), const std::string& path = "");
-  const static std::string STORAGE_TYPE;
+  static const std::string STORAGE_TYPE;
+
+  explicit
+  CaMemory(const Name& caName = "", const std::string& path = "");
 
 public:
-  /**
-   * @throw if request cannot be fetched from underlying data storage
-   */
   RequestState
   getRequest(const RequestId& requestId) override;
 
-  /**
-   * @throw if there is an existing request with the same request ID
-   */
   void
   addRequest(const RequestState& request) override;
 
@@ -61,7 +56,6 @@
   std::map<RequestId, RequestState> m_requests;
 };
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
 
 #endif // NDNCERT_DETAIL_CA_MEMORY_HPP
diff --git a/src/detail/ca-profile.cpp b/src/detail/ca-profile.cpp
index 624ec7a..41c5d56 100644
--- a/src/detail/ca-profile.cpp
+++ b/src/detail/ca-profile.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -41,7 +41,7 @@
   // CA max validity period
   profile.maxValidityPeriod = time::seconds(json.get(CONFIG_MAX_VALIDITY_PERIOD, 86400));
   // CA max suffix length
-  profile.maxSuffixLength = nullopt;
+  profile.maxSuffixLength = std::nullopt;
   auto maxSuffixLength = json.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH);
   if (maxSuffixLength) {
     profile.maxSuffixLength = *maxSuffixLength;
@@ -78,7 +78,7 @@
   // anchor certificate
   profile.cert = nullptr;
   auto certificateStr = json.get(CONFIG_CERTIFICATE, "");
-  if (certificateStr != "") {
+  if (!certificateStr.empty()) {
     std::istringstream ss(certificateStr);
     profile.cert = ndn::io::load<Certificate>(ss);
   }
@@ -100,7 +100,7 @@
     for (const auto& key : probeParameterKeys) {
       JsonSection keyJson;
       keyJson.put(CONFIG_PROBE_PARAMETER, key);
-      probeParametersJson.push_back(std::make_pair("", keyJson));
+      probeParametersJson.push_back({"", keyJson});
     }
     caItem.add_child("", probeParametersJson);
   }
@@ -109,7 +109,7 @@
     for (const auto& challenge : supportedChallenges) {
       JsonSection challengeJson;
       challengeJson.put(CONFIG_CHALLENGE, challenge);
-      challengeListJson.push_back(std::make_pair("", challengeJson));
+      challengeListJson.push_back({"", challengeJson});
     }
     caItem.add_child("", challengeListJson);
   }
diff --git a/src/detail/ca-profile.hpp b/src/detail/ca-profile.hpp
index 74af5c0..620f49b 100644
--- a/src/detail/ca-profile.hpp
+++ b/src/detail/ca-profile.hpp
@@ -83,7 +83,7 @@
    * E.g., When its value is 2, at most 2 name components can be assigned after m_caPrefix.
    * Default: none.
    */
-  optional<size_t> maxSuffixLength = nullopt;
+  std::optional<size_t> maxSuffixLength;
   /**
    * @brief A list of supported challenges. Only CA side will have m_supportedChallenges.
    */
diff --git a/src/detail/ca-request-state.hpp b/src/detail/ca-request-state.hpp
index 2c4a1d6..5fbb8f7 100644
--- a/src/detail/ca-request-state.hpp
+++ b/src/detail/ca-request-state.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -27,7 +27,7 @@
 
 namespace ndncert {
 
-typedef std::array<uint8_t, 8> RequestId;
+using RequestId = std::array<uint8_t, 8>;
 
 enum class Status : uint16_t {
   BEFORE_CHALLENGE = 0,
@@ -127,7 +127,7 @@
   /**
    * @brief The challenge state.
    */
-  optional<ChallengeState> challengeState;
+  std::optional<ChallengeState> challengeState;
 };
 
 std::ostream&
diff --git a/src/detail/ca-sqlite.cpp b/src/detail/ca-sqlite.cpp
index 0632edc..3794ce0 100644
--- a/src/detail/ca-sqlite.cpp
+++ b/src/detail/ca-sqlite.cpp
@@ -28,8 +28,7 @@
 #include <boost/filesystem.hpp>
 #include <boost/property_tree/json_parser.hpp>
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 using ndn::util::Sqlite3Statement;
 
@@ -148,7 +147,7 @@
     std::memcpy(state.encryptionKey.data(), statement.getBlob(11), statement.getSize(11));
     state.encryptionIv = std::vector<uint8_t>(statement.getBlob(12), statement.getBlob(12) + statement.getSize(12));
     state.decryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
-    if (state.challengeType != "") {
+    if (!state.challengeType.empty()) {
       ChallengeState challengeState(statement.getString(3), time::fromIsoString(statement.getString(7)),
                                     statement.getInt(8), time::seconds(statement.getInt(9)),
                                     convertString2Json(statement.getString(6)));
@@ -279,7 +278,7 @@
     std::memcpy(state.encryptionKey.data(), statement.getBlob(12), statement.getSize(12));
     state.encryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
     state.decryptionIv = std::vector<uint8_t>(statement.getBlob(14), statement.getBlob(14) + statement.getSize(14));
-    if (state.challengeType != "") {
+    if (!state.challengeType.empty()) {
       ChallengeState challengeState(statement.getString(4), time::fromIsoString(statement.getString(8)),
                                     statement.getInt(9), time::seconds(statement.getInt(10)),
                                     convertString2Json(statement.getString(7)));
@@ -299,5 +298,4 @@
   statement.step();
 }
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-sqlite.hpp b/src/detail/ca-sqlite.hpp
index 969cacf..5ad10d9 100644
--- a/src/detail/ca-sqlite.hpp
+++ b/src/detail/ca-sqlite.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -25,13 +25,12 @@
 
 struct sqlite3;
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 class CaSqlite : public CaStorage
 {
 public:
-  const static std::string STORAGE_TYPE;
+  static const std::string STORAGE_TYPE;
 
   explicit
   CaSqlite(const Name& caName, const std::string& path = "");
@@ -39,15 +38,9 @@
   ~CaSqlite() override;
 
 public:
-  /**
-   * @throw if request cannot be fetched from underlying data storage
-   */
   RequestState
   getRequest(const RequestId& requestId) override;
 
-  /**
-   * @throw if there is an existing request with the same request ID
-   */
   void
   addRequest(const RequestState& request) override;
 
@@ -67,7 +60,6 @@
   sqlite3* m_database;
 };
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
 
 #endif // NDNCERT_DETAIL_CA_SQLITE_HPP
diff --git a/src/detail/ca-storage.cpp b/src/detail/ca-storage.cpp
index 24fe38f..c3c08a7 100644
--- a/src/detail/ca-storage.cpp
+++ b/src/detail/ca-storage.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -20,13 +20,12 @@
 
 #include "detail/ca-storage.hpp"
 
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
 
 std::unique_ptr<CaStorage>
 CaStorage::createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path)
 {
-  CaStorageFactory& factory = getFactory();
+  auto& factory = getFactory();
   auto i = factory.find(caStorageType);
   return i == factory.end() ? nullptr : i->second(caName, path);
 }
@@ -34,9 +33,8 @@
 CaStorage::CaStorageFactory&
 CaStorage::getFactory()
 {
-  static CaStorage::CaStorageFactory factory;
+  static CaStorageFactory factory;
   return factory;
 }
 
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-storage.hpp b/src/detail/ca-storage.hpp
index 0fa7238..6b8f03c 100644
--- a/src/detail/ca-storage.hpp
+++ b/src/detail/ca-storage.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,20 +23,24 @@
 
 #include "detail/ca-request-state.hpp"
 
-namespace ndncert {
-namespace ca {
+#include <map>
+
+namespace ndncert::ca {
 
 class CaStorage : boost::noncopyable
 {
-public: // request related
+public:
+  virtual
+  ~CaStorage() = default;
+
   /**
-   * @throw if request cannot be fetched from underlying data storage
+   * @throw std::runtime_error The request cannot be fetched from underlying data storage
    */
   virtual RequestState
   getRequest(const RequestId& requestId) = 0;
 
   /**
-   * @throw if there is an existing request with the same request ID
+   * @throw std::runtime_error There is an existing request with the same request ID
    */
   virtual void
   addRequest(const RequestState& request) = 0;
@@ -56,11 +60,11 @@
 public: // factory
   template<class CaStorageType>
   static void
-  registerCaStorage(const std::string& caStorageType = CaStorageType::STORAGE_TYPE)
+  registerCaStorage(const std::string& type = CaStorageType::STORAGE_TYPE)
   {
-    CaStorageFactory& factory = getFactory();
-    BOOST_ASSERT(factory.count(caStorageType) == 0);
-    factory[caStorageType] = [] (const Name& caName, const std::string& path) {
+    auto& factory = getFactory();
+    BOOST_ASSERT(factory.count(type) == 0);
+    factory[type] = [] (const Name& caName, const std::string& path) {
       return std::make_unique<CaStorageType>(caName, path);
     };
   }
@@ -68,28 +72,24 @@
   static std::unique_ptr<CaStorage>
   createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path);
 
-  virtual
-  ~CaStorage() = default;
-
 private:
-  using CaStorageCreateFunc = std::function<std::unique_ptr<CaStorage> (const Name&, const std::string&)>;
-  using CaStorageFactory = std::map<std::string, CaStorageCreateFunc>;
+  using CreateFunc = std::function<std::unique_ptr<CaStorage>(const Name&, const std::string&)>;
+  using CaStorageFactory = std::map<std::string, CreateFunc>;
 
   static CaStorageFactory&
   getFactory();
 };
 
-#define NDNCERT_REGISTER_CA_STORAGE(C)                           \
-static class NdnCert ## C ## CaStorageRegistrationClass          \
-{                                                                \
-public:                                                          \
-  NdnCert ## C ## CaStorageRegistrationClass()                   \
-  {                                                              \
-    ::ndncert::ca::CaStorage::registerCaStorage<C>();            \
-  }                                                              \
-} g_NdnCert ## C ## CaStorageRegistrationVariable
+} // namespace ndncert::ca
 
-} // namespace ca
-} // namespace ndncert
+#define NDNCERT_REGISTER_CA_STORAGE(C)                        \
+static class NdnCert##C##CaStorageRegistrationClass           \
+{                                                             \
+public:                                                       \
+  NdnCert##C##CaStorageRegistrationClass()                    \
+  {                                                           \
+    ::ndncert::ca::CaStorage::registerCaStorage<C>();         \
+  }                                                           \
+} g_NdnCert##C##CaStorageRegistrationVariable
 
 #endif // NDNCERT_DETAIL_CA_STORAGE_HPP
diff --git a/src/detail/challenge-encoder.cpp b/src/detail/challenge-encoder.cpp
index 1a7a83d..9f36042 100644
--- a/src/detail/challenge-encoder.cpp
+++ b/src/detail/challenge-encoder.cpp
@@ -20,10 +20,10 @@
 
 #include "detail/challenge-encoder.hpp"
 
-namespace ndncert {
+namespace ndncert::challengetlv {
 
 Block
-challengetlv::encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
+encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
 {
   Block response(tlv::EncryptedPayload);
   response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::Status, static_cast<uint64_t>(request.status)));
@@ -52,7 +52,7 @@
 }
 
 void
-challengetlv::decodeDataContent(const Block& contentBlock, requester::Request& state)
+decodeDataContent(const Block& contentBlock, requester::Request& state)
 {
   auto result = decodeBlockWithAesGcm128(contentBlock, state.m_aesKey.data(),
                                          state.m_requestId.data(), state.m_requestId.size(),
@@ -122,4 +122,4 @@
   }
 }
 
-} // namespace ndncert
+} // namespace ndncert::challengetlv
diff --git a/src/detail/challenge-encoder.hpp b/src/detail/challenge-encoder.hpp
index 2013061..5c502d2 100644
--- a/src/detail/challenge-encoder.hpp
+++ b/src/detail/challenge-encoder.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -24,8 +24,7 @@
 #include "detail/ca-request-state.hpp"
 #include "requester-request.hpp"
 
-namespace ndncert {
-namespace challengetlv {
+namespace ndncert::challengetlv {
 
 Block
 encodeDataContent(ca::RequestState& request, const Name& issuedCertName = Name());
@@ -33,7 +32,6 @@
 void
 decodeDataContent(const Block& contentBlock, requester::Request& state);
 
-} // namespace challengetlv
-} // namespace ndncert
+} // namespace ndncert::challengetlv
 
 #endif // NDNCERT_DETAIL_CHALLENGE_ENCODER_HPP
diff --git a/src/detail/error-encoder.cpp b/src/detail/error-encoder.cpp
index 990ce84..3dcb6ca 100644
--- a/src/detail/error-encoder.cpp
+++ b/src/detail/error-encoder.cpp
@@ -22,10 +22,10 @@
 
 NDN_LOG_INIT(ndncert.encode.error);
 
-namespace ndncert {
+namespace ndncert::errortlv {
 
 Block
-errortlv::encodeDataContent(ErrorCode errorCode, const std::string& description)
+encodeDataContent(ErrorCode errorCode, const std::string& description)
 {
   Block response(ndn::tlv::Content);
   response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<size_t>(errorCode)));
@@ -35,7 +35,7 @@
 }
 
 std::tuple<ErrorCode, std::string>
-errortlv::decodefromDataContent(const Block& block)
+decodefromDataContent(const Block& block)
 {
   try {
     block.parse();
@@ -47,34 +47,35 @@
     for (const auto& item : block.elements()) {
       if (item.type() == tlv::ErrorCode) {
         error = static_cast<ErrorCode>(readNonNegativeInteger(block.get(tlv::ErrorCode)));
-        codeCount ++;
+        codeCount++;
       }
       else if (item.type() == tlv::ErrorInfo) {
         errorInfo = readString(block.get(tlv::ErrorInfo));
-        infoCount ++;
+        infoCount++;
       }
       else if (ndn::tlv::isCriticalType(item.type())) {
-        otherCriticalCount ++;
+        otherCriticalCount++;
       }
       else {
         //ignore
       }
     }
     if (codeCount == 0 && infoCount == 0) {
-      return std::make_tuple(ErrorCode::NO_ERROR, "");
+      return {ErrorCode::NO_ERROR, ""};
     }
     if (codeCount != 1 || infoCount != 1) {
       NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(codeCount) + " error code(s) and " +
-                                      std::to_string(infoCount) + "error info(s), instead of expected 1 times each."));
+                                   std::to_string(infoCount) + " error info(s), instead of expected 1 time each."));
     }
     if (otherCriticalCount > 0) {
       NDN_THROW(std::runtime_error("Unknown Critical TLV type in error packet"));
     }
-    return std::make_tuple(error, errorInfo);
-  } catch (const std::exception& e) {
-    NDN_LOG_ERROR("[errortlv::DecodeFromDataContent] Exception in error message decoding: " << e.what());
-    return std::make_tuple(ErrorCode::NO_ERROR, "");
+    return {error, errorInfo};
+  }
+  catch (const std::exception& e) {
+    NDN_LOG_ERROR("Exception in error message decoding: " << e.what());
+    return {ErrorCode::NO_ERROR, ""};
   }
 }
 
-} // namespace ndncert
+} // namespace ndncert::errortlv
diff --git a/src/detail/error-encoder.hpp b/src/detail/error-encoder.hpp
index cd136cf..57fdf92 100644
--- a/src/detail/error-encoder.hpp
+++ b/src/detail/error-encoder.hpp
@@ -23,8 +23,7 @@
 
 #include "detail/ca-profile.hpp"
 
-namespace ndncert {
-namespace errortlv {
+namespace ndncert::errortlv {
 
 /**
  * Encode error information into a Data content TLV
@@ -38,7 +37,6 @@
 std::tuple<ErrorCode, std::string>
 decodefromDataContent(const Block& block);
 
-} // namespace errortlv
-} // namespace ndncert
+} // namespace ndncert::errortlv
 
 #endif // NDNCERT_DETAIL_ERROR_ENCODER_HPP
diff --git a/src/detail/info-encoder.cpp b/src/detail/info-encoder.cpp
index 9331f1f..2a9df67 100644
--- a/src/detail/info-encoder.cpp
+++ b/src/detail/info-encoder.cpp
@@ -22,15 +22,15 @@
 
 NDN_LOG_INIT(ndncert.encode.info);
 
-namespace ndncert {
+namespace ndncert::infotlv {
 
 Block
-infotlv::encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
+encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
 {
   Block content(ndn::tlv::Content);
   content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.caPrefix));
-  std::string caInfo = "";
-  if (caConfig.caInfo == "") {
+  std::string caInfo;
+  if (caConfig.caInfo.empty()) {
     caInfo = "Issued by " + certificate.getSignatureInfo().getKeyLocator().getName().toUri();
   }
   else {
@@ -48,7 +48,8 @@
 }
 
 CaProfile
-infotlv::decodeDataContent(const Block& block) {
+decodeDataContent(const Block& block)
+{
   CaProfile result;
   block.parse();
   for (auto const &item : block.elements()) {
@@ -74,13 +75,10 @@
         if (ndn::tlv::isCriticalType(item.type())) {
           NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
         }
-        else {
-          //ignore
-        }
         break;
     }
   }
   return result;
 }
 
-} // namespace ndncert
+} // namespace ndncert::infotlv
diff --git a/src/detail/info-encoder.hpp b/src/detail/info-encoder.hpp
index 4eaa967..e4a9cab 100644
--- a/src/detail/info-encoder.hpp
+++ b/src/detail/info-encoder.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,8 +23,7 @@
 
 #include "detail/ca-profile.hpp"
 
-namespace ndncert {
-namespace infotlv {
+namespace ndncert::infotlv {
 
 /**
  * Encode CA configuration and its certificate into a TLV block as INFO Data packet content.
@@ -38,7 +37,6 @@
 CaProfile
 decodeDataContent(const Block& block);
 
-} // namespace infotlv
-} // namespace ndncert
+} // namespace ndncert::infotlv
 
 #endif // NDNCERT_DETAIL_INFO_ENCODER_HPP
diff --git a/src/detail/ndncert-common.hpp b/src/detail/ndncert-common.hpp
index d9863a5..5549550 100644
--- a/src/detail/ndncert-common.hpp
+++ b/src/detail/ndncert-common.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -37,8 +37,11 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <memory>
+#include <optional>
 #include <string>
 #include <tuple>
+#include <vector>
 
 #include <ndn-cxx/data.hpp>
 #include <ndn-cxx/encoding/block.hpp>
@@ -48,7 +51,6 @@
 #include <ndn-cxx/security/certificate.hpp>
 #include <ndn-cxx/util/exception.hpp>
 #include <ndn-cxx/util/logger.hpp>
-#include <ndn-cxx/util/optional.hpp>
 #include <ndn-cxx/util/time.hpp>
 
 #include <boost/algorithm/string.hpp>
@@ -65,9 +67,6 @@
 using ndn::SignatureInfo;
 using ndn::security::Certificate;
 
-using ndn::optional;
-using ndn::nullopt;
-
 namespace time = ndn::time;
 using namespace ndn::time_literals;
 using namespace std::string_literals;
diff --git a/src/detail/probe-encoder.cpp b/src/detail/probe-encoder.cpp
index 13b040e..cdace7e 100644
--- a/src/detail/probe-encoder.cpp
+++ b/src/detail/probe-encoder.cpp
@@ -20,10 +20,10 @@
 
 #include "detail/probe-encoder.hpp"
 
-namespace ndncert {
+namespace ndncert::probetlv {
 
 Block
-probetlv::encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters)
+encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters)
 {
   Block content(ndn::tlv::ApplicationParameters);
   for (const auto& items : parameters) {
@@ -35,7 +35,8 @@
 }
 
 std::multimap<std::string, std::string>
-probetlv::decodeApplicationParameters(const Block& block) {
+decodeApplicationParameters(const Block& block)
+{
   std::multimap<std::string, std::string> result;
   block.parse();
   const auto& elements = block.elements();
@@ -56,8 +57,8 @@
 }
 
 Block
-probetlv::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength,
-                            std::vector<ndn::Name> redirectionItems)
+encodeDataContent(const std::vector<Name>& identifiers, std::optional<size_t> maxSuffixLength,
+                  const std::vector<Name>& redirectionItems)
 {
   Block content(ndn::tlv::Content);
   for (const auto& name : identifiers) {
@@ -78,9 +79,9 @@
 }
 
 void
-probetlv::decodeDataContent(const Block& block,
-                            std::vector<std::pair<Name, int>>& availableNames,
-                            std::vector<Name>& availableRedirection) {
+decodeDataContent(const Block& block, std::vector<std::pair<Name, int>>& availableNames,
+                  std::vector<Name>& availableRedirection)
+{
   block.parse();
   for (const auto& item : block.elements()) {
     if (item.type() == tlv::ProbeResponse) {
@@ -121,4 +122,4 @@
   }
 }
 
-} // namespace ndncert
+} // namespace ndncert::probetlv
diff --git a/src/detail/probe-encoder.hpp b/src/detail/probe-encoder.hpp
index 223c61b..bd15463 100644
--- a/src/detail/probe-encoder.hpp
+++ b/src/detail/probe-encoder.hpp
@@ -23,27 +23,26 @@
 
 #include "detail/ndncert-common.hpp"
 
-namespace ndncert {
-namespace probetlv {
+namespace ndncert::probetlv {
 
 // For Client use
 Block
 encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters);
 
 void
-decodeDataContent(const Block& block, std::vector<std::pair<Name, int>>& availableNames,
+decodeDataContent(const Block& block,
+                  std::vector<std::pair<Name, int>>& availableNames,
                   std::vector<Name>& availableRedirection);
 
 // For CA use
 Block
 encodeDataContent(const std::vector<Name>& identifiers,
-                  optional<size_t> maxSuffixLength = nullopt,
-                  std::vector<ndn::Name> redirectionItems = std::vector<ndn::Name>());
+                  std::optional<size_t> maxSuffixLength = std::nullopt,
+                  const std::vector<Name>& redirectionItems = {});
 
 std::multimap<std::string, std::string>
 decodeApplicationParameters(const Block& block);
 
-} // namespace probetlv
-} // namespace ndncert
+} // namespace ndncert::probetlv
 
 #endif // NDNCERT_DETAIL_PROBE_ENCODER_HPP
diff --git a/src/detail/profile-storage.cpp b/src/detail/profile-storage.cpp
index 8ae2bb6..ebd33ac 100644
--- a/src/detail/profile-storage.cpp
+++ b/src/detail/profile-storage.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -20,11 +20,9 @@
 
 #include "detail/profile-storage.hpp"
 
-#include <boost/filesystem.hpp>
 #include <boost/property_tree/json_parser.hpp>
 
-namespace ndncert {
-namespace requester {
+namespace ndncert::requester {
 
 void
 ProfileStorage::load(const std::string& fileName)
@@ -47,13 +45,12 @@
 {
   m_caProfiles.clear();
   auto caList = json.get_child("ca-list");
-  for (auto item : caList) {
-    CaProfile caItem;
-    caItem = CaProfile::fromJson(item.second);
-    if (caItem.cert == nullptr) {
+  for (const auto& item : caList) {
+    auto profile = CaProfile::fromJson(item.second);
+    if (profile.cert == nullptr) {
       NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration."));
     }
-    m_caProfiles.push_back(std::move(caItem));
+    m_caProfiles.push_back(std::move(profile));
   }
 }
 
@@ -62,14 +59,12 @@
 {
   JsonSection configJson;
   for (const auto& caItem : m_caProfiles) {
-    configJson.push_back(std::make_pair("", caItem.toJson()));
+    configJson.push_back({"", caItem.toJson()});
   }
   std::stringstream ss;
   boost::property_tree::write_json(ss, configJson);
-  std::ofstream configFile;
-  configFile.open(fileName);
+  std::ofstream configFile(fileName);
   configFile << ss.str();
-  configFile.close();
 }
 
 void
@@ -96,5 +91,4 @@
   return m_caProfiles;
 }
 
-} // namespace requester
-} // namespace ndncert
+} // namespace ndncert::requester
diff --git a/src/detail/profile-storage.hpp b/src/detail/profile-storage.hpp
index 3df6407..41791ed 100644
--- a/src/detail/profile-storage.hpp
+++ b/src/detail/profile-storage.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,8 +23,7 @@
 
 #include "detail/ca-profile.hpp"
 
-namespace ndncert {
-namespace requester {
+namespace ndncert::requester {
 
 /**
  * @brief CA profiles kept by a requester.
@@ -53,8 +52,7 @@
 
   /**
    * @brief Add a new CA profile
-   *
-   * Be cautious. This will add a new trust anchor for requesters.
+   * @warning This will add a new trust anchor for requesters.
    */
   void
   addCaProfile(const CaProfile& profile);
@@ -66,7 +64,6 @@
   std::list<CaProfile> m_caProfiles;
 };
 
-} // namespace requester
-} // namespace ndncert
+} // namespace ndncert::requester
 
 #endif // NDNCERT_DETAIL_PROFILE_STORAGE_HPP
diff --git a/src/detail/request-encoder.hpp b/src/detail/request-encoder.hpp
index e94571e..a5b80f6 100644
--- a/src/detail/request-encoder.hpp
+++ b/src/detail/request-encoder.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,8 +23,7 @@
 
 #include "detail/ca-request-state.hpp"
 
-namespace ndncert {
-namespace requesttlv {
+namespace ndncert::requesttlv {
 
 Block
 encodeApplicationParameters(RequestType requestType, const std::vector<uint8_t>& ecdhPub,
@@ -42,7 +41,6 @@
 decodeDataContent(const Block& content, std::vector<uint8_t>& ecdhKey,
                   std::array<uint8_t, 32>& salt, RequestId& requestId);
 
-} // namespace requesttlv
-} // namespace ndncert
+} // namespace ndncert::requesttlv
 
 #endif // NDNCERT_DETAIL_REQUEST_ENCODER_HPP
diff --git a/src/name-assignment/assignment-email.cpp b/src/name-assignment/assignment-email.cpp
index 685a820..2cb6184 100644
--- a/src/name-assignment/assignment-email.cpp
+++ b/src/name-assignment/assignment-email.cpp
@@ -22,7 +22,7 @@
 
 namespace ndncert {
 
-NDNCERT_REGISTER_FUNCFACTORY(AssignmentEmail, "email");
+NDNCERT_REGISTER_NAME_ASSIGNMENT_FUNC(AssignmentEmail, "email");
 
 AssignmentEmail::AssignmentEmail(const std::string& format)
   : NameAssignmentFunc(format)
diff --git a/src/name-assignment/assignment-email.hpp b/src/name-assignment/assignment-email.hpp
index c0bb330..f4eb5c5 100644
--- a/src/name-assignment/assignment-email.hpp
+++ b/src/name-assignment/assignment-email.hpp
@@ -26,12 +26,13 @@
 namespace ndncert {
 
 /**
- * assign names base on client probe parameter
+ * @brief Assign names based on requester's email address
  */
 class AssignmentEmail : public NameAssignmentFunc
 {
 public:
-  explicit AssignmentEmail(const std::string& format = "");
+  explicit
+  AssignmentEmail(const std::string& format = "");
 
   std::vector<ndn::PartialName>
   assignName(const std::multimap<std::string, std::string>& params) override;
diff --git a/src/name-assignment/assignment-func.cpp b/src/name-assignment/assignment-func.cpp
index f4728d3..1e33986 100644
--- a/src/name-assignment/assignment-func.cpp
+++ b/src/name-assignment/assignment-func.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -40,15 +40,15 @@
 std::unique_ptr<NameAssignmentFunc>
 NameAssignmentFunc::createNameAssignmentFunc(const std::string& challengeType, const std::string& format)
 {
-  CurriedFuncFactory& factory = getFactory();
+  auto& factory = getFactory();
   auto i = factory.find(challengeType);
   return i == factory.end() ? nullptr : i->second(format);
 }
 
-NameAssignmentFunc::CurriedFuncFactory&
+NameAssignmentFunc::FuncFactory&
 NameAssignmentFunc::getFactory()
 {
-  static NameAssignmentFunc::CurriedFuncFactory factory;
+  static FuncFactory factory;
   return factory;
 }
 
diff --git a/src/name-assignment/assignment-func.hpp b/src/name-assignment/assignment-func.hpp
index 7f35fcf..f08e50c 100644
--- a/src/name-assignment/assignment-func.hpp
+++ b/src/name-assignment/assignment-func.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -30,10 +30,12 @@
 class NameAssignmentFunc : boost::noncopyable
 {
 protected:
-  explicit NameAssignmentFunc(const std::string& format = "");
+  explicit
+  NameAssignmentFunc(const std::string& format = "");
 
 public:
-  virtual ~NameAssignmentFunc() = default;
+  virtual
+  ~NameAssignmentFunc() = default;
 
   /**
    * @brief The name assignment function provided by the CA operator to generate available
@@ -48,40 +50,40 @@
   virtual std::vector<ndn::PartialName>
   assignName(const std::multimap<std::string, std::string>& params) = 0;
 
-public:
-  template <class AssignmentType>
+public: // factory
+  template<class AssignmentType>
   static void
-  registerNameAssignmentFunc(const std::string& typeName)
+  registerNameAssignmentFunc(const std::string& type)
   {
-    CurriedFuncFactory& factory = getFactory();
-    BOOST_ASSERT(factory.count(typeName) == 0);
-    factory[typeName] = [](const std::string& format) { return std::make_unique<AssignmentType>(format); };
+    auto& factory = getFactory();
+    BOOST_ASSERT(factory.count(type) == 0);
+    factory[type] = [] (const std::string& format) { return std::make_unique<AssignmentType>(format); };
   }
 
   static std::unique_ptr<NameAssignmentFunc>
-  createNameAssignmentFunc(const std::string& challengeType, const std::string& format = "");
+  createNameAssignmentFunc(const std::string& type, const std::string& format = "");
 
 NDNCERT_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
   std::vector<std::string> m_nameFormat;
 
 private:
-  typedef std::function<std::unique_ptr<NameAssignmentFunc>(const std::string&)> FactoryCreateFunc;
-  typedef std::map<std::string, FactoryCreateFunc> CurriedFuncFactory;
+  using CreateFunc = std::function<std::unique_ptr<NameAssignmentFunc>(const std::string &)>;
+  using FuncFactory = std::map<std::string, CreateFunc>;
 
-  static CurriedFuncFactory&
+  static FuncFactory&
   getFactory();
 };
 
-#define NDNCERT_REGISTER_FUNCFACTORY(C, T)                                        \
-  static class NdnCert##C##FuncFactoryRegistrationClass                           \
-  {                                                                               \
-  public:                                                                         \
-    NdnCert##C##FuncFactoryRegistrationClass()                                    \
-    {                                                                             \
-      ::ndncert::NameAssignmentFunc::registerNameAssignmentFunc<C>(T);            \
-    }                                                                             \
-  } g_NdnCert##C##ChallengeRegistrationVariable
-
 } // namespace ndncert
 
+#define NDNCERT_REGISTER_NAME_ASSIGNMENT_FUNC(C, T)                           \
+static class NdnCert##C##NameAssignmentRegistrationClass                      \
+{                                                                             \
+public:                                                                       \
+  NdnCert##C##NameAssignmentRegistrationClass()                               \
+  {                                                                           \
+    ::ndncert::NameAssignmentFunc::registerNameAssignmentFunc<C>(T);          \
+  }                                                                           \
+} g_NdnCert##C##NameAssignmentRegistrationVariable
+
 #endif // NDNCERT_ASSIGNMENT_FUNC_HPP
diff --git a/src/name-assignment/assignment-hash.cpp b/src/name-assignment/assignment-hash.cpp
index f406619..c8c56e5 100644
--- a/src/name-assignment/assignment-hash.cpp
+++ b/src/name-assignment/assignment-hash.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -24,7 +24,7 @@
 
 namespace ndncert {
 
-NDNCERT_REGISTER_FUNCFACTORY(AssignmentHash, "hash");
+NDNCERT_REGISTER_NAME_ASSIGNMENT_FUNC(AssignmentHash, "hash");
 
 AssignmentHash::AssignmentHash(const std::string& format)
   : NameAssignmentFunc(format)
diff --git a/src/name-assignment/assignment-param.cpp b/src/name-assignment/assignment-param.cpp
index e1961e7..6995557 100644
--- a/src/name-assignment/assignment-param.cpp
+++ b/src/name-assignment/assignment-param.cpp
@@ -22,7 +22,7 @@
 
 namespace ndncert {
 
-NDNCERT_REGISTER_FUNCFACTORY(AssignmentParam, "param");
+NDNCERT_REGISTER_NAME_ASSIGNMENT_FUNC(AssignmentParam, "param");
 
 AssignmentParam::AssignmentParam(const std::string& format)
   : NameAssignmentFunc(format)
diff --git a/src/name-assignment/assignment-random.cpp b/src/name-assignment/assignment-random.cpp
index 3305cae..efbe07a 100644
--- a/src/name-assignment/assignment-random.cpp
+++ b/src/name-assignment/assignment-random.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -24,7 +24,7 @@
 
 namespace ndncert {
 
-NDNCERT_REGISTER_FUNCFACTORY(AssignmentRandom, "random");
+NDNCERT_REGISTER_NAME_ASSIGNMENT_FUNC(AssignmentRandom, "random");
 
 AssignmentRandom::AssignmentRandom(const std::string& format)
   : NameAssignmentFunc(format)
@@ -34,7 +34,7 @@
 std::vector<ndn::PartialName>
 AssignmentRandom::assignName(const std::multimap<std::string, std::string>&)
 {
-  return {ndn::PartialName(ndn::to_string(ndn::random::generateSecureWord64()))};
+  return {ndn::PartialName(std::to_string(ndn::random::generateSecureWord64()))};
 }
 
 } // namespace ndncert
diff --git a/src/redirection/redirection-email.cpp b/src/redirection/redirection-email.cpp
index 863eb91..c13ad91 100644
--- a/src/redirection/redirection-email.cpp
+++ b/src/redirection/redirection-email.cpp
@@ -19,16 +19,16 @@
  */
 
 #include "redirection-email.hpp"
+
 #include <boost/algorithm/string.hpp>
 
 namespace ndncert {
 
-NDNCERT_REGISTER_POLICY_FACTORY(RedirectionEmail, "email");
+NDNCERT_REGISTER_REDIRECTION_POLICY(RedirectionEmail, "email");
 
 RedirectionEmail::RedirectionEmail(const std::string& format)
-  : RedirectionPolicy(format)
+  : m_domain(format)
 {
-  m_domain = format;
 }
 
 bool
diff --git a/src/redirection/redirection-email.hpp b/src/redirection/redirection-email.hpp
index 9a338ef..9a8c403 100644
--- a/src/redirection/redirection-email.hpp
+++ b/src/redirection/redirection-email.hpp
@@ -25,13 +25,11 @@
 
 namespace ndncert {
 
-/**
- * assign names base on client probe parameter
- */
 class RedirectionEmail : public RedirectionPolicy
 {
 public:
-  explicit RedirectionEmail(const std::string& format = "");
+  explicit
+  RedirectionEmail(const std::string& format = "");
 
   bool
   isRedirecting(const std::multimap<std::string, std::string>& params) override;
diff --git a/src/redirection/redirection-param.cpp b/src/redirection/redirection-param.cpp
index d74d105..1ce9517 100644
--- a/src/redirection/redirection-param.cpp
+++ b/src/redirection/redirection-param.cpp
@@ -19,18 +19,19 @@
  */
 
 #include "redirection-param.hpp"
+
 #include <boost/algorithm/string.hpp>
 
 namespace ndncert {
 
-NDNCERT_REGISTER_POLICY_FACTORY(RedirectionParam, "param");
+NDNCERT_REGISTER_REDIRECTION_POLICY(RedirectionParam, "param");
 
 RedirectionParam::RedirectionParam(const std::string& format)
-  : RedirectionPolicy(format)
 {
   if (format.empty()) {
     return;
   }
+
   std::vector<std::string> strs;
   boost::split(strs,format,boost::is_any_of("&"));
   for (const auto& s : strs) {
@@ -47,7 +48,7 @@
 {
   for (const auto& p : m_format) {
     bool found = false;
-    for (auto it = params.find(p.first); it != params.end() && it->first == p.first; it ++) {
+    for (auto it = params.find(p.first); it != params.end() && it->first == p.first; ++it) {
       if (it->second == p.second) {
         found = true;
         break;
diff --git a/src/redirection/redirection-param.hpp b/src/redirection/redirection-param.hpp
index d067a1f..270fb3d 100644
--- a/src/redirection/redirection-param.hpp
+++ b/src/redirection/redirection-param.hpp
@@ -25,13 +25,11 @@
 
 namespace ndncert {
 
-/**
- * assign names base on client probe parameter
- */
 class RedirectionParam : public RedirectionPolicy
 {
 public:
-  explicit RedirectionParam(const std::string& format = "");
+  explicit
+  RedirectionParam(const std::string& format = "");
 
   bool
   isRedirecting(const std::multimap<std::string, std::string>& params) override;
diff --git a/src/redirection/redirection-policy.cpp b/src/redirection/redirection-policy.cpp
index 88aef3e..9ecda9a 100644
--- a/src/redirection/redirection-policy.cpp
+++ b/src/redirection/redirection-policy.cpp
@@ -25,7 +25,7 @@
 std::unique_ptr<RedirectionPolicy>
 RedirectionPolicy::createPolicyFunc(const std::string& policyType, const std::string& format)
 {
-  PolicyFactory& factory = getFactory();
+  auto& factory = getFactory();
   auto i = factory.find(policyType);
   return i == factory.end() ? nullptr : i->second(format);
 }
diff --git a/src/redirection/redirection-policy.hpp b/src/redirection/redirection-policy.hpp
index cba0cd8..9e55467 100644
--- a/src/redirection/redirection-policy.hpp
+++ b/src/redirection/redirection-policy.hpp
@@ -29,11 +29,9 @@
 
 class RedirectionPolicy : boost::noncopyable
 {
-protected:
-  explicit RedirectionPolicy(const std::string& format = "") {}
-
 public:
-  virtual ~RedirectionPolicy() = default;
+  virtual
+  ~RedirectionPolicy() = default;
 
   /**
    * @brief The Redirection Policy provided by the CA operator to decide if redirection is suitable.
@@ -45,37 +43,37 @@
   virtual bool
   isRedirecting(const std::multimap<std::string, std::string>& params) = 0;
 
-public:
-  template <class PolicyType>
+public: // factory
+  template<class PolicyType>
   static void
-  registerRedirectionPolicy(const std::string& typeName)
+  registerRedirectionPolicy(const std::string& type)
   {
     PolicyFactory& factory = getFactory();
-    BOOST_ASSERT(factory.count(typeName) == 0);
-    factory[typeName] = [](const std::string& format) { return std::make_unique<PolicyType>(format); };
+    BOOST_ASSERT(factory.count(type) == 0);
+    factory[type] = [] (const std::string& format) { return std::make_unique<PolicyType>(format); };
   }
 
   static std::unique_ptr<RedirectionPolicy>
   createPolicyFunc(const std::string& policyType, const std::string& format = "");
 
 private:
-  typedef std::function<std::unique_ptr<RedirectionPolicy>(const std::string&)> FactoryCreateFunc;
-  typedef std::map<std::string, FactoryCreateFunc> PolicyFactory;
+  using CreateFunc = std::function<std::unique_ptr<RedirectionPolicy>(const std::string &)>;
+  using PolicyFactory = std::map<std::string, CreateFunc>;
 
   static PolicyFactory&
   getFactory();
 };
 
-#define NDNCERT_REGISTER_POLICY_FACTORY(C, T)                                  \
-  static class NdnCert##C##PolicyFactoryRegistrationClass                      \
-  {                                                                               \
-  public:                                                                         \
-    NdnCert##C##PolicyFactoryRegistrationClass()                               \
-    {                                                                             \
-      ::ndncert::RedirectionPolicy::registerRedirectionPolicy<C>(T);        \
-    }                                                                             \
-  } g_NdnCert##C##RedirectionPolicyRegistrationVariable
-
 } // namespace ndncert
 
+#define NDNCERT_REGISTER_REDIRECTION_POLICY(C, T)                             \
+static class NdnCert##C##RedirectionPolicyRegistrationClass                   \
+{                                                                             \
+public:                                                                       \
+  NdnCert##C##RedirectionPolicyRegistrationClass()                            \
+  {                                                                           \
+    ::ndncert::RedirectionPolicy::registerRedirectionPolicy<C>(T);            \
+  }                                                                           \
+} g_NdnCert##C##RedirectionPolicyRegistrationVariable
+
 #endif // NDNCERT_REDIRECTION_POLICY_HPP
diff --git a/src/requester-request.cpp b/src/requester-request.cpp
index 4e6d654..32e29df 100644
--- a/src/requester-request.cpp
+++ b/src/requester-request.cpp
@@ -39,8 +39,7 @@
 
 #include <boost/lexical_cast.hpp>
 
-namespace ndncert {
-namespace requester {
+namespace ndncert::requester {
 
 NDN_LOG_INIT(ndncert.client);
 
@@ -63,7 +62,7 @@
   return std::make_shared<Interest>(interestName);
 }
 
-optional<CaProfile>
+std::optional<CaProfile>
 Request::onCaProfileResponse(const Data& reply)
 {
   auto caItem = infotlv::decodeDataContent(reply.getContent());
@@ -74,7 +73,7 @@
   return caItem;
 }
 
-optional<CaProfile>
+std::optional<CaProfile>
 Request::onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName)
 {
   auto caItem = infotlv::decodeDataContent(reply.getContent());
@@ -94,7 +93,7 @@
   interestName.append("CA").append("PROBE");
   auto interest = std::make_shared<Interest>(interestName);
   interest->setMustBeFresh(true);
-  interest->setApplicationParameters(probetlv::encodeApplicationParameters(std::move(probeInfo)));
+  interest->setApplicationParameters(probetlv::encodeApplicationParameters(probeInfo));
   return interest;
 }
 
@@ -220,7 +219,7 @@
   if (challenge == nullptr) {
     NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
   }
-  auto challengeParams = challenge->genChallengeRequestTLV(m_status, m_challengeStatus, std::move(parameters));
+  auto challengeParams = challenge->genChallengeRequestTLV(m_status, m_challengeStatus, parameters);
 
   Name interestName = m_caProfile.caPrefix;
   interestName.append("CA").append("CHALLENGE").append(Name::Component(m_requestId));
@@ -284,5 +283,4 @@
                                " and Error Info: " + std::get<1>(errorInfo)));
 }
 
-} // namespace requester
-} // namespace ndncert
+} // namespace ndncert::requester
diff --git a/src/requester-request.hpp b/src/requester-request.hpp
index 0f8447a..729d106 100644
--- a/src/requester-request.hpp
+++ b/src/requester-request.hpp
@@ -27,8 +27,7 @@
 
 #include <ndn-cxx/security/key-chain.hpp>
 
-namespace ndncert {
-namespace requester {
+namespace ndncert::requester {
 
 class Request : boost::noncopyable
 {
@@ -61,7 +60,7 @@
    * @return the CaProfile if decoding is successful
    * @throw std::runtime_error if the decoding fails or receiving an error packet.
    */
-  static optional<CaProfile>
+  static std::optional<CaProfile>
   onCaProfileResponse(const Data& reply);
 
   /**
@@ -76,7 +75,7 @@
    * @return the CaProfile if decoding is successful
    * @throw std::runtime_error if the decoding fails or receiving an error packet.
    */
-  static optional<CaProfile>
+  static std::optional<CaProfile>
   onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName);
 
   /**
@@ -276,7 +275,6 @@
   ndn::security::Key m_keyPair;
 };
 
-} // namespace requester
-} // namespace ndncert
+} // namespace ndncert::requester
 
 #endif // NDNCERT_REQUESTER_REQUEST_HPP