r/programming • • Mar 12 '14

Implementing a web server in a single printf() call

http://tinyhack.com/2014/03/12/implementing-a-web-server-in-a-single-printf-call/
582 Upvotes

123 comments sorted by

View all comments

Show parent comments

9

u/Delta-62 Mar 13 '14

Oh wow, thanks! Have some gold for the detailed explanation. From your personal experience, what learning resources would you recommend for learning C/C++?

14

u/Tynach Mar 13 '14

I'm still a student myself, and I've never 'worked in the industry'. This may be crap advice, but I'll go ahead and tell you what finally kicked what I do know into my head:

Don't try to learn individual syntax first, try to learn concepts and ideas. Instead of figuring out "How do I do <x>", instead learn, "What individual features are available, and what are they actually used for?" Essentially, find out what is possible with the language, and learn the situations that are appropriate for that facet of the language.

You can become an expert at syntax and grammar and still not know what to do or how to do things. You can even memorize the entire standard library functions and still be in the dark about how to properly use things.

Finding out what other people can do with the language, finding out HOW they do that with the language, and then double checking the things they did with others and figuring out whether what they did was appropriate or not, is MUCH more useful than learning syntax and built-in functions.

Some people recommend you do this by reading other people's code. Open up some open source software repositories, fork some Git repos, and just study up on other people's code.

That never worked for me. Especially at first, but still today, I suck at reading other people's code. For me, however, it helped to watch people program, and talk out loud as they programmed.

This is why I was stuck at making infinite loop silly terminal programs up until I actually took programming courses in college - because then, my instructors (especially a very particular one) would actually write out the code on the projector, run it, explain the thought process (often while typing it), find out it doesn't work, debug, modify, try again, etc... And document, verbally, every step.

For me especially, this also showed what the languages being taught were capable of and what they held within them. I mean, I could read about different functions and language features all day, but until I actually have someone use them in a real project while rambling out loud their thought process, I don't really understand their purpose at all.

Oh yeah, that instructor I rambled on about? He didn't do this. He came to class with program files already pre-built, and he went through and described what every single line did individually. Yeah, ok, sure, I could easily look that up online. But why did you use it like that?


So, anyway... The other thing that really helped me, was learning a 'language feature' in a different language that revolved around it. For me, this was object oriented programming and Java. Before I took a Java class (heh), I knew how to make classes and structs in C++, Python, and other languages... But I didn't know why so many people went crazy over it.

I really don't like Java. But man did taking a Java class help me understand and appreciate object oriented programming. And I'm pretty sure that there are other things to be learned in other languages in a similar fashion - but this is the only example I've had direct experience with.

Except, well, I didn't understand programming in general (except syntax and functions and all that crap that did me no good) until I took a PHP class. First class with that specific favorite instructor. Lots of people will hate on PHP, and maybe I'm biased a bit, but when you only finally 'get' programming after using what some claim to be the shittiest modern language, you start to think it's not a shitty language.


To answer your actual question:

Take classes, and try to find good teachers. I've not tried it personally, but there might be some good video tutorials on Youtube or thereabouts - if you can find some where the instructor in them is 'programming live' (didn't come in with pre-built code that he more or less re-types, knowing it already works fine), that's best.

Look at other people's code. Browse Github, try Google+ (I met some GREAT programmers - local too - on Google+. Your mileage may vary though; was through a sheer coincidence) and other social networking sites, and also...

Just start coding. With C++ especially, use header files to 'outline' your program and figure out how all the pieces fit together, and then use the .cpp/.cxx/whatever files to actually implement each thing. Post your early projects on Github, and go on here (/r/learnprogramming is good) and IRC (immediate feedback) to ask for help/advice/opinions.

Do not be afraid of criticism! Unless there are two other people battling over whether something is actually bad or not, assume that the person is right and that your code sucks. Ask them for advice on what they would do if they were you.

If they go off on a tangent and talk about complete redesign of the architecture, perhaps don't exactly do what they say, but still listen to what they say. Such tips may not help with your particular questions, but they still might come in handy later with more 'real' projects you do later.

Though really, if they just say it sucks and don't want to give a reason, don't give up. Maybe your code sucks, maybe they're a troll, or maybe they suck at coding and only think you suck - and nobody else is on to put them in their place. Or they're an elitist asshole, in which case, well, try to sound friendly and reverent, even though they don't deserve it. Maybe they'll give a few genuinely good pointers. Or reveal their idiocy. Either way, fun will be had on your part.

...

I type too much.

1

u/[deleted] Mar 13 '14

So I have "worked in the industry" for about 2.5 years now. I fully endorse this advice. It is wonderful.

6

u/[deleted] Mar 13 '14

SICP. And then learn C properly before C++.

4

u/bstamour Mar 13 '14

It's 2014. There's no reason to learn C before C++. They're different languages with different idioms and best practices that happen to share a similar syntax. Would you advise someone to learn C before JavaScript?

3

u/[deleted] Mar 13 '14

There's no reason to learn C before C++.

Of course there is. The reason is to understand low-level programming without burdening the learner with OOP, templates and all that shit that is not necessary when trying to understand low-level programming.

They're different languages with different idioms and best practices that happen to share a similar syntax.

C is almost a subset of C++, so you must learn 99% of C anyway if you want to learn C++. There is no reason to try take in all of C++ at once. Starting from C makes more sense than trying to start from OOP or templates.

Would you advise someone to learn C before JavaScript?

Yes.

1

u/bstamour Mar 13 '14

Of course there is. The reason is to understand low-level programming without burdening the learner with OOP, templates and all that shit that is not necessary when trying to understand low-level programming.

Okay I'll give you this point here: if you're interested in low-level C-style programming (I say C-style as, as you know, there are many ways one can program in C++) then one can learn C first, and then adapt to the additional nuances of C++, such as its stronger type system, new instead of malloc, etc. However for the majority of applications, I would argue that starting at the higher level is more beneficial. std::vector is just as efficient as a dynamic array built from malloc, realloc, et al for 99% of use cases. And when it's not suitable, you can swap the allocator. And when that won't work, you can roll your own.

C is almost a subset of C++, so you must learn 99% of C anyway if you want to learn C++. There is no reason to try take in all of C++ at once. Starting from C makes more sense than trying to start from OOP or templates.

This was true in the 80's, but ISO standard C++ and ISO standard C are more like siblings (both have ANSI C as a parent) then subset/supersets of one another. There are plenty of examples of idiomatic C that are not valid C++ and vice-versa. Even the simple statement

int *ptr = malloc(N * sizeof(int));

is invalid C++, because C++'s type system disallows implicit casts from void pointers. Furthermore, C++ supports type deduction through auto, (which still has its old meaning in C), while C supports runtime-sized arrays that change the semantics of sizeof, variadic macros etc. The languages are growing further apart with each standardization.

Further on this note: idiomatic, or modern C++ is all about templates, exceptions, etc. and less about inheritance, raw memory, etc. You can learn C first, but you will have to un-learn many standard techniques because they lead to unsafe and antiquated C++.

If you want to use C, then learn C. If you want to use C++, then learn C++. One is not a prerequisite of the other.

1

u/misplaced_my_pants Mar 14 '14

Check out CS50 on edx. It uses C.