Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 31st, 2007, 3:35 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That's the OP's call. He said "short", not "looks short." Abstraction has its uses. As a means of programmer productivity at the expense of performance, I think this one has already failed.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 31st, 2007, 3:53 AM   #12
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by DaWei View Post
If it makes me a curmudgeon to point out when you're being silly, yes, indeed. The OP distinctly asked for a short way. Merely hiding things so that they aren't visible at the point of invocation doesn't qualify as either short or elegant.
I disagree. Hiding code away within black box is exactly what object orientation is all about. It's unlikely this solution will affect the program significantly in terms of efficiency, and from the outside it's very concise.

That said, I think the solution is silly, but for reasons different to your own, DaWei. It seems to me as if this:
discount = Percent(20)
origprice = 35.00
saleprice = origprice - discount
Is less understandable than this:
origprice = 35.00
saleprice *= 0.8
Because you're overriding the subtraction operator to do something that isn't immediately obvious. You'd have to keep track of all your Percent objects, or not assign them to any references, otherwise things could get confusing.

Multiplication is well understood by any programmer, and multiplying by 0.8 should instantly be recognisable as a reduction of 20%. Wrapping this up in a class just makes it harder to figure out what's going on.
Arevos is offline   Reply With Quote
Old Jan 31st, 2007, 5:34 AM   #13
ptmcg
Newbie
 
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0 ptmcg is on a distinguished road
Just a couple more examples:

fica = Percent(7)
fedtax = Percent(15)
medicare = Percent(3)
deductions = fica + fedtax + medicare
gross = 100000
net = gross - deductions
print net # answer: 75000

wholesale = 10
markup = Percent(35)
retail = wholesale + markup
print retail # answer: 13.5

yearlyApprec = Percent(8)
basis = 10000
newbasis = basis + yearlyApprec + yearlyApprec + yearlyApprec
print newbasis # answer: 12597.12

I still think this is rather pretty, the kind of thing that DSL's are made of. Imagine applying this same technique (emulating numeric types using _add_, _radd_, etc.) to applications such as:
  • adding resistors in a network
  • adding temperature of various quantities of water
  • adding vectors
  • adding physical bodies (with gravity, momentum) to a dynamic model
Yet I can certainly sympathize with the maintainer who, trying to debug the above code later wonders, "where the hell do they calculate the markup?".

Still, here is what the OP started with:
Tolerance = 0.05 * Energy
Energy = Energy - Tolerance

Of course, this is probably the simplest and most direct implementation of subtracting 5% of something from something else. Why was the OP asking the question in the first place? The Percent class illustrates an atypical object capability provided by Python, to be able to define an object that can take additional information from its arithmetic context to modify its behavior. With Percent, the OP can define
Tolerance = Percent(5)
completely independently from what the tolerance is to be calculated on, and then later apply it to Energy using
Energy = Energy - Tolerance
or even just
Energy -= Tolerance

So is this Percent class silly overkill for something so mundane as calculating a percentage? Perhaps. But I find Python's numeric type emulation capabilities to be quite interesting, and it seemed a decent fit and a simple example to the OP's question.

Last edited by ptmcg; Jan 31st, 2007 at 6:18 AM. Reason: after re-reading the original post; added one more example
ptmcg is offline   Reply With Quote
Old Jan 31st, 2007, 7:51 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Hiding code away within black box is exactly what object orientation is all about.
I disagree. Object orientation is about building things as objects. The encapsulation may be used to protect parts of the object from unintended use, if one wants to call that hiding. Don't attach a spring to the butterfly valve in my carburetor in order to make a gauge showing how much the accelerator is depressed. Find another way or ask me to expose something suitable. Similarly, enclosing a carburetor in a spherical rubber case is a silly, though perhaps interesting, way to make a basketball.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 31st, 2007, 8:34 AM   #15
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by DaWei View Post
I disagree. Object orientation is about building things as objects. The encapsulation may be used to protect parts of the object from unintended use, if one wants to call that hiding.
That's essentially what I was angling toward. OO restricts and hides information, and as far as I'm aware, this is the only difference between OO and procedural languages.

Unless you consider OO to be more a state of mind, rather than an syntactical attribute of a programming language?
Arevos is offline   Reply With Quote
Old Jan 31st, 2007, 10:52 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I consider OO to be a design methodology that far predates its 'discovery' in the software arena. It is far more than a syntactical attribute in those areas. MEs and hardware designers would probably be surprised to discover that it's considered abstruse.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 4th, 2007, 7:46 PM   #17
kurt
Programmer
 
Join Date: Oct 2005
Posts: 68
Rep Power: 3 kurt is on a distinguished road
Thanks, ptmcg, Arevos, DaWei and everyone for your feedback.

As I just started learning Python for work, I was wondering if Python has special utilities or functions that can perform what I posted previously, albeit it is already very short and simple.

The examples given are interesting and I thank you all for it.

Your different views on OOP (well, from a different perspective, at least) is thought provoking, and it's really beneficial for a fresh graduate like me.

Thanks, everyone.
kurt is offline   Reply With Quote
Old Mar 12th, 2007, 3:30 PM   #18
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
"%" can be expressed with <b>e-2</b>.
free-zombie is offline   Reply With Quote
Old Mar 17th, 2007, 12:27 PM   #19
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
This thread made me smile. Only on a programming forum will you see people carefully rip apart and analyze something as simple as calculating a percentage.

Thanks for the laugh guys.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ strange arithmetic? Mcoy C++ 13 Mar 18th, 2006 12:49 AM
Arithmetic CodeJunkie Assembly 2 Jan 8th, 2006 2:28 PM
modulo arithmetic question LOI Kratong C++ 6 Sep 28th, 2005 2:07 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC