473,796 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to hide implementation details

Hi,

I was reading effictive C++ and some other books again and they all tell
you about hiding implementation details (proxy/pimpl/inheritance) but
they never really explain when to use it.

I am starting on a new project which is part library so I think it would
be good to hide the implementation for the public classes in the library
but this seems a lot of overhead to me (both when developing and runtime
overhead).

For example, it needs to interface with C and I created a class
MemoryBuffer which basically just allocates memory and allows some
operations on the memory buffer. This class is in this library and is
used by several client applications. So, should I hide the implementation
for this class? I think I would but somehow this seems like lots of
unneeded overhead.

Another example, I have exception classes which stores a message and some
other information. Should I hide this information?

Anyway, my concern is that if I just hide all implementation details I
might overdo the whole implementation hiding idea and make the code less
clear and maintainable. Are there any rules on when to hide
implementation and when not ?
The only rule I could find is basically 'hide everything and when the
profiler says it is slow, unhide it'. Is this a good approach ?

I hope you understand what I am trying to say and have some good advice.
Thanks,

Ralph.
Jun 27 '08 #1
6 3967
Ralph wrote:
I was reading effictive C++ and some other books again and they all
tell you about hiding implementation details
(proxy/pimpl/inheritance) but they never really explain when to use
it.
Early reaction: you hide something you don't want others to see.

What would be the reason you don't want people or tools to see
something? It could be (a) proprietary technology you're proud
of but want to keep secret, (b) bad code that you're ashamed of,
(c) something that could make people/tools waste too much time
looking at or exploring. There are probably other reasons that
at this time escape from me.
I am starting on a new project which is part library so I think it
would be good to hide the implementation for the public classes in
the library but this seems a lot of overhead to me (both when
developing and runtime overhead).

For example, it needs to interface with C and I created a class
MemoryBuffer which basically just allocates memory and allows some
operations on the memory buffer. This class is in this library and is
used by several client applications. So, should I hide the
implementation for this class? I think I would but somehow this seems
like lots of unneeded overhead.
If you can't find a good reason to hide that, and all you can see is
the overhead, then why hide? See the reasons I enumerated above.
Another example, I have exception classes which stores a message and
some other information. Should I hide this information?
Again, it's _competely_ up to you. We can't "should" you, we can
only tell you where others hide some implementation and why (if
they actually told us or we guessed).
Anyway, my concern is that if I just hide all implementation details I
might overdo the whole implementation hiding idea and make the code
less clear and maintainable. Are there any rules on when to hide
implementation and when not ?
No, there are no rules. There are general guidelines, maybe.
The only rule I could find is basically 'hide everything and when the
profiler says it is slow, unhide it'. Is this a good approach ?
No. If the profiler tells you to unhide a state secret, for which
you can go to jail for life, would you do that?
I hope you understand what I am trying to say and have some good
advice. Thanks,
Performance [of the final code] is rarely a concern when hiding
implementation details is discussed. Performance of the compiler,
OTOH, is. One of the reasons for PIMPL pattern is to _reduce_
dependencies between modules, so when you change one, you don't
have to recompile the other.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
Ralph wrote:
I was reading effictive C++ and some other books again and they all tell
you about hiding implementation details (proxy/pimpl/inheritance) but
they never really explain when to use it.
....
>
Anyway, my concern is that if I just hide all implementation details I
might overdo the whole implementation hiding idea and make the code less
clear and maintainable. Are there any rules on when to hide
implementation and when not ?
The only rule I could find is basically 'hide everything and when the
profiler says it is slow, unhide it'. Is this a good approach ?
Hiding the implementation does result in a performance hit because all
actions must be redirected to the underlying implementation, and you
cannot take advantage of inline.

The most common reason for hiding the implementation is to avoid the
necessity to recompile all of the programs that reference a library. So
commercial libraries usually hide their implementation so their
customers can install new versions of their product without having to
recompile all of their applications. For example consider the havoc
that would arise if every time Microsoft issued a fix to their libraries
everyone had to recompile all of their applications! However if your
library has only a small well-known set of clients then requiring a
global recompile may not be a big deal.
Jun 27 '08 #3
On Thu, 24 Apr 2008 10:12:55 -0400, Victor Bazarov wrote:
What would be the reason you don't want people or tools to see
something?
Ah, yes, that was what I forgot to mention:
My main reason for hiding is to prevent recompiling everything when
something changes in the implementation and have a cleaner interface for
other users without all the distracting private stuff.

If you can't find a good reason to hide that, and all you can see is the
overhead, then why hide? See the reasons I enumerated above.
I was just wondering because all the books says hiding implementations is
a good idea but they almost never tell you when you should use it.

Again, it's _competely_ up to you. We can't "should" you, we can only
tell you where others hide some implementation and why (if they actually
told us or we guessed).
Ok, then I should have asked 'when do you hide implementations ?'
So, when do you hide implementations ? ;)
Are there some general guidelines?

>The only rule I could find is basically 'hide everything and when the
profiler says it is slow, unhide it'. Is this a good approach ?

No. If the profiler tells you to unhide a state secret, for which you
can go to jail for life, would you do that?
Well, I think they mean just hide the implementation of all classes but
in the final product you can unhide performance bottlenecks.
Thanks,

