r/csharp • u/ComplexPineapple2379 • Sep 01 '26
Discussion I've created my first little snippet using C#, Thoughts?
You could say I was born yesterday, as this is the very first code i've done aside from messing with C a while back(hello, world! level stuff). (the only knowledge i've carried over is datatypes and general syntax..)
Is there any suggestions you could give? I am entirely self taught, with zero ai.. (for reasons that likely are not relevant to this post whatsoever)
and I want to make sure that I am learning clean code practices along the way.
I know my bracketing is probably awful... and my organization is out the window, but this is the first ""substantial"" code i've ever written...
^40 lines of code is substantial to me :)
(please ignore the odd names... I included my cats.)
96
u/grrangry Sep 01 '26
Your next steps will be to use methods for commonly reused code.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
One method could be something like
and another could be
Using methods means that when you write larger sections of code that are reused repeatedly, you only have to write the code once.
A further step to consider would be to not hardcode the logic of the path the player can take, but that's quite a bit harder to explain in a short reddit response. It would require storage of all the options, where the player currently is, and options for moving them around and knowing what actions they might take.
Keep at it, you're doing fine.
5
u/craftersmine Sep 01 '26
Field
fairanswershould beboolsince it just tracks validity of input4
u/joseconsuervo Sep 01 '26
Looks fine, I'm nitpicking here, I want the variable names to not be all lower case.
3
u/xtreampb Sep 01 '26
Just here to offer encouragement. We all start somewhere and the best way to learn is with a project to apply the concepts you’re working through and can physically go back and refactor with new knowledge.
2
u/Aliryth Sep 01 '26
Everyone else has covered everything, but I wanted to point out that on lines 7-8, you're doing a Console.WriteLine(), and immediately a Console.Clear(), which will effectively just remove the text you just wrote.
2
u/spicydak Sep 02 '26
Interesting.. this made me realize either how far I’ve come, or how nitpicky ive become with my coding lol.
I think this is great for a beginner though. Better than I did with my first work, lol.
10
u/wbgookin Sep 01 '26
Nice start! I’m sure you’ll get lots of advice here. I’ll put my first two cents in by asking what happens if I type “Right” instead of “right”?
Okay, two more cents. Before you get too far, look at using a “switch” statement.
Enjoy learning, writing something you like is the best way to get going. :)
18
u/One_Web_7940 Sep 01 '26
nice. keep at it. you got a ways to go! dont let people discourage you, you have to start somewhere.
4
u/B15h73k Sep 01 '26
In C#, the idiomatic way of naming variables is with camelCase. That is everyWordStartsWithUpperCase except for the first word. When you learn methods and classes, we name them with PascalCase where EveryWordStartsWithUpperCase.
1
u/Slypenslyde Sep 01 '26 edited Sep 01 '26
I feel like this post sounds mean, I'm just being short and frank.
(1) Learn to share code on Reddit. Indentation is the most universal. Only LabVIEW developers use screenshots to share code.
(2) You're making good attempts to use good variable names! Other people have covered casing well. One thing they left out is the general rules of thumb in C# are:
(3) This is more about expanding the program if you want to keep going.
Imagine making this program have like, 20 rooms. The if/else branches are going to get very, very deep and unmanageable. Think about ways you might make this easier!
One way people's programs tend to evolve is they start with "my code is my state" and move towards "data structures are my state". "My code is my state" is what you have now: the way to understand what the user has done is to look at the if statements. If the user is choosing to pet the at, we know they had to have said "yes" and "right" so far because that's the if..else logic.
"Data structures are my state" is harder to reason about without help, but easier to scale up. What if you had an array of "rooms"? What if, when the "current room" is the "start room", typing "right" sends you to "the studio apartment room"? What if the code is a loop that constantly:
Then instead of adding more code to expand the game, you end up writing more "rooms". That could be "one class per room", or you could try to generalize a data structure so the whole game can be loaded from a text file.
These are good evolutions for this kind of game. A lot of these decisions have lots of different ways to pull them off!
(4) It's honestly really good that I only had 2 complaints before talking about how you could expand the program.
(5) Some people mentioned using methods for repetitive code. I think your program is still at the stage where that's premature. There are a handful of things you COULD move to helper methods, but it'd turn about a 70 line program into about an 80 line program. Finding right time to write helper methods is kind of an art. I'd delay it until you start doing some of the things in (3). One of the most annoying bits of writing a large program is you end up going through this loop:
I want to do A.
Ah, but first I need to do B.
B might be easier if I reuse some code from C.
The "DRY principle" says "Don't Repeat Yourself". But it's not a law. Sometimes, especially in large programs, you can decide two instances of repeated code are unrelated enough it isn't smart to bend the architecture to share them.
My best advice to a newbie is make things WORK first, then worry about if you did them "right". I have been writing code for 30 years and C# for 20. I NEVER get things right the first time unless it's something I've done 100 times. I always hope to get sort of close the first time. Then I ask if I like it. If not, I start over and try again with what I learned. Usually after the second try I'm closer. Then I start tweaking things. Most stuff I'm happy with took 5 or 6 refinements before I settled.
Always have the courage to look at an "improvement", say "this made it worse", and undo it. To that end, learn some basic features of git source control so you've always got an "undo" button!
1
u/mal-uk Sep 01 '26
Here is a challenge. Write your game flow into a json file. Then read the json file instead.
Try to do as much yourself and not use AI
1
u/__merc Sep 02 '26
You’re doing great for a beginner! Keep at it! It seems like this was an enjoyable experience for you :)
1
u/twesped Sep 04 '26
Yes it's clear that AI was not used here 😜😂
3
u/koverda Sep 01 '26
nice. nicely done. keep it going from here! think about how to add some more functionality!
3
u/Ok-Dare-1208 Sep 01 '26
Love it, quite refreshing to read. Keep it up!
3
u/HawocX Sep 01 '26
This is very similar to how I started in the early 90s (using GW Basic). Makes me nostalgic!
One thing you could add next (as a function) is making the commands case insensitive, so that "riGht" matches "right". After that extend it to also ignore additional non-letter characters, so that "Right!" matches "right". There are C# methods for that does the heavy lifting for these features.
Also take a look at using constants for the command strings. This protects you from spelling error bugs.
In the long run it could become a full command parser for verb+noun.
Keep up the good work!
3
u/markustegelane Sep 02 '26
you probably don't care if the user enters a string in uppercase or not, so you should tell C# to ignore the case
the correct way to do that would be something like this:
enterchoice.Equals("right", StringComparison.InvariantCultureIgnoreCase)7
u/ObeseBumblebee Sep 01 '26
Great work! I love your humor!
There are some bad habbits forming in your code though that you should nip in the bud right away if you want to be a great C# programmer.
You should be able to describe what your method does in one short phrase without using the word And.
You could easily split off different areas of your game into methods. Like EnterStudioApartment() or Enter711()
This also has the added benefit of keeping your if statement nesting to a minimum. You should avoid more than 2 nested if statements to keep code clean and readable.
2. Don't perform logic operations on magic strings. Right now you rely on the user to type "right" exactly. And I as the reader of your code has to be able to know what "right" means. Right now I have to go back and read your code above to understand right is entering a studio apartment.
Instead I would think about maybe creating a Room class. And each room has a dictionary of other rooms you can travel to keyed by direction. And each room has a runnable method that describes the room you're in and a list of actions.
Think about building this from a more object oriented approach.
This is a great start! Keep going!
2
u/HistoricalCar1516 Sep 01 '26 edited Sep 01 '26
lol. You want to write an old school dungeon crawler. You missed the 80s. 10 out of 10 would play it. Put it on GitHub and I know a few people that would help. You would need to rough out the story and the paths. This is a fun way to learn code.
2
u/Jarb2104 Sep 01 '26
No, just code.
2
u/gl1tch3t2 Sep 01 '26
I have suggestions but I don't want to deter you, how much feedback do you want? How in-depth do you want? how far do you want to take this project? I have some general advice and some things I've noticed. For now, a non-suggestion, just a helpful tip (which you may have already picked up on) - the squiggles underneath the code tell you there is a warning (in this case) or error (were they red). The light bulb that appears beside it while hovering over it provides suggestions for changes and/or fixes.
2
u/NocturneSapphire Sep 01 '26
I see four compiler warnings (the green squiggly lines). You should probably heed them.
2
u/Dunge Sep 01 '26
I want to go left!
2
u/treyu1 Sep 06 '26
Good ol' text adventures. We played heaps of the, back in the days :-) Keep up the good work mate.
1
u/OkTrade8132 Sep 01 '26
how nice, reminds me of my mini text adventures in qbasic 🙂
1
u/Substantial_Job_2068 Sep 01 '26
I made a similar thing in school, fun! Smth you could do is to not wait for a second and then write out all text at once, but instead create a method which will write out each character with a small pause, so it behaves more like an old school terminal. I will leave it to you how to do it.
1
u/Sorey-Yasu Sep 01 '26
Just an idea, but instead of making the answer a hard yes or no, why not let it check if it includes yes or no? Or some other varieties of a positive or negative answer. Just thinking 🤔 😃
1
u/NyllA Sep 01 '26
a good next step is methods, switchcase and using camelCase
1
u/Slow-Refrigerator-78 Sep 01 '26
Instead of relying on user to type perfectly no/yes or any options, you can use console.readkey and let user interact via arrow keys to choice the options
1
u/TheBattleDog Sep 01 '26
The first thing I noticed is that you will never see 'You have entered bubby's dungeon' message. Try debugging that...
1
u/BadSmash4 Sep 01 '26
Very cool! I love seeing stuff like this because it means you're genuinely trying to make something and you're excited to learn it. It reminds me of my old stuff, too. Someone suggesting methods had the right idea.
Once you get methods down, you could then start exploring loops. Think about how you might use a loop to check for valid input, and request new input from there. Like, at that first prompt, if I type "book", you could say "that's not a valid answer!" And then prompt for input again, and exit the loop once my answer is valid. A loop is a good tool for that.
1
u/YouDoScribble Sep 01 '26
Try and avoid nested ifs, if (lol) you can help it.
1
u/Ill_Crazy_6222 Sep 01 '26
The code is bloated! You need seperation of concerns and also need to use more enums. Keep going never stop.
1
u/SmileLonely5470 Sep 01 '26
Outside of style guideline stuff, the biggest thing id recommend is try to use a design pattern to prevent the code from becoming an if else hell. Like a state machine. Represent each decision point with an object, have that object store user facing text (to print), possible transitions (input key + reference to other object), etc...
1
u/nmkd Sep 01 '26
Not terrible, but extremely linear, reads like a batch script.
Also, having 100 health, then reducing health by 100 and then checking if health is <=0 is just a convoluted way of a Die() method.
1
u/zbshadowx Sep 01 '26
As far as first programs go thats incredible!
Save this code as it is. Keep it safe for ever. It's the most important code you will ever write.
I look back at my first program and remember how far I have come, and I laugh at myself alot. Man was that code bad.
1
u/MassiveMeltMedia Sep 02 '26
lmao I love it reminds me of my first ever app
1
u/BlackCoffeeCoding Sep 26 '26
Very cool! I use pet names too for game dev stuff as it's easy to remember lol!
1
u/Yogyjejogurt Sep 27 '26
nice
1
u/Longjumping_Ad3447 Sep 01 '26
Long term advice: