C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 31st, 2004, 12:52 PM
Jeff Mott's Avatar
Jeff Mott Jeff Mott is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 95 Jeff Mott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 19 sec
Reputation Power: 9
Assert Parameters

Is it a good programming practice to list assertions for all the parameters? Or bad practice to not do so?

For example,
Code:
bool
LinkedList_IsEmpty(LinkedList *list) {
	assert(list != NULL);
	return list->length == 0;
}
It seems like it would help with debugging, but at the same time it seems to bloat the code, especially for functions that would otherwise be just a single line. Should this responsibility be left to the user? Where the documentation might read, for example, If list does not point to a valid LinkedList structure the behaviour is undefined.

Reply With Quote
  #2  
Old December 31st, 2004, 01:46 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,473 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 6 h 45 m 54 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3242
Assertions ARE debugging features. When you generate release code they will go away.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

Reply With Quote
  #3  
Old January 4th, 2005, 02:25 AM
GnaM GnaM is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 29 GnaM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking Obsessed with ASSERTS

Hi,

Speaking as a newbie,

My opinion,
ASSERTS are eXtremely important when programming.
Every function has it's set of preconditions and postconditions
which you should state for yourself.
The good thing about this practice is that it changes your way of
thinking when programming the code. Because you have to rigorously
state what can be accepted and what not you are more focused as to
what can go wrong than what can go right about your code.
Just imagine yourself being a teacher who has to correct the
examinations of a 1000 students. Because of the amount of work to be
done, the most likely approach that you will do in correcting these
examinations is to look directly to the solution, skipping the student's
reasoning. Of course, you are not assured to be correcting 100%
correctly but it gets the job done within reasonable parameters.
So what the teacher actually did was just to check the postcondition
of every answer, with the mentality that when then answer is correct,
the reasoning must also be correct. (Again not a 100% correct
assumption)

An example, from my practical experience :
We had to implement a priorityqueue based on a heap.
We were allowed to use a STL container(like vector) to implement it.
So I have a

Code:
template<T>
class CHEAP {
public :.....
           typedef vector<T> ContainerType;     
private : ContainerType container;
};


I am almost obsessed with setting pre and postconditions, upto the
point I do like this :
Code:
template<T>
  CHEAP::insert(in_el) {  
1)        ContainerType old(container);
	/** --Contracts- Pre
	 * old == getContainer(), to make postcondition sane
	 */
2)	assert(old == getContainer());

        /** --Contracten-- Post
	 * Assure that in_el is added.
	 * old != getContainer() makes sense thanks to precondition
	 * element_added(...) to assure this
	 */
	assert(old != getContainer());
	assert(element_added(old, container, in_el));
    };

element_added simple checks the contents of the 2 containers
and returns true if all the elements are identical except for in_el.
After I tested the class CHEAP, I decided to make an interface class
CPriorityQueue . Everything was working, but somehow, believe it or
not, but due to some stupid mistake (2) FAILED despite (1) being set.
This was contradictory to all logic, and wouldn't it have been for
that assert, I would had to spend hours in trying to find the relevant
bug.(because the bug is contradictory to logic...)

I realized very quickly that I did not call the methods of CHeap correctly
in CPriorityQueue. The compiler didn't give an error, which I think is
strange due to what I had done...

Whenever I make a program, I first make a design stating all pre and postconditions. Then I only implement the headings of this design.
I use vim to edit, and when I press F4 I get something like this
:MakeComment
I can input a parameter
:MakeComment +Foo(in_bar) : bool out_gnam {query}
When I press enter I get :
Code:
/* <------------------------- Foo(in_bar) : bool out_gnam {query} ------------------------------>
 *
 * PRECONDITIONS  : -
 *                 -
 * POSTCONDITIONS : -
 *                 -
 */
bool
CLASS::Foo(in_bar)
{
/*                  ----PRECONDITIONS-----
 */
   Output("CLASS::Foo(in_bar)" );
/*                  ----POSTCONDITIONS----
 */
   return  out_gnam;
}



If you look on the web, Software Engineering Xtreme ASSERT you will probably find more information regarding this topic.

Regards.

Reply With Quote
  #4  
Old January 4th, 2005, 02:49 AM
GnaM GnaM is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 29 GnaM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Oberon has nice features.

If you know Oberon,
Turbo Pascal --> Modula --> Modula 2 --> Oberon,
By Nicklaus Wirth, you will certainly agree with me that it has
a nice feature regarding setting of pre- and postconditions.
Into your code you put in something like this
assert(condition, number)
with 100-->110 reserved for preconditions, 110-->120 invariants,
120 --> 130 for postconditions.

Now the interesting thing is that when you use the tool Watson
it lets you give an overlook of your implementation headings.
(so the function bodies are all skipped, only the function heading is visible)
When you have specified pre- and/or postconditions they become visible as well, permitting the user to get a quick overvieuw of
what to expect from the code.
--------> like this
Function heading
precondition 101 : ................
precondition 102 : ................

All the others(< 100, 110 < 120 and > 120), do not become visible.

Jeff Mott said something like this,
Quote:
It seems like it would help with debugging, but at the same time it seems to bloat the code, especially for functions that would otherwise be just a single line. Should this responsibility be left to the user? Where the documentation might read, for example, If list does not point to a valid LinkedList structure the behaviour is undefined.


To my newbie knowledge, I would say no, this responsibility should
not be left to the user with a manual like that. Because he/she will most likely abuse it willingly/unwillingly.

Also, as a user I don't trust the word of the implementer regarding
what to expect from the code. Simply because bugs can't be avoided.
But if you put in ASSERTS into your code, and I use a tool like WATSON
then I at least have something to build upon, some axioms regarding the use of the code.

Regarding the bloating of the code, that depends.
You are right that some conditions are not very easy to state,
especially because everything that is being used in those condtions
should be publically visible, but this shouldn't prevent you using them.

As I stated before, I am a newbie, don't know much, so

I hope someone more experienced can explain in more detail and
point out my stupid remarks, so I can improve in this topic.


Regards.

Last edited by GnaM : January 4th, 2005 at 02:53 AM. Reason: none of your bussiness :)

Reply With Quote
  #5  
Old January 4th, 2005, 03:16 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 8,978 Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 39th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 22 h 11 m 29 sec
Reputation Power: 3561
assert() is great for debugging, because it stops the code right where the problem occurs, rather than let the code proceed and fail later on down the line. Sometimes, debugging code is a real pain in the neck, because the cause of the error is far away from where the error occurs (say, the result of using a null pointer causes a function four levels up the call stack to fail occasionally). When you're using assert(), you're putting in some sanity checking to make sure that your data is within an expected range at certain stages in your program.

Quite a few (if not all) C++ compilers will compile out the assert() statements in release mode, so the extra assert() code never makes it out to the final executable, if you compile it with the necessary optimization switches.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Diary of a first time dog owner <-- my cousin's blog

Reply With Quote
  #6  
Old January 4th, 2005, 04:48 AM
lectricpharaoh's Avatar
lectricpharaoh lectricpharaoh is offline
Ultimate Obliterator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wet West Coast of Canada
Posts: 261 lectricpharaoh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 33 m 40 sec
Reputation Power: 8
Quote:
Originally Posted by Scorpions4ever
Quite a few (if not all) C++ compilers will compile out the assert() statements in release mode, so the extra assert() code never makes it out to the final executable, if you compile it with the necessary optimization switches.
You don't need optimizations; just #define NDEBUG, or compile in release mode (if your compiler has such a switch, which generally defines this macro).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Assert Parameters


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap