Return value or Exception or something else ?
-
Joergen Sigvardsson wrote:
Uhm.. that is a rather dull guideline. Just because some academic has decided that references are better than pointers (which the majority of the C++ community uses), it's counter productive NOT to use them in cases like this, as it would cut your development by at least an hour (seeing that you posted this approximately an hour ago).
Knowing how to use references increases code readability, maintainability, and decreases the liklihood of bugs being introduced via improper usage of pointers. In general, if you can get away with not using pointers, you should do so.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Zac Howland wrote:
Knowing how to use references increases code readability, maintainability
That I don't buy. The only thing that differs between references and pointers in terms of readability are "->" vs "." and "*" vs "&". If anything, references goes against readability, because it's not obvious by seeing the usage of a variable whether it's by value or by reference. With a pointer, it becomes quite obvious. References also have a cumbersome feature which is not always possible to overcome. You can't assign a reference later on - it must be initialized. That is not always possible. Also, you do not have the option to represent "no object" with references (unless you use unsafe methods which aren't portable).
Zac Howland wrote:
In general, if you can get away with not using pointers, you should do so.
The only time I use references are when "no object" isn't an option. In the case of the OP, a reference isn't worthwhile as it doesn't cope well with the NULL case - an expected case it turned out later in a clarification by the OP's author. Returning a status value indicating success is not a desired either as it requires an extra copy which may not be doable if the state of the job object is to be visible to all.
Zac Howland wrote:
and decreases the liklihood of bugs being introduced via improper usage of pointers.
I can buy that.
-- Broadcast simultaneously one year in the future
-
This is my function :
CThreadPool::t_Job& CThreadPool::CJobQueue::getJob()
{
WaitForSingleObject(m_JobListMutex, INFINITE);for ( std::deque::iterator it = m\_JobList.begin(); it != m\_JobList.end(); it++ ) if ( it->m\_JobState != JOB\_WORKING) { it->m\_JobState = JOB\_WORKING; ReleaseMutex(m\_JobListMutex); return (\*it); } ReleaseMutex(m\_JobListMutex); // Need to do something here to indicate no item has been found
}
I don't want to use a pointer for return. And i don't think it's apropriate to throw an exception, since that scenario is unavoidable, in fact quite regular. My only Idea left would be to return some special reference to a static element that functions as sort of a NULL. Any other ideas/comments ?
I use boost::optional library for similar problems. But you return a reference and it introduces some limitations to what boost::optional library could do. Good luck..
Orhun Birsoy
-
Zac Howland wrote:
Knowing how to use references increases code readability, maintainability
That I don't buy. The only thing that differs between references and pointers in terms of readability are "->" vs "." and "*" vs "&". If anything, references goes against readability, because it's not obvious by seeing the usage of a variable whether it's by value or by reference. With a pointer, it becomes quite obvious. References also have a cumbersome feature which is not always possible to overcome. You can't assign a reference later on - it must be initialized. That is not always possible. Also, you do not have the option to represent "no object" with references (unless you use unsafe methods which aren't portable).
Zac Howland wrote:
In general, if you can get away with not using pointers, you should do so.
The only time I use references are when "no object" isn't an option. In the case of the OP, a reference isn't worthwhile as it doesn't cope well with the NULL case - an expected case it turned out later in a clarification by the OP's author. Returning a status value indicating success is not a desired either as it requires an extra copy which may not be doable if the state of the job object is to be visible to all.
Zac Howland wrote:
and decreases the liklihood of bugs being introduced via improper usage of pointers.
I can buy that.
-- Broadcast simultaneously one year in the future
Joergen Sigvardsson wrote:
That I don't buy. The only thing that differs between references and pointers in terms of readability are "->" vs "." and "*" vs "&". If anything, references goes against readability, because it's not obvious by seeing the usage of a variable whether it's by value or by reference. With a pointer, it becomes quite obvious.
Most software engineers I know disagree with you on that, but as this area can quickly become a religious topic, I won't go into the discussion.
Joergen Sigvardsson wrote:
References also have a cumbersome feature which is not always possible to overcome. You can't assign a reference later on - it must be initialized. That is not always possible
It is not always cumbersome, but rather forces a limitation on the code that is likely desirable. For example, calling delete on a pointer you don't "own" will delete the object, but cause problems at runtime (and may be difficult to debug). However, calling delete on a reference will give you a compile time error with line number pointing to your problem (no pun intended).
Joergen Sigvardsson wrote:
Also, you do not have the option to represent "no object" with references (unless you use unsafe methods which aren't portable).
That is when exceptions and status values are more valuable. Returning NULL requires that the client code must check to make sure they received a valid pointer (or suffer a bad runtime bug). Throwing an exception will break the execution of the current method so that no harm can be done to memory. Return a status code and passing in a reference to copy data to ensures that (even if the client fails to check the status), the object will be valid (i.e. no memory corruption).
Joergen Sigvardsson wrote:
The only time I use references are when "no object" isn't an option. In the case of the OP, a reference isn't worthwhile as it doesn't cope well with the NULL case - an expected case it turned out later in a clarification by the OP's author. Returning a status value indicating success is not a desired either as it requires an extra copy which may not be doable if the state of the job object is to be visible to all.
There are ways to handle the NULL case. The question isn't whether it does, but which method works better for a given situation. Pointers will w
-
This is my function :
CThreadPool::t_Job& CThreadPool::CJobQueue::getJob()
{
WaitForSingleObject(m_JobListMutex, INFINITE);for ( std::deque::iterator it = m\_JobList.begin(); it != m\_JobList.end(); it++ ) if ( it->m\_JobState != JOB\_WORKING) { it->m\_JobState = JOB\_WORKING; ReleaseMutex(m\_JobListMutex); return (\*it); } ReleaseMutex(m\_JobListMutex); // Need to do something here to indicate no item has been found
}
I don't want to use a pointer for return. And i don't think it's apropriate to throw an exception, since that scenario is unavoidable, in fact quite regular. My only Idea left would be to return some special reference to a static element that functions as sort of a NULL. Any other ideas/comments ?
A bit off topic but you may want to consider using Window's built-in thread pool. Look up the
QueueUserWorkItem
API.Steve