473,761 Members | 8,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this good style of C++?

Recently I happens to read some sourcecode which has different style
from my previous experience.

In this style, all functions return a RETURN_TYPE_T which is defined as
typedef unsigned int RETURN_TYPE_T;
and the return value can only be enum value
enum
{
SUCCESS = 0,
FAILURE = 1,
ABORT = 2
};
If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other
way first, and then call this method with the pointer as parameter.

The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value.

This style of coding seems strange to me, but I am told that it is a
good style, because it applys the rule "Who creates it, who releases
it". As a result, this style are supposed to reduce risk of memory leak.

I am still not quite convinced. Anybody with long time C++ exprirence
can give some comments?
Thanks & Regards

Jul 22 '05 #1
29 2017
Cheng Mo wrote:
Recently I happens to read some sourcecode which has different style
from my previous experience.

In this style, all functions return a RETURN_TYPE_T which is defined as
typedef unsigned int RETURN_TYPE_T;
and the return value can only be enum value
enum
{
SUCCESS = 0,
FAILURE = 1,
ABORT = 2
};
If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other
way first, and then call this method with the pointer as parameter.

The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value.

This style of coding seems strange to me, but I am told that it is a
good style, because it applys the rule "Who creates it, who releases
it". As a result, this style are supposed to reduce risk of memory leak.

I am still not quite convinced. Anybody with long time C++ exprirence
can give some comments?



If such a style is used in an application for a particular reason (that
is, it is not a style), then it is OK.
Otherwise as a *general style* of programming for C++ it looks primitive
and ancient.
If you want to handle errors you had better use exceptions, and not
return error code checks that this style implies.
If you want to create bullet proof code without memory leaks or *any
other resource* leaks, use "Resource Acquisition is Initialisation" (RAII):
http://groups.google.com/groups?q=%2...ntua.gr&rnum=1


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2
All in all its horrible.

"Cheng Mo" <mo******@nospa m.nospam> skrev i en meddelelse
news:co******** **@avnika.corp. mot.com...
Recently I happens to read some sourcecode which has different style from
my previous experience.

In this style, all functions return a RETURN_TYPE_T which is defined as
typedef unsigned int RETURN_TYPE_T;
and the return value can only be enum value
enum
{
SUCCESS = 0,
FAILURE = 1,
ABORT = 2
}; Do not use UPPERCASE_ONLY for values. These names should be reserved for
macroes only. If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other way
first, and then call this method with the pointer as parameter. Also horrible. The first signature should be preferred unless the call could
fail in a normal case in which case a smart pointer (see boost) would be
appropriate.
The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value. In general, using exceptions is far more reliable. There's no forgetting of
checking the return-type, and you would not have to obfuscate your code with
all that checking.
This style of coding seems strange to me, but I am told that it is a good
style, because it applys the rule "Who creates it, who releases it". As a
result, this style are supposed to reduce risk of memory leak. This is good advice, but not so relevant for C++. Use RAII instead.
I am still not quite convinced. Anybody with long time C++ exprirence can
give some comments?
Thanks & Regards


Kind regards
Peter
Jul 22 '05 #3
Peter Koch Larsen wrote:
Do not use UPPERCASE_ONLY for values. These names should be reserved for
macroes only.

Actually one convention is to name all constants with uppercase, be them
macros, const objects, or members of an enumeration.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
>> If a function should return a object, the API is not like
Foo getFoo();
or
Foo* getFoo();
Instead, it is declared as
RETURN_TYPE_T getFoo(Foo* foo);
The caller has to new a Foo or get pointer to Foo object through other
way first, and then call this method with the pointer as parameter.

The function fill valuable info into the Foo object pointed by the
paramter. The result of invocation is judged by inspecting the returned
RETURN_TYPE_T value.

This style of coding seems strange to me, but I am told that it is a
good style, because it applys the rule "Who creates it, who releases
it". As a result, this style are supposed to reduce risk of memory leak.
If you want to handle errors you had better use exceptions, and not
return error code checks that this style implies.


Unless errors are frequent and expected, in which case exceptions are not
such a good choice. Throwing an exception typically has big overheads, so
unless given error is *exceptional*, it should be handled w/o throwing an
exception. Network code comes to mind, if transmission errors, timeouts
etc. are frequent.

Cheers, Kuba Ober
Jul 22 '05 #5
Kuba Ober wrote:
Unless errors are frequent and expected, in which case exceptions are not
such a good choice. Throwing an exception typically has big overheads, so
unless given error is *exceptional*, it should be handled w/o throwing an
exception. Network code comes to mind, if transmission errors, timeouts
etc. are frequent.

Well, my experience is in .NET, where every error is signified as an
exception and not as a return value, and so far I had no problems with
it. Also all the C++ standard library with the exception of the C
subset, uses exceptions to signify run-time errors.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

