ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Sorry for asking the standard "which language first?" question, but I have a slightly different slant. Also, I apologize for the long post, but I figure the more specific I can be, the easier it will be for others to provide feedback - and avoid another language war ;-)
I have some experience programming - I've used PHP to develop some basic database driven websites and modify Wordpress, worked through one of those "Java in 5 second books," and have hacked around a bit with Python and C. So I understand the basic concepts of OO programming, for/while loops, if / then statements, etc. My knowledge lacks in the areas of data structures, writing efficient code, and more complex topics.
I now want to "master" a language, both for practical development purposes and to establish a firm grasp of programming. I am specifically interested in server-side / web, natural language processing, AI, and data (economic and financial) analysis.
Python appears to be a great choice for this, though I know with AI and data analysis there can be some massive number crunching that may require dropping down into C.
My gut is to start with Python, but many of the good programmers I know learned C / C++ first. My sense is that learning the lower level language first gave them a deeper understanding of what was going on "underneath the hood" and therefore when they moved to higher level languages, they picked them up extremely quick and were able to write more efficient and eloquent code.
On the other hand, I may be able to learn these same things in Python without having to worry about memory management, buffer overflows, etc.. Then, when I need to learn C, I would do so already having a good handle on some of the topics I am currently weak on.
My gut is to start with Python, but many of the good programmers I know learned C / C++ first. My sense is that learning the lower level language first gave them a deeper understanding of what was going on "underneath the hood" and therefore when they moved to higher level languages, they picked them up extremely quick and were able to write more efficient and eloquent code.
IMO, you are right on with this idea. C++ was the first programming language I learned (aside from VB in high school) and I do believe that knowing c and c++ gives you a tremendous advantage when trying to learn a new language and how to write efficiently. When trying to do data structures, Object Oriented programming languages are the way to go(aka C++ and java). Java is a bit easier to learn, but I still prefer C++.
I think C is wonderful, and I'd encourage any developer on any platform to learn and use good, old fashioned C.
But that's not really your question.
The real answer to the question "Python or C++ for OO for a semi-newbie?" is:
Quote:
It depends.
It depends:
a) on the task (the right tool for the right job)
b) your personal inclinations
But all things being equal ... between C++ and Python ... I'd vote for Python. Hands down. It's cleaner, it's arguably a *lot* easier, and, arguably, the resulting Python code is almost always a lot better than the C++ equivalent.
I really don't think it matters which language you learn first; in another thread someone posted that it takes a week to learn the fundamentals of a new language and I absolutely agree. I have had to learn new languages just to support ongoing work - no one is going to spend the time to translate 20+ years of work into your favorite language.
What *is* important is that you understand the concepts used in programming. I'd say ideally you should learn a little about the theory of computation and if you can find an ancient book on the subject you'll have less than 200 pages to read though - but that would be some of the most basic and most important stuff and would not have changed at all over the years. Then I think it's good to have some idea of how those concepts are put to use in hardware. For this part I think Don Knuth's series "The Art of Computer Programming" does a great job. If you can read through the entire series and do all the exercises you'll be ready to tackle most jobs. Of course the series doesn't cover *everything* - that's just not possible - so as far as I know there are no sections on graphical rendering and such.
The main thing is that simply knowing the keywords and syntax of a programming language does not make you a good programmer; there's an awful lot more to learn. At least when you're starting out it's not unusual to write a lot of code and in the future decide it was all garbage and throw it all away and start again. As you improve your skills you'll find less reason to throw away your work. I'm still happily throwing away other people's work though - mostly the work of people who think that learning the keywords and syntax make them a programmer - there's an oversupply of such people I'm afraid, and they produce the most awful code imaginable especially as they're influenced by the 'book du jour' of programming and haven't even got a consistent style.
I'd suggest learning C, then python, then C++: that's the path of increasing conceptual complexity.
Writing good C is tricky at first, but the language has fewer moving parts than python. Notable exceptions are unions, the keyword 'static', and the explicit use of pointers. In turn, python have fewer moving parts than C++.
To learn C, use "The C programming language" by Kernighan and Ritchie [or find a tutorial on the web]. Do the exercises you find interesting; enough that you feel confident you got a good grasp of the language. Get used to passing (pointers to) structs around to functions. Get used to function pointers. Learn how to use qsort and bsearch correctly. learn about binary search trees (they're in the book), and splay trees*. implement a splay tree data structure, with a user-specified comparison (hint: function pointers).
(* the book talks about self-balancing binary search trees. Splay trees are the simplest to implement; alternatives are red-black trees [they're a bitch to get right] and AVL trees [less so]. A downside is that splay trees are a little slower in practice, and the theoretical speed equivalence is only amortized).
Then move on to python. Read "how to think like a computer scientist: learning with python". A lot of stuff should be familiar from C; a little bit should be new. Passing objects around explicitly should be familiar, as should passing around functions. Learn how inheritance, operator overloading, exceptions and modules work.
Then move on to C++. Operator overloading and exceptions works similarly, namespaces work roughly similar to modules. Inheritance requires judicious use of the keyword "virtual" to work properly, but is otherwise similarly. References are going to be new, as are templates and access rules (public/protected/private). Adapt the splay tree from C to store objects of a template type, and implement all the methods of std::set. Make good use of public and private members; use nested classes. Learn about smart pointers, and in particular reference counting ones; it'll make object deallocation much easier.
A good exercise is to write object-oriented code in C, with pointers to vtables and superclass instances as struct members (and optionally pointers). It'll give you a good sense of how C++ works under the covers.
Performance hints: if disk access or network bandwidth are your major performance limiters, and/or n is small, it's safe to go with python. If you start writing things in python and discover a performance bottleneck, you can write python modules in C. That may or may not be good enough. Measure first
When C++ tools first became commercially available, at least on Windows, most of us had already learned C. We were told then that it would be easier to learn C++ because after all we had a C background. We were also told that learning C++ would be difficult, because we had a function-call mindset. Both observations were correct.
Thinking about objects, for me, took a complete virtual 90-degree rotation of my mind. I had programmed firmware using Bliss and VAX Assembler, not the stuff used to create objects, and worked on networking software, not necessarily the first area you would think of to implement C++.
This is what I would do:
If you have to learn one of these languages, because of a need, school project, work, etc, learn that. Else, learn C++ first. Don't learn it as a "better C"; that was a rubbish reason given to us to encourage moving to C++; a lot of developers did not want to use it way back when.
Edit
I suggest learning C++ first to get the object mindset strongly in the way you do things.
End Edit
Regardless of the language you use, learn what you need to use as a tool, how its implementation will affect execution speed, and how long you expect the code to be running.
I have worked on public sector database projects for nearly five years now. Most of the time Informix's 4GL code is fast enough. However, when I chose to add an interactive link from a 4GL application over to an address verification application whose link was http protocol, I implemented it in C.
Last edited by cmnorton; 11-22-2008 at 07:04 PM.
Reason: add more info
My gut is to start with Python, but many of the good programmers I know learned C / C++ first. My sense is that learning the lower level language first gave them a deeper understanding of what was going on "underneath the hood" and therefore when they moved to higher level languages, they picked them up extremely quick and were able to write more efficient and eloquent code.
That would all depend on the learning materials used. If you take "Teach Yourself C/C++ in 24 hours", you may actually learn more from a good Python manual.
Also I would not take it for granted that learning C/C++ makes your more efficient and eloquent in other languages. I took C++ after Java and Python and looking back on it, I can say with confidence that it did not teach me all that much that was of practical value for coding in Java/Python (which is not to say C++ is not powerful, on the contrary - it is just that most of what makes it powerful does not (easily) translate into another language).
I would suggest to learn both Python and C/C++ in parallel.
And among scripting languages Python is not the cleanest one conceptually. And maybe not the most efficient.
Ruby would be cleaner; Lua simpler and maybe cleaner.
As I said in a different thread, Python lacks proper scoping and closures, and this makes it a non-starter for me.
C/C++ at least have proper scoping, and GNU C even has kind of closures - one can declare and implement a function inside another function.
I stick with Perl - and maybe they'll finally implement Perl6 - that beast is _really_ interesting because of a lot of paradigms and features to be present.
I take the old-fashioned view: Learn first the simplest, most powerful, and least forgiving, before learning things that either add layers of difficulty and obfuscation, or attempt to make things easier. To me, this means learn C first.
Python makes some stuff so easy that you might never learn to worry about data types, etc.
Having first struggled to learn C, I can't imagine piling on the OOP layer to make C++, and trying to learn it all at once.
I take the old-fashioned view: Learn first the simplest, most powerful, and least forgiving, before learning things that either add layers of difficulty and obfuscation, or attempt to make things easier. To me, this means learn C first.
Python makes some stuff so easy that you might never learn to worry about data types, etc.
Having first struggled to learn C, I can't imagine piling on the OOP layer to make C++, and trying to learn it all at once.
On the one hand I agree with you.
On the other, knowing just C forces you sometimes to write complex programs for problems easily solved by a scripting language, especially a high level one like Python/Perl.
I.e. one must know elastic arrays and hashes exist, but one must also know the cost of implementing them, and looking into a C library which does the implementation is quite revealing.
I am not a programmer by education, but in the last 20 years I've doing mostly code, even it was HW design.
Just to clear up some confusion, I wasn't considering C++. If I am going to do OO, I'd probably go with Python and down the road, Java (given my small experience with Java). Hence why I am looking at Python and C.
Also, I don't think i have time to learn multiple languages in parallel - I am leading a serial life ;-)
To throw in another loop, I have been told for what I would like to eventually do - AI, natural language processing, data analysis, and some web / network stuff (mainly to pull down data and send back machine or human readable responses) maybe I should go with Java and forget the rest given its speed, relative easy compared to C++, available libraries, and a good teaching language.
On the other hand, I think it is generally agreed that Python (or Ruby) is usually quicker to program with vs. Java, C++, or C, easier to do more difficult tasks, and you can always write modules in C if there is a processing bottleneck.
Let me pose a different question: do I lose much by starting with Python, and then taking up C when I start to run into performance issues that require me to write some C modules?
do I lose much by starting with Python, and then taking up C when I start to run into performance issues that require me to write some C modules?
Absolutely not. In fact, regarding cmnorton's post above (which I completely agree with) (except for the "learning C++" part ;-)), learning python first (and consequently not only getting up to speed with useful projects quicker, but also becoming comfortable with OO concepts at the same time) is probably the ideal solution.
It sounds like a great plan - go for it! And have fun!
IMHO .. PSM
PS:
Again, I definitely encourage you to learn C when the opportunity presents itself. And, as cmnorton observed, if you ever want to learn C++, do *not* approach it as a "better C". You'd be much better served learning C++ strictly from an OO perspective, and learning C by itself (on its own terms, as a very powerful - but still procedural - language).
If as your original post implied you wanted "to establish a firm grasp of programming". Then I would suggest C, with the following caveat: Focus on data structures, you already have a grounding in programming so picking up the basics of C should be fairly easy (although you will need to spend time understanding pointers & memory management). Grab a copy of the O'Reily Algorithms with C [Loudon, Kyle], the first dozen chapters cover pointers, recursion and data structures and the rest of the book look at the classic algorithms.
Understand how the structures work and when to use them. Then when you decide to use Python (or Ruby) where these data structures are part of the language will know which is best to use, and why.
To throw in another loop, I have been told for what I would like to eventually do - AI, natural language processing, data analysis, and some web / network stuff (mainly to pull down data and send back machine or human readable responses) maybe I should go with Java and forget the rest given its speed, relative easy compared to C++, available libraries, and a good teaching language.
This is mainly to satisfy my own curiosity, but with regards to data analysis, will it be strictly necessary for you to use a "regular" language (e.g. Python, C++, Java, etc) as opposed to a language/environment made for data analysis (e.g. MATLAB, IDL, R, etc)? I'm just wondering if you had considered those already or not really .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.