Ralph
Jun 27 '08 #4
Ralph wrote:
[..]
Ok, then I should have asked 'when do you hide implementations ?'
So, when do you hide implementations ? ;)
Are there some general guidelines?
Generally, if your implementation is dynamic, you have no other
choice but to use the pimpl idiom. The pointer is to the base
(usually abstract) class that implements some behaviour and the
actual implementation is obtained from an external module loaded
by the system based on some kind of configuration setting. This
is the strongest reason to "hide" the implementation. Making
the object lightweight if it's stored/copied much more often than
its implementation counterpart is another reason. Whether you
actually _hide_ the implementation or simply _separate_ it from
the "interface" , is up to you, but I'd consider it "hiding".
>>The only rule I could find is basically 'hide everything and when
the profiler says it is slow, unhide it'. Is this a good approach ?

No. If the profiler tells you to unhide a state secret, for which
you can go to jail for life, would you do that?

Well, I think they mean just hide the implementation of all classes
but in the final product you can unhide performance bottlenecks.
The point is, you have to weigh the relative importance of the
reasons you used to hide the implementation versus the reasons
telling you to expose it. If the performance is important, and
more than the ability to substitute implementations without
forcing your class users to recompile, put everything in one
class and be done with it, if you found that separating the
implementations causes performance problems. But I thought that
it went without saying...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #5
Ralph wrote:
I was just wondering because all the books says hiding implementations is
a good idea but they almost never tell you when you should use it.
Probably they all copy it from earlier books :).
The main reason is that a programmer using a module is less inclined to
rely on implementation details (a bad thing to do) if the implementation
is somehow hidden and only an interface is visible. I doubt that
actually forbidding the programmer access to implementation details is a
good idea, though. It isn't really possible with open-source anyways and
if the programming language doesn't allow it, programmers will find some
workaround, which will not be pretty either.
Jun 27 '08 #6
On Thu, 24 Apr 2008 13:33:39 +0000, Ralph wrote:
I was reading effictive C++ and some other books again and they all tell
you about hiding implementation details (proxy/pimpl/inheritance) but
they never really explain when to use it.
After reading all the comments and thinking about it I think I just don't
hide implementation details unless recompiling is going to take a long
time after a really small change. Since my project is not that huge, it
should be ok without hiding implementations .

Thanks again.

Ralph.
Jun 27 '08 #7

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

Similar topics

3
698
by: forums_mp | last post by:
After reading a few texts - nameley koeing/moo and eckel - I decided to investigating the importance of templates. Of course there's no substitute for practice so I figured I try a simple program. That said I'm interested in suggestions for improvement on the template class (be gracious i'm slowly coming up to speed in ++ :)) but more importantly I have a few questions. For starters, in an ideal world each call to Store should be...
2
4482
by: pvinodhkumar | last post by:
I am reading Lippman's Inside C++ object model. I feel lonely because the Microsoft C++ compiler which I use does not provide me an implementation details manual, describing how they implement vtables in case of single inheritance and multiple inheritance. Just to verify what I read is correct I don't have the same from any other compiler vendors also. I do not know where CFront's implementation manuals are.
1
1567
by: P Vinodh Kumar | last post by:
Reposting, please give your ideas/suggestions/comments. I am reading Lippman's Inside C++ object model. I feel lonely because the Microsoft C++ compiler which I use does not provide me an implementation details manual, describing how they implement vtables in case of single inheritance and multiple inheritance. Just to verify what I read is correct I don't have the same from any
2
2582
by: Greg | last post by:
Hello, I am trying to display order ids and order details (order items). I would like to give the user Hide/Show option to either display or hide order details. The page would look like: Expand All Collapse All
15
2354
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in computers memory? Or does Python hide such implementation details that deep, that there is no way to get down to them? The test code below shows, that extracting bits from an integer value n is faster when using n&0x01 than when using n%2 and I...
1
1293
by: vighnesh | last post by:
Hi Folks Can anybody please help me in finding out information ( implementation details )regarding Registry Optimization, defragmentation ? Since I am dealing with an application in which I have to implment those features, I googled the NET, but clouldn't find any implmentation details than some tools that do these defragmentation and Optimization. Please suggest me . I am thanking you very much in advance.
3
16995
by: hitoall | last post by:
Hi, I have a requirement to hide the web page being viewed by the user. i.e. the address bar should always display www.mysite.com. I donot want to use IFrame for this.. is there anyother way to achieve this? Thanks...
7
2168
by: somenath | last post by:
Hi All , As a process of my C language learning I was trying to learn how malloc can be implemented from K&R2 .But I am not able to understand few points . It would be very much helpful if some body give some inputs in the bellow mentioned points. Point 1) I page 186 it is said that "In malloc, the requested size in characters is rounded up to the proper number of header-sized units; the block that will be allocated contains one...
1
1136
by: x_O | last post by:
Hi I'm working recently with XMLRPC for python and It never was so easy. But I've meet a obstacle. Because python is not giving us any reasonable encapsulation mechanism, I have problems with hiding some part of the implementation. When class has 2 methods and I want to make rpc private only ONE of them. I can always use __name. But what in case when I really need to
0
10457
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...
0
10231
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
9054
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
7550
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
6792
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
5443
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...
1
4119
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
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
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.