473,699 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Argument Checking


Does anyone have any coding rules they follow when doing argument checking?

When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw exceptions?

Aug 8 '06 #1
4 2380
Patient Guy wrote:
>
Does anyone have any coding rules they follow when doing argument
checking?

When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw exceptions?
Hi,

I guess everybody have their own coding rules.
I am not aware of some RFC-like proposal on how to handle it, and if it
existed, I wouldn't care. ;-)

Bottomline is: What do YOU (or your boss/custumer) want to check?

This is how I do it most of the time:
1) In the form I make clear to the visitor which elements I want to be
filled in.
2) When submitting, check the values.
If they are ok: just submit
If not, do not submit, and make an alert saying "Please give your first
name" or something like that.

And most important: Do NOT rely on Javascript when accepting formvalues on
the server. Always recheck them at the server.

So yes, this is always double work, but once you've done it a few times,
you'll get very quick and maybe use a few prefabbed functions (on both the
server and in Javascript).

Be wise and first discover how to check values yourself before using some
downloaded script of somebody else. :-)
I have seen a lot of bad checkingalgorit ms on the net.

Regards,
Erwin Moller

Aug 8 '06 #2
Patient Guy wrote:
Does anyone have any coding rules they follow when doing argument
checking?
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw exceptions?
Depends on what you want to do.

IMO, you should usually skip argument checking altogether. The person using
a function should know what arguments to pass. Checking them each time a
function is run only (potentially) slows things down.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 8 '06 #3
"Matt Kruse" <ne********@mat tkruse.comwrote in comp.lang.javas cript:
Patient Guy wrote:
>Does anyone have any coding rules they follow when doing argument
checking?
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw exceptions?

Depends on what you want to do.

IMO, you should usually skip argument checking altogether. The person
using a function should know what arguments to pass. Checking them
each time a function is run only (potentially) slows things down.
If you write code that might become a library distributed to others, they
might very much appreciate argument checking, or else they might be forever
trying to figure out their bug.

I wrote a longer version of this message with an example function that I
wanted comment on, but the length of the message likely scared off those
with even the longest attention spans.

You can find the message here:

<news:Xn******* ***********@207 .115.17.102>

There are probably good reasons to do argument checking anyway even if you
are using the code for your own purposes. Wouldn't it actually help you
debug faster or force you to work a better design in creating functions?
Aug 8 '06 #4

"Patient Guy" <sevisen.adam@g mailDOTHEREcomw rote in message
news:Xn******** **********@207. 115.17.102...
>
Does anyone have any coding rules they follow when doing argument
checking?
I've some general guidelines that I follow.

Firtly I would define "arugment checking" as dedicated code to determine the
existence of an argument and optionally it's type, format or content and
react according to need. The reaction might be to default the value of the
argument, throw an error or just plain ignore it.

Generally I WILL perform argument checking when:

+) The code in question is a "black box" of functionality. Components and
libraries designed to be imported and used by others should, I believe,
assume no special or deep knowledge of the code's inner workings. Argument
existence and type checking should be done and appropriate (and clear) error
messages provided.

In general the more any piece of code (function, method, custom object, etc)
is aimed at re-use the more formal I'll get about argument checking.

+) The code in question accepts end-user-generated ("uncontrolled" ) input.
When the code in question can accept uncontrolled input (as from a form
field perhaps) you should make it a point to check the argument. Almost all
field validation can be seen as a kind of "argument checking" (or maybe more
accurately "parameter checking").

Never trust a user.

+) When data-typing is required. Does your method require a number? An
array? A date? If so it's a good idea to check the input to make sure
you're getting what you know you need.

+) When the arugment is optional (especially when it's optional and can be
set to a default). Arguments are often optional, argument checking will
tell you if these optional arguments have been passed or not so that your
code can react appropriately. In many cases you may want to default the
value of these optional parameters (for example an optional string might
better be set to a blank space rather than left as undefined).

+) Whenever failure to check the argument can cause things to go belly up.
This is pretty simple but why let your code get (possibly) deep into a
(possibly time-consuming) process only to discover that an argument is
wrong/missing/hosed in some way? Better to check up front an react quickly
in my opinion.
I will generally NOT perform argument checking when:

+) The code is "automated" and called only by formalized code. For example
I prefer in some cases to provide generic eventhandlers: onChange and
onFocus and so forth that take a form field as an arugment. Since these are
called only by form field-specific event handlers in the form of
"onChange(this) " there really isn't much of a need to check the argument.

+) When the code is so simple that any error will both be quickly encoutered
and just as illuminating as any custom one provided.
Most of the time however I'll lean towards full, formalized argument
checking.
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw exceptions?
It depends. As a general rule it's a good idea in most cases. For me I
look at the method/function as a series of questions and resulting
statements (flow chart style). This is why it's best to define (write a
specification) for your code beforehand: the answers will be in there.

For example consider a function "parseDate(Inpu t)". The function takes a
value and tries to convert it to a date - if it can, it returns the date, if
not it returns null. Your specification has the answer: anything that can't
be converted to a date results in null.

Considering that I would check the input argument and if it's not a string
return null immediately. You _could_ return an error that says "a string is
required" but that adds unneccessarily to the definition.

In this case the usage is easier as well. You don't have to wrap
parseDate() in try/catch blocks every time you use it.

On the other hand sometimes an error is needed. Consider a function
countVowels(Inp ut) which takes a value and returns the number of vowels in
it.

Here the spec doesn't define an "out" - you have to return a number. Since
asking for the number of vowels in, say, an integer value, is nonsensical
(returning "zero" this case would be misleading, I think) I would throw an
error for all non-string input. You'll either get an error or a number.

All told its best to find a style and stick to it even if it's sometimes
counter intuitive. Consistency helps you more in later maintenance more
than almost anything else.

Jim Davis
Aug 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4667
by: aa | last post by:
I use the following fragment of code to output datf from MySQL: ====================================================== $chan = mysql_connect ($db_host, $username, $password); mysql_select_db ($DB_name, $chan); $resultid = mysql_query ("select name_ru, description_ru, retail, dealer from lasershot WHERE le='1'", $chan); ........ ======================================================
134
7871
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
5
1961
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like the one I am using (NULL in this example) or an argument count parameter (ugh). The following function concatenates a series of C-style strings. I am aware of the fact that not allocating enough memory for `dst` results in UB (gave me a...
8
1877
by: Brendan | last post by:
There must be an easy way to do this: For classes that contain very simple data tables, I like to do something like this: class Things(Object): def __init__(self, x, y, z): #assert that x, y, and z have the same length But I can't figure out a _simple_ way to check the arguments have the
3
4279
by: Tomás | last post by:
Let's say we have a variable length argument list function like so: unsigned GetAverage(unsigned a, unsigned b, ...); I wonder does the compiler go through the code looking for invocations of the function, and from there, treat it sort of like a template function? For instance, if we had a template function like so: template<class T> T GetAverage(T a, T b);
50
3301
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? "None" doesn't seem to work.. Thanks in advance.
20
1952
by: Chris | last post by:
I'm not sure if this has been done before, but I couldn't easily find any prior work on Google, so here I present a simple decorator for documenting and verifying the type of function arguments. Feedback/suggestions/criticism is welcome. ''' 2006.12.21 Created. ''' import unittest
8
2295
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8932
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8892
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7772
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6539
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.