lundi 4 mai 2020

how could you improve some of this c++ code?

I am wondering how you might improve some code, we are trying to improve some of our code like a peer review, and I have never done anything like this and to me the code looks pretty good. Our teacher didn't even really give us any guidelines, just, don't say its good or bad. Just some possible suggestions. How would that go review some of this code?

void Date::convert(string & data, string & format) 
{ 
   std::locale loc; 
   string lc; 
   for(size_t i = 0; i < format.length(); i++) 
          lc += std::tolower(format[i], loc); 
   isValid = false; 

     if(lc == "mm-dd-yyyy" && lc.length() == data.length()) 
{ 
     setMonth(atoi(data.substr(0,2).c_str())); 
     if(!isValid) 
           return; 
     setDay(atoi(data.substr(3, 2).c_str())); 

     if(!isValid) 
            return; 
     setYear(atoi(data.substr(6).c_str())); 
}


or 

void Date::setDay(const int d) 
{ 
         isValid = false; 
         if(d <= 0 || d > 31) 
                 return; 
         day = d; 
         isValid = true;

void Date::setMonth(const int m) 
{ 
             isValid = false; 
             if(m <= 0 || m > 12) 
                   return; 
             month = m;
             isValid = true; }

void Date::setYear(const int y) 
{ 
          isValid = false; 
          if(y <= 0 || y > 3000) 
                 return; 
          year = y;
          isValid = true;

}

or

void College::addClass(string & clName, string & clId) 
{ 
     ID id(clName, clId); 
     ColClass cl(clName, clId ); 
     colClasses.insert(make_pair(clId, cl)); 
}

void College::addStudent(string & stName, string & stId) 
{ 
//always check 
      ID id(stName, stId); 
      if(students.find(id) != students.end()) 
      { 
            cout<<"\nA student exists"; 
            return; 
      } 
      Student st(stName.c_str(), stId.c_str(), "11-01-2019"); //by that time of 
                                                                           day 
            //I already forgot what was that only //date format that I support... 
            //Oh,well... it's getting late... 
      students.insert(make_pair(id, st)); 
}

Registrar & College::getRegistrar() 
{ 
      return reg; 
}

Aucun commentaire:

Enregistrer un commentaire