473,787 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is a better style?

Lets say I have a class that is only available if a specific DLL (a.dll) is
present. I can't link to that DLL through lib files or my app will fail on
any machine that doesn't have a.dll.

So I do LoadLibrary()'s and keep function pointers in my wrapper class...
Which is a better style?

DWORD CClass::SomeFun c()
{
if (m_pfn != NULL)
return (*m_pfn)(parm1, parm2);

return (DWORD) -1;
}

or

DWORD CClass::SomeFun c()
{
ASSERT(m_pfn != NULL);
return (*m_pfn)(parm1, parm2);
}

I guess the first version fails "more graciously" if the developer calls it
incorrectly, but the 2nd version isn't returning "made up" return codes and
is more "in your face" during development.
Oct 30 '05 #1
4 2265
Nobody wrote:
Lets say I have a class that is only available if a specific DLL (a.dll) is
present. I can't link to that DLL through lib files or my app will fail on
any machine that doesn't have a.dll.

So I do LoadLibrary()'s and keep function pointers in my wrapper class...
Which is a better style?

DWORD CClass::SomeFun c()
{
if (m_pfn != NULL)
return (*m_pfn)(parm1, parm2);

return (DWORD) -1;
}

or

DWORD CClass::SomeFun c()
{
ASSERT(m_pfn != NULL);
return (*m_pfn)(parm1, parm2);
}

I guess the first version fails "more graciously" if the developer calls it
incorrectly, but the 2nd version isn't returning "made up" return codes and
is more "in your face" during development.


Assertions are used during development to catch "that's not supposed to
happen" errors, such as bad arguments to a function. They disappear
when the program is compiled in "release" mode, so they are of no use
to convey information to users.

Returning error codes (or sometimes throwing exceptions) are common
ways to signal a "valid" error in a program. It is then possible to
display a user-friendly message, recover or fail.

As you can see, both ways are not necessarily exclusives. Actually, it
is common practice to use both assertions and error codes in the same
function. If it is normal (or predictable) for an error to happen, you
should not use (only) assertions to catch them, since you don't want a
user to see them.

Remember to be both user-friendly (try to recover or display an
informative message and fail graciously) and developper-friendly
(convey as much informations you can, such as with compile-time or
run-time assertions).
Jonathan

Oct 30 '05 #2
"Nobody" <no****@cox.net > wrote in message
news:5dV8f.779$ 5N1.161@dukerea d08...
: Lets say I have a class that is only available if a specific DLL
(a.dll) is
: present. I can't link to that DLL through lib files or my app will
fail on
: any machine that doesn't have a.dll.
:
: So I do LoadLibrary()'s and keep function pointers in my wrapper
class...
: Which is a better style?
:
: DWORD CClass::SomeFun c()
: {
: if (m_pfn != NULL)
: return (*m_pfn)(parm1, parm2);
:
: return (DWORD) -1;
: }
This function is ok if a value such as (DWORD)-1 makes sense
and can be interpreted as an error condition by the caller.
But as you mentioned, such an "invalid" result value may not
always exist -- so a separate way to report errors may be needed.

And this is exactly what exceptions are for.
I would actually write this function as:
DWORD CClass::SomeFun c()
{
if( m_pfn == NULL )
throwLibraryNot LoadedError(); // calls throw(..smthg.. )
return (*m_pfn)(parm1, parm2);
}

: or
:
: DWORD CClass::SomeFun c()
: {
: ASSERT(m_pfn != NULL);
: return (*m_pfn)(parm1, parm2);
: }

So what happens in release mode, assuming that ASSERT checks
are then disabled, if that function is called when m_pfn==0 ?
You will be getting undefined behavior (hopefully just a
crash of your program).
Can this ever be a good idea ?

: I guess the first version fails "more graciously" if the developer
calls it
: incorrectly, but the 2nd version isn't returning "made up" return
codes and
: is more "in your face" during development.

My approach is to throw an exception if one of the preconditions
of the function is violated (for instance, if no DLL was loaded).
To help with debugging, I may also want to trigger an assertion
failure, breakpoint or warning at debug time (i.e. within the function
throwLibraryNot LoadedError() ).

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact
form
Brainbench MVP for C++ <> http://www.brainbench.com

Oct 30 '05 #3

Nobody wrote:
Lets say I have a class that is only available if a specific DLL (a.dll) is
present. I can't link to that DLL through lib files or my app will fail on
any machine that doesn't have a.dll.

So I do LoadLibrary()'s and keep function pointers in my wrapper class...
Which is a better style?

DWORD CClass::SomeFun c()
{
if (m_pfn != NULL)
return (*m_pfn)(parm1, parm2);

return (DWORD) -1;
}

or

DWORD CClass::SomeFun c()
{
ASSERT(m_pfn != NULL);
return (*m_pfn)(parm1, parm2);
}

I guess the first version fails "more graciously" if the developer calls it
incorrectly, but the 2nd version isn't returning "made up" return codes and
is more "in your face" during development.


The difference between these two versions is more than an issue of
style. Each is making a very different statement about the program.
Either one may be correct, but not both.

The first version states that m_pfn could be NULL while this program is
executing; so it is necessary for the program to check for that
possibility and report it to the caller.

