r/learnprogramming • u/[deleted] • Feb 05 '16
C# total beginner, first time posting code. Am I on the right path with how I coded this?
Short: Please look at my c# project that solves this problem and tell me how to improve it (horrible beginner-mistakes/bad habits in the making, general setup and style...) just generally what jumps out at you?
The catch: How could I have done this better still using only the tools I've learned so far and tried to use here. For example only using arrays but not lists.
Long: Hi, I'm a coding and c# beginner following Bob Tabor's phenomenal free course on MSVA and I'm now a few enthusiastic weeks into it. I'm working with the free companion guide (by Dina and Sasha Goldshtein of shecodes academy) to Bob's course which seems to get less attention than deserved (only 6 watches/stars) but is very helpful!
The code I'm posting is my solution to the "Module 5 Lab" of the mentioned companion guide. It is an exercise for people that have finished Bob's lectures 1- 14. It analyzes data from a very large .csv file containing flight performance data for the year 2014 (flight delays and such). It is my first real-world problem beyond the "hello-world"-alikes and I'm excited!
Again, here is the problem and here is my solution.
I've worked on this code for days and days, after getting a initial working version up in maybe 4 hours (total beginner, like I said). I didn't look at the solution by shecodes before I got it to work. Sorry: I went a bit overboard on Bob's advice to use comments frequently.
Essentially, I used only the methods and variables that had been taught in the course to this point and I consider that a great exercise in itself: it teaches me how to use them (arrays) and learn about their power and limitations before I progress to new ones (e.g.lists). So it would be awesome if you could tell me how to improve this code still only using the tools I had at hand?
For debugging, I created two smaller versions of the .csv file which are also included in the project and can be switched to by changing the variable "airlinePerformanceFileName" accordingly.
This is the first time I've posted my own code and first time using github. New on reddit, too. It actually feels a little like going to the beach when you're insecure to show your body! It took my days to write, test and improve the code and I realize that my post asking for critique is probably overkill, as well. What should I have done differently here? Wrong website, wrong subreddit? go to stack overflow? gtfo? any suggestions are welcome.
Thank You!
Some more questions if you have the time:
I learned I should avoid copying and pasting code and rather use helper methods. I'm stumped as how to do that with the streamreader that I'm using 7 times for different tasks. Suggestions how to do that here or in general how to better refactor my methods?
Is it bad to call a method from within another method? I did that to avoid either making variables global (or methods public?) and to keep the Main method as free (of variables needed to call those methods) as possible.
By using only the tools I have used, is there a way to employ them more efficiently? So my code uses less resources?
Please note: All delays displayed by this program are "arrival-delays" meaning the flight(s) in question being late. I consider this the worst kind of delay as it can cause you to miss connecting flights. Flights leaving early sounds bad but in my experience they only do that if all passengers are on board.
Here is a link to the shecodes solution to their problem. However, it uses tools that weren't covered in lectures 1-14 (like lists) which I wanted to avoid for now and itcurrently also returns different results. I am still trying to find out why.
I used Visual Studio 2015 to write the code and boy is that software amazing <3. The debug functionality is like scanning your code with a Star Trek Medical Tricorder.
Again, you can test by executing, and inputting New York NY and Los Angeles CA when you're prompted for origin and destination.
Edit: formatting
2
u/svgwrk Feb 06 '16
Code review: https://youtu.be/1azd6ayEVgE
My crack at the same thing: https://youtu.be/Bfxeh-ihpZU
Code: https://gist.github.com/archer884/ce2a41c0f10f12d86fa2
1
Feb 06 '16
Just watched the 1 hour review. Mind=Blown. Thanks so much for the very helpful input. Lots of small and big things I wrote down from that video and will try to keep in mind.
It's exactly what I was looking for. Quite a bit to process for me, though - I'll watch the video again tomorrow. I made everything overly-complicated by using arrays instead of lists. Sort of like trying to build a simple hut without using hammer and nails to force yourself to think harder and come up with ideas... yeah, well... it made sense at the time. Looking forward to checking out your own shot at the program and will report back in. Awesome effort, thanks again!!
2
u/svgwrk Feb 07 '16
I'm glad you found it helpful. I know that practically everyone I've ever talked to has said that they learned more in a few months (or even weeks) on the job, working with other more experienced devs, than they learned in four years of school. I know I certainly learn a lot every damn day.
1
Feb 08 '16
I watched the "live code airports!" video, as well. Just as informative as the first on, yet admittedly a bit overwhelming as many of the concepts are yet to be covered in the course I'm taking. Also, I've never seen somebody just grab a library and start using it which is really cool. Then again, I've never seen anyone code anything other than the teacher of the course I'm taking. Some minor things I was able to pick up on by just watching but for most I'll have to return once I've acquainted myself with said concepts. Damn, no wonder you scoffed at my overuse of arrays, these other types and methods are a LOT more powerful.
2
u/svgwrk Feb 08 '16
The cool thing about C# (and other strongly typed languages in general, provided that you have autocompletion in your IDE) is that the type system allows you to just grab a library someplace and start using it without spending a lot of time trying to understand what's going on.
So, for example, what's
public IEnumerable<Chicken> GetChickens()do? It returns an enumerable of chickens, of course--the method signature itself tells us that. :)Feel free to ask whatever, and I apologize for giving the impression that I was scoffing at you doing anything; it's more that I don't like when classes artificially kind of shoehorn students into doing things that they would never do in the real world. I'm not a teacher and maybe they have good reasons for doing things like that, but it still bugs me.
2
u/svgwrk Feb 05 '16
I'll make a note to look at the code in greater detail when I get home, but for right now I'll answer the questions you asked:
It's standard practice not to use
StreamReaderfor this at all, so don't I wouldn't worry about it too much; the long and short of it is that you can write a method to do what you're doing, but someone else already did that and you should be using theirs.No. All code works this way.
For this one I'll have to do a more in depth review.