LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


View Poll Results: Return boolean value or error message
boolean value 4 50.00%
error message 4 50.00%
Voters: 8. You may not vote on this poll

Reply
  Search this Thread
Old 12-04-2015, 02:14 PM   #1
portonvictor
LQ Newbie
 
Registered: Jul 2013
Posts: 6

Rep: Reputation: Disabled
Question What (data model) validation function should return?


A question about software design:

When registering a field validation callback for a data model, should it be a pair of a function which returns boolean value and of an error message, or should it be a function which may return the error message (or NULL if no error)?
 
Old 12-04-2015, 02:16 PM   #2
portonvictor
LQ Newbie
 
Registered: Jul 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Returning error message may avoid repeated passing data structures with the same error message more than once.

Returning bool separates logic and usage.

What is better?
 
Old 12-05-2015, 03:24 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I think this is impossible to answer, depends on the actual piece of software (program) that you are writing.

Best regards,
HMW
 
Old 12-05-2015, 07:55 AM   #4
portonvictor
LQ Newbie
 
Registered: Jul 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
I think this is impossible to answer, depends on the actual piece of software (program) that you are writing.
I am writing an object-relational mapping for our big complicated Web site.

The ORM is to be used for such things as validating Web form inputs.

We are now doing site code refactoring, to rewrite the site with an ORM.

Last edited by portonvictor; 12-05-2015 at 08:04 AM.
 
Old 12-05-2015, 08:04 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
The problem with both methods is that you cannot return warnings...

A boolean is enough for simple true/false tests - but things like a password aren't just true/false (think updating a password - you have a true value when all tests pass, you have a false if required tests fail, but you also have a "maybe" if the required tests pass, but quality checks might not).
 
Old 12-05-2015, 08:07 AM   #6
portonvictor
LQ Newbie
 
Registered: Jul 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
The problem with both methods is that you cannot return warnings...

A boolean is enough for simple true/false tests - but things like a password aren't just true/false (think updating a password - you have a true value when all tests pass, you have a false if required tests fail, but you also have a "maybe" if the required tests pass, but quality checks might not).
I think, we should not complicate the code with "warnings". (Or we may add such code at a later stage.)

I need to decide how to process errors: with a function returning error message or with a function returning bool.

Last edited by portonvictor; 12-05-2015 at 08:52 AM.
 
Old 12-05-2015, 10:41 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Return an error code, but try to standardize the values, eg:

Code:
0=ok, valid data
1=empty
>1=other little problem
<0=error
 
Old 12-05-2015, 11:15 AM   #8
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
you throw an exception because that can not be ignored if something goes wrong
or you create an optional type that will be returned and can be queried if valid, if not and you want to access the value, throw.
Now the question is, do you have a programming language that support creating this? or do you stuck with C, than you are limited to return codes
 
Old 12-05-2015, 11:18 AM   #9
portonvictor
LQ Newbie
 
Registered: Jul 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by a4z View Post
you throw an exception because that can not be ignored if something goes wrong
or you create an optional type that will be returned and can be queried if valid, if not and you want to access the value, throw.
Now the question is, do you have a programming language that support creating this? or do you stuck with C, than you are limited to return codes
A colleague asked me not to throw exceptions, but just to return a result indicating an error. I am not sure about his reason, but do as he asks.

We use Perl. Perl supports all this.
 
Old 12-05-2015, 11:20 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Exceptions are also preferable because they can occur "many levels deep" in a nest of subroutines, with "the catcher's mitt" waiting at the outer level. When the exception goes off, it "lands directly in the mitt," and all the intervening nest of calls is forgotten. None of the code has to "test return codes and exit." Basically, "if we return from the validation call," the validation was successful. Likewise, if we ever wind up in the catcher's mitt, we know that an exception has occurred.

If your language also supports "finally" clauses, you can arrange for certain things to happen whether-or-not an exception has just gone off.

Particularly useful is when the exception that gets thrown is, itself, an object within a class, which can contain specific information about the exception and which can be recognized by the class (or superclass) it belongs to, within the "catcher's mitt" code.

Last edited by sundialsvcs; 12-05-2015 at 11:22 AM.
 
Old 12-05-2015, 11:21 AM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by portonvictor View Post
Returning error message may avoid repeated passing data structures with the same error message more than once.

Returning bool separates logic and usage.

What is better?
The second one.
 
Old 12-05-2015, 11:21 AM   #12
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by portonvictor View Post
A colleague asked me not to throw exceptions, but just to return a result indicating an error. I am not sure about his reason, but do as he asks.

We use Perl. Perl supports all this.
than return a optional type that can be queried if valid and throw if you access the value.
and ask your colleague what he prefers, undefined behaviour or secure code with exceptions, I think the answer should be obvious
 
Old 12-05-2015, 11:36 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Exceptions are good, but ONERRORGOTO was even better in BASIC, or EXLST in Assembly.
 
Old 12-05-2015, 05:45 PM   #14
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by a4z View Post
you throw an exception because that can not be ignored if something goes wrong
or you create an optional type that will be returned and can be queried if valid, if not and you want to access the value, throw.
Now the question is, do you have a programming language that support creating this? or do you stuck with C, than you are limited to return codes
Actually, you can return structures... I would use something like:

Code:
struct Status {
    unsigned short severity;
    int   id;
    char  *message;
}
The severity can be used to for anything from bool (0 = no error, >0 warning level, 65535 = error).
The id can be used for anything (such as an error count, where more than 5 errors becomes fatal),
The message is just a pointer into a static message array for the specific error.
 
Old 12-06-2015, 01:44 AM   #15
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by jpollard View Post
Actually, you can return structures... I would use something like:

Code:
struct Status {
    unsigned short severity;
    int   id;
    char  *message;
}
The severity can be used to for anything from bool (0 = no error, >0 warning level, 65535 = error).
The id can be used for anything (such as an error count, where more than 5 errors becomes fatal),
The message is just a pointer into a static message array for the specific error.
yes of course you can, but you can ignore severity and access message which may point to something or not and have undefined behaviour. And the severity check will not be captured by lint or similar static analysis tools that check that you do not ignore a return value of a function, so this would not pass code review in some environments because it's insecure.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Data validation in c++ chocopie1986 Programming 1 10-12-2011 06:34 AM
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
Problem with Data Validation gregarion Programming 4 04-10-2010 12:37 AM
Problems with data validation and cin darkangel29 Programming 2 09-14-2008 08:23 PM
mysql - data validation at the table-creation-time prabhatsoni Linux - Software 2 03-24-2006 12:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration