Thread: Assert: When to and when not to.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    I would go with something like
    Code:
    if ( _employee != NULL ) {
        // do stuff
    } else {
        assert(_employee != NULL);
    }
    This way, the code won't suddenly blow up in a release build should a NULL pointer unexpectedly arrive.

    I could carp about symbols beginning with underscore as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Quote Originally Posted by Salem View Post
    I would go with something like
    Code:
    if ( _employee != NULL ) {
        // do stuff
    } else {
        assert(_employee != NULL);
    }
    This way, the code won't suddenly blow up in a release build should a NULL pointer unexpectedly arrive.

    I could carp about symbols beginning with underscore as well.
    That's just my way to differentiate between local variables and passed variables, any other suggestions for that?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Shamino View Post
    That's just my way to differentiate between local variables and passed variables, any other suggestions for that?
    Add the "_" at the end of the variable name instead.
    Leading underscores on variable names tend to be reserved for compiler and library users.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I've made some adjustments according to the responses to the thread, thanks for your input everyone!

    Employee.h:
    Code:
    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H
    #include <string>
    using namespace std;
    
    class employee
    {
    public:
    	employee(string _name, string _payrate)
    	{
    		name = _name;
    		payrate = _payrate;
    	}
    
    	string& get_name()
    	{
    		return name;
    	}
    
    	string& get_payrate()
    	{
    		return payrate;
    	}
    	void edit()
    	{
    		string input;
    		int selection;
    		bool goodinput = false;		//loop conditional, input must match conditions, else continue
    		while (goodinput == false)
    		{
    			cout << "What would you like to change?" << endl		// prompt
    				 << "1. Name" << endl << "2. Payrate" << endl;
    
    			getline(cin, input);	//get input
    			stringstream(input) >> selection;	//extract selection integer
    
    			if (selection == 1)
    			{
    				cout << "Please enter a name for this employee: ";
    				getline(cin, name);
    				goodinput = true;
    			}
    			else if (selection == 2)
    			{
    				goodinput = true;
    				cout << "Please enter a pay rate for this employee (hourly): ";
    				getline(cin, payrate);	
    			}
    			else
    			{
    				goodinput = false;	// invalid selection - continue loop
    				cout << "Please enter a valid selection, 1 or 2." << endl;
    			}
    		}
    	}
    
    private:
    	string name;
    	string payrate;
    };
    
    #endif
    Experimentations.cpp (main)
    Code:
    // Experimentations.cpp : main project file.
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <assert.h>
    #include "Employee.h"
    using namespace std;
    
    vector<employee> employees;
    
    employee create()
    {
        cout << "Please enter the new employee's name: ";
        string name;
        getline(cin, name);
        cout << "Please enter the new employee's payrate: ";
        string payrate;
        getline(cin, payrate);
        employee new_employee(name, payrate);
    	return new_employee;
    }
    
    employee & get_employee(string name)
    {
    	for (unsigned int i = 0; i < employees.size();)
    	{
    		if (name.compare(employees[i].get_name()) == 1)
    		{
    			return employees[i];
    		}
    		else
    		{
    			i++;
    		}
    	}
    	throw;
    }
    
    void list_employees(vector<employee> & employees)
    {
    	for (unsigned int i = 0; i < employees.size(); i++)
    	{
    		cout << employees[i].get_name() << endl;
    	}
    }
    
    int main()
    {
    	int selection;
    	string input;
    	bool goodinput = false;
        while (goodinput == false)
    	{
    		cout << "Welcome to the employee database, what would you like to do?" << endl		// prompt
    			 << "1. Add an employee" << endl << "2. Modify an employee" << endl;
    
    		getline(cin, input);	//get input
    		stringstream(input) >> selection;	//extract selection integer
    
    		if (selection == 1)
    		{
    			goodinput = true;
    			employees.push_back(create());
    		}
    		else if (selection == 2)
    		{
    			cout << "Enter the name of the employee you'd like to edit: ";
    			getline(cin, input);
    			get_employee(input).edit();
    			goodinput = true;
    		}
    		else
    		{
    			goodinput = false;	// invalid selection - continue loop
    			cout << "Please enter a valid selection, 1 or 2." << endl;
    		}
    	}
        return 0;
    }
    I'm not quite sure how to logically arrange the functions that actually handle the employees vector. I'm thinking about making menu objects to simplify that aspect of programming this thing, any suggestions?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Salem View Post
    I would go with something like
    Code:
    if ( _employee != NULL ) {
        // do stuff
    } else {
        assert(_employee != NULL);
    }
    This way, the code won't suddenly blow up in a release build should a NULL pointer unexpectedly arrive.
    That's one of those "This should never happen, but if it does" kind of things. Those bug me because it really shows that the programmer is unsure if putting the assert there is the right thing to do, i.e. they aren't sure if it will never happen. If you're sure enough that it can't happen to go and put the assert there, then why on earth would you code for the possibility that you were wrong? It just means you were never sure enough to put the assert there to begin with.

    Precondition testing is basically one of the reasons that you can never be sure that an assert is appropriate, and in that case it is fair enough.
    One option is throwing on error:
    Code:
    if (employee == NULL) throw something;
    However to avoid such an exception from being inadvertantly caught and ignored, you unfortunately do need to assert also. At work we use an ASSERT_THEN_THROW_IF_FALSE macro. Yes its long and wordy, but I don't have to explain what it does now do I

    Something allong the lines of a CHECK_PRECONDITION macro might be even better.

    Then you can save the assert for things which you know for sure are not going to be false.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assert/Macro
    By tempster09 in forum C Programming
    Replies: 15
    Last Post: 12-17-2009, 10:13 PM
  2. assert
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-22-2007, 03:17 AM
  3. Assert
    By Shamino in forum C++ Programming
    Replies: 8
    Last Post: 01-24-2006, 11:02 AM
  4. How to use assert?
    By jjbuchan in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 01:40 PM
  5. assert
    By ammar in forum C++ Programming
    Replies: 1
    Last Post: 10-19-2002, 08:17 AM