"Kuba Ober" <ku**@mareimbri um.org> wrote in message
news:co******** **@charm.magnus .acs.ohio-state.edu...
If you want to handle errors you had better use exceptions, and not
return error code checks that this style implies.


Unless errors are frequent and expected, in which case exceptions are not
such a good choice.


Unexpected behavior is pretty much the definition of an exception.
Jul 22 '05 #7
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in
news:1102010767 .803954@athnrd0 2:
Kuba Ober wrote:
Unless errors are frequent and expected, in which case exceptions are
not such a good choice. Throwing an exception typically has big
overheads, so unless given error is *exceptional*, it should be
handled w/o throwing an exception. Network code comes to mind, if
transmission errors, timeouts etc. are frequent.

Well, my experience is in .NET, where every error is signified as an
exception and not as a return value, and so far I had no problems with
it. Also all the C++ standard library with the exception of the C
subset, uses exceptions to signify run-time errors.


I don't agree with that assessment. End-of-file on a stream, for example,
is _not_ an exception, neither is dynamic_casting a pointer to an unrelated
type. Offhand I can only think of a couple of places where an exception is
thrown from the Standard library, and that's because either it really is an
exceptional case (like running out of memory), or there is no other way to
indicate an error (like dynamic_casting a reference to an unrelated type,
or using .at() on a vector past the end of the vector).
Jul 22 '05 #8
Andre Kostur wrote:
I don't agree with that assessment. End-of-file on a stream, for example,
is _not_ an exception, neither is dynamic_casting a pointer to an unrelated
type.

These are not errors. End-of-file is just the end of a file and
dynamic_cast is actually a run-time test.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in
news:1102022154 .76165@athnrd02 :
Andre Kostur wrote:
I don't agree with that assessment. End-of-file on a stream, for
example, is _not_ an exception, neither is dynamic_casting a pointer
to an unrelated type.


These are not errors. End-of-file is just the end of a file and
dynamic_cast is actually a run-time test.


Then I don't understand exactly what you would call an "error" then. IMHO:
If I try to read some data from a stream, and I don't get the data I was
expecting, then I'd call that an error. If I try to dynamic_cast a pointer
to an unrelated type, the runtime will return me a NULL to signify that
error. If I try to dynamic_cast a reference to an unrelated type, the
runtime will throw an exception to signify that error.
Jul 22 '05 #10

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

Similar topics

24
3612
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works, first and foremost. If it works well, even better. The same goes for ease of maintenance, memory footprint, speed, etc, etc. Most of the time, people are writing code for a use in the *real world*, and not just as an academic exercise. Look at...
3
1583
by: Woolly Mittens | last post by:
I'm depressed... The customer bought XHTML strict templates from me. The customer is a university and wants to set a good example. They gave the strict XHTML templates to a third party content management developer. The third party wanted to set a good example to... I suppose they wanted to prove you can build a content management system using dreamweaver *sob*.
39
7663
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
43
2662
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some serious motivations for many issues... I hope you can help me :) I begin with the two major for now, others will come for sure! - function variables: they're used to java, so no pointers. Personally I'd use always pointers, but why could be better...
2
1415
by: Sky Sigal | last post by:
Hello: I'm currently messing around, and need as much feedback/help as I can get, trying to find the most economical/graceful way to build usercontrols that rely on styling to look any good... It's the last part that has got me all frazzled (the 'rely on...to look good')... Let me explain -- and please bear with me as it's a bit longer than my usual questions:
0
1211
by: André | last post by:
I'm trying to change an app so that it uses gettext for translations rather than the idiosyncratic way I am using. I've tried the example on the wxPython wiki http://wiki.wxpython.org/index.cgi/RecipesI18n but found that the accented letters would not display properly. I have found a workaround that works from Python in a Nutshell; however it is said in that book that "...this is not good style". I would like to do things "in good...
2
1972
by: silvrfoxx | last post by:
I created this using notepad and was checking it in Firefox, but when i finished and checked it in IE it looks like crap. I even opened up Dreamweaver and it shows it looking great. I can't find what is causing it. I have double, triple checked to make sure that all my ", <, >, tr, td, etc. are opened and closed properly. Please can anyone help? Thanks in advance. Here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
14
1838
by: Astley Le Jasper | last post by:
I'm still learning python and would like to know what's a good way of organizing code. I am writing some scripts to scrape a number of different website that hold similar information and then collating it all together. Obviously each site needs to be handled differently, but once the information is collected then more generic functions can be used. Is it best to have it all in one script or split it into per site scripts that can then...
1
2045
by: Sarvesh1989 | last post by:
I have one main div & in that main div i have included 2 differrent div it show good result in firefox but in ie it doesn't show that same result div just get one above the other.plz do help me. my ccs is: /* General elements */ body {
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
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
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8813
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
7358
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
6640
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();...
0
5266
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2788
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.