The second version states that the program is certain that m_pfn will
never be NULL while it is executing. The program's behavior is
therefore defined only when m_pfn is not NULL. Should that assumption
ever turn out not to be true, then the program's behavior will be
undefined. Ideally the program would crash quickly without causing any
harm.

Greg

Oct 30 '05 #4
Nobody wrote:
Lets say I have a class that is only available if a specific DLL (a.dll) is
present. I can't link to that DLL through lib files or my app will fail on
any machine that doesn't have a.dll.

So I do LoadLibrary()'s and keep function pointers in my wrapper class...
Which is a better style?

DWORD CClass::SomeFun c()
{
if (m_pfn != NULL)
return (*m_pfn)(parm1, parm2);

return (DWORD) -1;
}

or

DWORD CClass::SomeFun c()
{
ASSERT(m_pfn != NULL);
return (*m_pfn)(parm1, parm2);
}

I guess the first version fails "more graciously" if the developer calls it
incorrectly, but the 2nd version isn't returning "made up" return codes and
is more "in your face" during development.


I guess later is better - because not every function returns DWORD that
can be interpreted. Your code has to verify the presence of .dll abefore
calling methods anyway, so you are just duplicating this there.

BTW, in our system we have introduced nice related tool to automatize
generating such wrapper objects. To create such object, we provide
".dli" file with content like (actual example is for Lotus Notes client
..dll)

FN(WORD, OSLoadString, (HMODULE hModule, STATUS StringCode, char
*retBuffer, WORD BufferLength))
FN(WORD, OSTranslate, (WORD TranslateMode, char far *In, WORD
InLength, char far *Out, WORD OutLength))
FN(STATUS, NotesInitExtend ed, (int argc, char **argv))
FN(STATUS, OSPathNetConstr uct, (char *PortName, char *ServerName, char
far *FileName, char *retPathName))
FN(STATUS, NSFDbOpen, (char far *PathName, DBHANDLE far *rethDB))
FN(STATUS, NSFDbClose, (DBHANDLE hDB))
............

- basically, this is somewhat "reparsed" header file for .dll. Then place

#define DLLFILENAME "nnotes.dll "
#define DLIMODULE NOTES
#define DLIHEADER <notes/notes.dli>
#define DLLCALL LNPUBLIC
#include <Core/dli_header.h>

to common header file and

#define DLLFILENAME "nnotes.dll "
#define DLIMODULE NOTES
#define DLIHEADER <notes/notes.dli>
#define DLLCALL LNPUBLIC
#include <Core/dli_header.h>

to some .cpp file.

This creates global function NOTES() returning object instance that has
all .dll functions described in .dli file defined as its methods.
Moreover, it has operator bool that can be used to test whether .dll is
present:

char h[256];
if(NOTES())
NOTES().OSLoadS tring(GetModule Handle(NULL), ERR(nError), h, 255);
(http://upp.sf.net)

Mirek
Oct 30 '05 #5

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

Similar topics

220
19164
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
54
6577
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
21
2047
by: PassingBy | last post by:
I recently came across a template site selling cd's and was wondering what the groups opinion is of this? I purchased one of the cd's and the templates are great and Im looking forward to learning some design tips from the contents of the cd.... site: www.toptemplatecd.com Jim
7
2690
by: Bura Tino | last post by:
Hi, Going forward, what's better to use <td height="20"> or <td style="height:20;">
145
8862
by: Mark Johnson | last post by:
Oddly enough, I found it difficult, using Google, to find a list of best-of sites based on the quality of their css packages. So I'd ask. Does anyone know of particularly good sites which are in good measure because of their creative and useful css designs? I'm aware of Zen Garden and a few others. So don't bother with those. And I hope I don't get replies from people with a 'tin ear' and no design sense. Good sites. Good pages. That's...
12
2403
by: Kevin Blount | last post by:
I'm playing with dragable <DIV>s, being inspired by google.com/ig, where you can move items on the page around and have items you move over change position if necessary. I have 3 div's setup, one for each dragable item (boxes about 100x150px), inside another div that sets up a border. my script is instigated by an onMouseOver event and works great for moving items, setting the opacity, etc. The only trouble is (and I guess I should...
9
2460
by: Jay | last post by:
Everywhere I go (read/browse) I see these parameters.... ByVal sender As Object, ByVal e As System.EventArgs Yet I never see them used within the function/method. Could someone tell me what they mean/do please. JP
8
2010
by: clintonG | last post by:
Every single time neophytes or converts ask about naming and style conventions what are they told by the majority consensus? The answer is "do what you prefer but do so consistently" right? Yes, that's right, that's exactly what is said. People joining a team of developers are also encouraged to cooperate with their peers and adopt what has already been established right? Yes, that's exactly what is encouraged. Teamwork and cooperation...
49
2438
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books, internet, etc.) and found out about the CLR and how all of the .NET languages access it, the major difference being the syntax and structure of the individual languages. What I'm wondering, since VB.NET is obviously easier to learn/use than C#.NET and...
9
1967
by: tvnaidu | last post by:
This is just small plain HTML program wtote manually (since I am new to web programming, may be some tools I can use I think), this program loaded onto microcontroller with small foot print web server, when program prints, half-way it prints, then it struck, any issue with this?. when user clicks "submit" again it loaded same page. but it sends some sytring to server (plug1=ON&......), that string will be processed inside server. any open...
0
9498
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
10363
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
10110
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
8993
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...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.