473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Looking for feedback on coding style



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 UnrolledSidesExist()
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()
- RollMultipleTimes()
- UnrolledSidesExist()
---------------------------------

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.
Nov 16 '05 #1
1 1254
"Dgates" <dg****@spamlinkline.com> wrote in message
news:sd********************************@4ax.com...
(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
Nov 16 '05 #2

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

Similar topics

2
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...
1
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...
14
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...
6
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...
18
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 { }
144
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...
13
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...
3
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.