Thread: Need to fix my validation logic.

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    12

    Need to fix my validation logic.

    I need help correcting my logic from another post. Trying to connect together a header and two cpp files. Any help would be appreciated

    Apparently my validation logic is backwards. How can I fix this?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't know if I would say that it is backwards, but it is odd that the get functions make sure that they return a value that makes sense, instead of the set functions making sure that values that you assign make sense.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    well my teacher said exactly "The validation logic is backwards. Per the assignment, the ctor (or set methods) need to validate the parameters and leave the object in a valid state. If the set methods have the validation logic, the ctor can call the set methods. However, once the ctor is done, the object's data members must be valid so that all other member functions can assume a valid state." I'm not sure if I follow what he is saying.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at this snippet:
    Code:
      
     void setMonth(int mm)
       {
               month = (mm);
       }
        
    ...
        int getMonth()
       {
             // If month is under 1 and over 12, then month = 1
          if (month < 1 || month > 12) 
          {
          month = 1;
          }
          return month; 
       }

    What your teacher is telling you is that your setter should be validating the input. In other words you should never store an invalid input into your class. You have the validation being done after you have set the value so the class can be holding invalid data.

    Your teacher is also telling that you should be validating the data provided in the constructor by calling your setter functions.

    Jim

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Right, then you need to move the logic to the set functions. The get functions can be simple return statements.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    Ahhhhh I see that makes alot of sense. How would you do that with the string function?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The string function? Maybe you mean getMonthName() -- that is about as simple as it will get.

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    Thank you so much for you help. So really, you need to have the functions in the set functions to validate the constructor and set functions, otherwise the set functions does not serve its purpose?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Pretty much.

    This is a bit of object oriented philosophy, but maybe coming out and saying it will help the comments on your work make sense. The only way objects ever really do anything is with methods. A method is jargon for function. But anyway, there are kinds of things that lots of objects do so there are kinds of methods that you can learn about. Some of these are concrete methods, like the constructor, destructor, and copy constructor (and others). There are more that are abstract in nature like getters and setters, or accessors and modifier methods.

    An accessor method is supposed to tell the rest of the program about the object. In so doing, it is often expected that the object doesn't change because of the method. In the Date class, examples of this would include methods like getMonth(), getMonthName(), getDay() and getYear(). They tell you about the Date object and nothing more. No changes occurred just because you asked a date object about itself.

    The other side of the coin is modifier methods, where you want to make a change to the date. Pretty much all bets are off and you have to consider the arguments to the function. Usually, this means validation because you want to transition from one valid state (an older day) to a different equally valid state (a newer day).

    To get a basic understanding of most classes, and their designs, separate them into functions that make changes to class variables and those that don't. Ones that change class variables are modifiers. Accessors provide access to the class's information. In the beginning, this means returning the variables. But in more complicated classes, it might mean something else.

  10. #10
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    I understand that and I am sorry for asking so many questions. I just want to understand what I did wrong fully before I move on.
    Is this all I needed to do to fix that? Then make the get functions that return the month, day, year?


    Code:
        //These functions sets the date by equaling the month, day and year to the private month, day, and year.
        void Date::setMonth(int mm) 
            {month = (mm);
          // If month is under 1 and over 12, then month = 1
      if (month < 1 || month > 12) 
          {
            month = 1;
         }
    }
     
    void Date::setDay(int dd) 
        {day = (dd);
          // If month equals 2 and day is greater than 28, then day = 1.
      if (month == 2 && day > 28) 
          {
            day = 1;
          }
     
      // If the day is less than 1 and greater than 31 and is one of the listed
      // months, then day = 1.
      if ((day < 1 || day > 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||month == 10 || month == 12)) 
          {
            day = 1;
          }
      // If the day is less than 1 and greater than 30 and is one of the listed
      // months, then day = 1.
      if ((day < 1 || day > 30) && (month == 4 || month == 6 || month == 9 || month == 11)) 
          {
            day = 1;
          }
    }
     
    void Date::setYear(int yy) 
        {year = (yy);
            //If year is under 1900, then year is 1900.
      if (year < 1900)
        year = 1900;
        }

  11. #11
    Guest
    Guest
    As a matter of form, I would evaluate the inputs (mm, dd), and do the assignments to the class members afterwards. Note that in setDay, you best arrange your logic so that day doesn't get modified while you're still doing checks on it. Check dd.

    Edit: Consider else if and the || operator. If you exclude (dd < 1 && dd > 31) first, you can save yourself quite a few checks I think!

    p.s. I find you indentation style hard to read, consider adopting one of the more popular ones, like:
    Code:
    void Date::setMonth(int mm) 
    {
        // If month is under 1 and over 12, then month = 1
        if (mm < 1 || mm > 12) {
            month = 1;
        }
    }
    Last edited by Guest; 09-26-2017 at 06:49 PM.

  12. #12
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    So are you saying that instead of "month = mm" it should be "mm = month"?

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No I think he just means using mm instead of month, dd instead of day, and yy instead of year up until you are sure that an assign is correct. In a lot of your functions, you simply assign first and then make it work, which is technically fine. It can be a bad habit though.

  14. #14
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    So I see he took out the "{month = (mm);" Was there any reason for that?

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, when he did that it made it not work. You would still have to assign mm to month somewhere, preferably at the end if you got the advice so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validation
    By ulti-killer in forum C Programming
    Replies: 5
    Last Post: 11-15-2012, 01:16 PM
  2. Validation
    By ulti-killer in forum C Programming
    Replies: 8
    Last Post: 11-15-2012, 12:06 AM
  3. validation in c
    By thangarasu1988 in forum C Programming
    Replies: 4
    Last Post: 01-05-2012, 02:42 PM
  4. c++ validation
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:53 PM
  5. Help with validation
    By boontune in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2003, 09:44 AM

Tags for this Thread