D
Dgates
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.
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.