I'm learning ASP.NET, C# and VB.NET, and hoping to get some feedback
from more experienced programmers on a few issues regarding efficient,
readable, well-organized code.
I'm trying to program anything I do in the best possible "style," even
if it's just a silly experiment (e.g., a simulation to find out the
average number of times you have to roll a die before all six sides
have come up).
With quickie experiments at home, alone, it's hard to ask myself
questions like:
- "Will my co-workers want access to my UnrolledSidesEx ist()
function?"
- "Do I need to speed up the efficiency of this process?"
I'll describe the 2 classes I've coded and then list my 4 main
issues/questions.
---------------------------------
CLASS #1:
The CoinGame class calculates expected results if you play a game
where you can triple your money every time you roll heads (and lose it
all as soon as you roll tails).
For this class, I put all the code into *one big method*, which ended
up nested in about 3 or 4 levels of for-next loops.
---------------------------------
CLASS #2:
The Roller class simulates rolling a die until all sides have come up
at least once. I split this into these methods:
- RunTests()
- RollMultipleTim es()
- UnrolledSidesEx ist()
---------------------------------
ISSUES/QUESTIONS:
(1) I feel "guilty" creating an object. I keep thinking I'd be
"wasting" the computer's time creating an object just for some little
thing, especially when the object would have to be instantiated
thousands of times.
For example:
- a Person object that rolls a die until he's rolled all sides, or
- a Die object that knows which of its sides have been rolled.
----------
(2) I coded all my methods as "static." I never instantiate a Roller
object or a CoinGame object.
Am I defeating the whole point of using an OO language here?
----------
(3) I read a coding practice that said something like:
"Once your [blank] gets to be more than 30 lines long, you should
break it into [blank]s."
Was that referring to breaking a method into smaller methods? And is
the goal simply more readable code?
----------
(4) When I split a method into several smaller methods, I saw a new
dilemma:
Should I pass the same two or three parameters around every time one
method calls another? Or should I declare (private) class-level
variables to which all of the methods have access?
----------
I'm open to any suggestions, whether they're about the 4 issues I
listed, or my basic assumptions, or just a request to see the code in
question.
Thanks in advance. 1 1269
"Dgates" <dg****@spamlin kline.com> wrote in message
news:sd******** *************** *********@4ax.c om... (1) I feel "guilty" creating an object. I keep thinking I'd be "wasting" the computer's time creating an object just for some little thing, especially when the object would have to be instantiated thousands of times.
For example:
- a Person object that rolls a die until he's rolled all sides, or - a Die object that knows which of its sides have been rolled.
You are overly obsessed with performance. Do what makes logical sense, and
then IF you see a demonstrable performance issue, you can tune from there.
And you will quickly learn what is expensive and what is not. You learn by
doing. Don't be afraid to make mistakes -- be willing to make them.
Objects are your friends ... they are not to be hoarded and used when
absolutely necessary, but used intelligently just like variables or anything
your'e already familiar with. A design with thousands of live objects is
common in any non-trivial application.
(2) I coded all my methods as "static." I never instantiate a Roller object or a CoinGame object.
Am I defeating the whole point of using an OO language here?
Static is appropriate where the method does not need instance data to do its
work, or where you will share data amongst all instances of a class.
(3) I read a coding practice that said something like:
"Once your [blank] gets to be more than 30 lines long, you should break it into [blank]s."
Was that referring to breaking a method into smaller methods? And is the goal simply more readable code?
Yes, you probably were reading that long methods should be broken down into
smaller methods. That is almost always good advice. And yes, the primary
goal is more readable / maintainable code. Remember that you (and perhaps
other humans) will have to read the code; and you can only keep a finite
amount of the problem in your head at one time.
(4) When I split a method into several smaller methods, I saw a new dilemma:
Should I pass the same two or three parameters around every time one method calls another? Or should I declare (private) class-level variables to which all of the methods have access?
Depends on the context. Parameters increase calling overhead, as well as
complexity. So, keeping them to a minimum (within reason) is a Good Thing.
If you find yourself passing the same bits of info around between methods
then they are probably better made into private or protected members. Ask
yourself if the info is an attribute of the class, or something internal to
the method. If it has to do with the class, then it should be a class
member.
--Bob This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: osgnamah |
last post by:
Hi All;
I am getting ready to launch a shareware program in the next few weeks
and the last step is going to be creating a website. So the last few
days I've been doing some web surfing in trying to figure out how I am
going to manage to do that in the next few weeks since it will be my
first go at html. Actually that's not true it will be my second go at
it since I did a homepage in the good old days of the lynx browser,
gophering,...
|
by: Oliver Hoehle |
last post by:
Hello!
This ist the source-code for an editable combobox implemented with
HTML,CSS and Javascript.
I have tested it with IE and Mozilla. But I don't know, if it will
work in other browsers (Opera, Konqueror, etc.) So I need your
feedback...
Regards
|
by: Gérard Talbot |
last post by:
I personally disagree with the Viewable in Any browser campain. The site
is frankly outdated, seems to be no longer maitained, has several
recommendations that don't make a lot of sense anymore. In my mind, it
is much more important to code according to standards (valid markup and
CSS code) and good coding practices (tableless design, graceful
degradation, etc.) than to code according to the lower common
denominator among browsers.
...
|
by: LenS |
last post by:
Hello
Was wandering if there is any place where some one could go to get
mentoring on python coding. I have started coding in python but I am
the only one in the shop using it. So there is no one around to look
over my code give suggestions on improvement, in style, logic, easier
ways of doing things etc. I am not really looking for hand holding as
much mentoring. I have purchased about every python book out and have
a list a mile...
|
by: craig |
last post by:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are
there names for these style?
style 1:
method {
}
| |
by: Natt Serrasalmus |
last post by:
After years of operating without any coding standards whatsoever, the
company that I recently started working for has decided that it might be a
good idea to have some. I'm involved in this initiative.
Typically I find that coding standards are written by some guy in the
company who has a way of coding that he likes and then tries to force
everybody else to write code the way he likes it, not for any rational
reason, but simply for the...
|
by: benben |
last post by:
Is there an effort to unify the c++ coding standard? Especially
identifier naming.
Not a big issue but it would be annoying to have to incorporate
different coding styles simultaneously when using more than one library.
The standard library seems to have everything lower-cased while a lot of
other libraries do their own way.
Ben
|
by: Carl Youngblood |
last post by:
I own the 3rd edition of Stroustrup's THE C++ PROGRAMMING LANGUAGE and
have read most of it, but I find its style to be more suitable for
reference than for tutorial. I still have a lot to learn about
template programming and don't have any experience with 3rd party
libraries, such as Boost. I'm looking to buy two more books that I'm
hoping will help take my C++ programming to the next level. Here are
some of my choices:
Beyond the...
|
by: lorlarz |
last post by:
Looking for feedback on Matching Exercises Maker/ Builder:
http://mynichecomputing.com/ReadIt/translateT.html
For one thing, I am concerned about storing the matching kwork (known
word)
as the className of the li which is the answer. I think this may be
problematic.
I also worry about using the ul as the container for the whole thing.
|
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,...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |