Ask HN: What should I learn as my first programming language?

26 points by FalconNine ↗ HN

97 comments

[ 2.9 ms ] story [ 177 ms ] thread
What do you think you will enjoy doing? Web pages? Android apps? iOS apps? Pick something fun then choose the right language.
I'm not doing this for work or school or anything, just for fun. I want to make small games as little pet projects, not aiming to make money or anything, just to have a hobby.
* Want to make webpages ? Learn JavaScript, HTML and CSS.

* Android apps ? Learn Java.

* iOS apps ? Learn Swift.

* No idea what you want to do, but strong desire to learn to code ? Learn Python. You could use Python for general purpose programming, small games and so on ...

How much easier will new languages be after I've learned Python, assuming that's what I go with?
Once you learn a programming language, the second one is usually much easier to learn.
(comment deleted)
Swift. Create mobile apps instead of websites.
Why mobile over websites? Just curious, because I'm also a newcomer that is debating web vs. mobile.
I would say Python - it's a great general purpose language of which many interesting things can be built.
Python is a good choice. I would argue that C, followed by C++, might have more applicability later on; a lot of languages are modeled on one of those two. They also are lower level and so give you a more in-depth understanding of how memory allocation works.
Someone show Ruby some love:)

Learn Ruby.

Its a beautiful language well worth the effort and it has a HUGE ecosystem behind it. You'll never lack for resources. It is powerful and I found it doable to learn it all on my own.

It will aid in the transition to making dynamic websites because it is used in Rails.

In the early part of my learning I found that the break in compatibility between python 2 and 3 was irritating and confusing to me. Some of the good books were on python 2 and I had been informed the world was moving on to 3. Ruby has major versions (1.9 vs 2) but no break.(I started out by trying to learn python but switched midway to ruby)

The only caveat is if you want to do math heavy stuff or machine learning - python excels at that.

Python is frequently recommended by people with a CS / computational background. I did not have that.

Some people do not approve and I understand. But this was my experience. YMMV.

Ruby is a terrible first language. Too many ways to do things and looking at other peoples example code will only confuse people new to programming. Also too easy to fall into bad habits.
You could go old school and start with Pascal. It's a great learning experience. And isolated from the real world to some extent, which at the start is a good idea (there isn't 300 frameworks staring back at you).
Agreed. Pascal has several advantages as a first language. The original Pascal (oP) language is small and quick to learn. ISO Pascal (iP) adds objects to oP, so the transition from oP to iP invites a graceful transition from procedural to object oriented thinking.

As a type safe language, Pascal also introduces concepts of data placement and memory management early on, and supports dynamic allocation and pointer access clearly and painlessly.

Learning C, C++, or Python after Pascal should be quick and easy, since they're just variants and generalizations on the primitives learned from oP and iP.

If you have absolutely no experience programming, start with Python. There are three things to learn when programming: 1) language syntax, 2) concepts (conditionals, logic gates, loops, etc.), and 3) how to solve problems.

What makes python a good choice is that you don't have to spend too much overhead learning language syntax in order to implement concepts, and you'll easily be able to keep the learning of those two items separate.

Once you have a strong foundation in concepts, you can much more easily pick up a different language. The biggest downfall there is that if you decide to later learn Ruby or JavaScript, you might approach a solution as, "This is how I would do it in Python." Don't fall into that trap. Learn the features each language provides and how to use them.

Since you mentioned games, there's a couple of books on the topic that are free: http://inventwithpython.com/

P.H.P
Stop trolling the OP. How would you feel if he believed you and actually picked up PHP?
Stop trolling PHP developers. How would you feel if your darling language received this kind of review?

Honestly, PHP is a fine language to begin learning, it's very easy to get a local development server going for someone who barely knows the CLI, it's integrated tightly with HTML (for better or worse) which make things simple before you move to patterns like MVC, etc.

You can build some pretty cool, performant, beautiful software with PHP - and there are always well paying jobs out there (especially if you're not in the valley where Ruby & Python are disproportionately more popular than elsewhere.)

PHP 7 with a new, (loose) type system and a lot of the old, insecure cruft removed isn't actually a bad language, especially if your build process involves Composer and you're following the standards laid out by the PHP FIG/PSR.

Well of course I played to the negative stereotype as a joke : ) Obviously reality is not that simple, but to briefly sum up my personal take on it, yes good PHP may be good, even beatiful, but in practice it's rare to come by. One of the reasons being the prevailing culture of duct tape mentality that seems to surround it. I'm speaking from my subjective experience, YMMV.

> How would you feel if your darling language received this kind of review?

I'm not that attached to my favourite languages, but the thing is - they just don't receive it for some reason : ) It's probably correlated with why they're my favourites. I don't have overly fancy preferences, I like C# (it is getting its share of hate, but hardly ever as a language in and of itself, it's typically directed at Microsoft and that's another story) and Kotlin (sort of like Swift but for Android).

Modern PHP's not actually that bad; most of the language's reputation comes from much older (and much worse) versions.

And it is really good at providing server-side programming to a web site with the utter minimum of work --- after all, that's why it's used so much! No compilation, no deployment, not even any need to have two files open at once; just stick a couple of lines of PHP into an HTML file, hit reload, and you're there.

I wouldn't call it a particularly interesting language, but if the OP reckons they'd be motivated by building web apps, it's a perfectly reasonable choice.

It has not improved much. It has added some superficial things. But I am afraid that the worst part of the language is still very much alive.

>No compilation, no deployment, not even any need to have two files open at once; just stick a couple of lines of PHP into an HTML file, hit reload, and you're there.

>And it is really good at providing server-side programming to a web site with the utter minimum of work

This is exactly the problem with the language. It lets you cuts corners when those corners are there for a reason. For example, look at the ease of deployment that you have mentioned. For a novice, this will be appealing. But for someone with experience with web applications, it might look appalling that you can map urls directly to execute arbitrary files in a directory.

This theme is found throughout the language. In an attempt to make work easier for novices and to make it easy to work with data send from html forms, the language has been packed with so much weird behaviors that you need to learn before you can build stuff in php with any sort of confidence.

And that is just the tip of the iceberg....

> just stick a couple of lines of PHP into an HTML file, hit reload, and you're there

This ease of use and low entry threshold is a double-edged sword though

I always recommend Lua as a first language. It's about as straightforward to learn as Python, but it's smaller and simpler, with fewer "gotchas". Once you've learned Lua, moving to Python is about as easy as moving to Javascript.

I don't recommend Python or Javascript because while having a rich set of third party modules is great for actual development, it's a detriment and distraction for beginners. When you're learning the nuts and bolts of programming, it's easy to be misled by how easy some common task is to perform via API call and think you understand it when you haven't really been exposed to it. This becomes a big problem when moving to some other language, where library functions work in a different but equally straightforward way. When it comes to things outside the core standard library, you should really learn to implement them yourself before becoming accustomed to using third party libraries.

Python is a great second language, but the way you write things for a beginner programmer is very different from the way you write things when you're trying to be clever and squeeze a lot of behavior out of a few characters or a few cycles, and python has too much of a mixture: searching for information on how to solve a problem in python will get you equal numbers of people who don't know how a for loop works and people who prefer to do everything by passing lambdas around.

Lua is not a bad choice: the docs are good and once you get the basics down https://love2d.org/ is great for writing Tetris and having having something you can point to and go "I did that"
I don't know that hiding complex ideas is necessarily helpful for a beginner. Admitting "by the way we're writing 'public static void' every time for a reason, but we won't worry about it yet" or "yes technically you could complete this assignment on one line, but trying to learn that right now is not useful" don't seem like bad ways of confronting the idea that programmers don't always know everything and that's okay. Keeping them completely insulated from those kinds of things could make them seem scarier than they really are.

Of course learning is complex and the "best" way totally depends on who is learning and their goals.

I disagree with some of these points. Specifically, programmers should necessarily be left unable to understand everything they use. Packaging systems are excellent for providing you with functionality without the mental cost of having to understand how that functionality works. They're not a distraction, they're how people get things done.

And Lua is not a great first language. If you know how to use it, it's a great language; but its 1-indexing, tiny standard library, inability to distinguish between hashmaps and arrays, confusing "metatables", and lack of external libraries really make it tough for me to recommend as a first lang.

This comes from someone who used LuaJIT often with Love2D to prototype games.

Anything that's trivial to write (think "left pad" and 90% of what's in NPM & PIP) shouldn't be used by people who can't conceive of how those things might be written, if their goal is to learn to write things. I'm not suggesting that beginners should start with writing bootloaders in machine code or anything; I'm suggesting that beginners should master things like loops and functions and variables before they start plugging together big chunks of other people's code, lest they overestimate their abilities and take a major blow to their self-esteem when they realize that they are completely incapable of doing simple things in another environment because they never really learned how to do things. Some of the biggest and most important hurdles to clear when learning to code are related to learning to master things you've never considered; convenient API calls are convenient to avoid considering. In this way, a tiny standard library is useful: all of the things in Lua's standard library are things that a beginner can afford to put off learning for a little while.

Lua has a handful of strange attributes that distinguish it from most programming languages -- 1-indexing, lack of a distinction between integer and fractional numeric types, lack of a distinction between integer-keyed and non-integer-keyed arrays (and lack of distinction between arrays and objects), and the use of tilde rather than exclamation point in comparison. The number of idiosyncracies is comparable to those in python and javascript. I don't think that distinguishing between arrays and objects is useful for a beginner, nor do I think that the fact that in most languages integer-keyed arrays are special for optimization reasons is terribly important for a beginner. And, metatables are an advanced feature -- they are similar to mechanisms in python and C++ and aren't all that weird, but they won't even come up until long after our hypothetical student is ready for their second and third language.

A first language is for learning. Whether or not it's useful for real work is totally irrelevant; someone who doesn't know ten languages inside-out isn't qualified for real work in any of them. Lua is a good candidate because although it can be used for real work, it's not optimized for that. Instead, it's a simple language that combines ideas that have historically been separated for reasons related to ease-of-implementation and separates ideas that have historically been combined to make ugly hacks possible. In other words, it's a fairly internally coherent and conceptually pure language that is easy to pick up because it lacks "gotchas" (except to people who come from other languages, and are offended by Lua's improvements).

90% of what's in npm or pip is not trivial to write. Calling database adapters or HTTP libraries "distractions" is silly. And in the same sense, restricting yourself to a subset of a language that makes it easily ported and contained within any application seems... silly.

Furthermore, plugging together pieces of other people's code can very much help in learning how to express concepts in the language. You can look at existing examples of how it's done, and do it yourself. You make small changes, then big changes, then write your own. Easy peasy.

Lua is a weird language for many reasons. Not that being weird is a bad thing, but that makes it difficult for people to learn other languages. If you learn a C-style language, you'll be more able to read other C-style languages.

And no, metatables are integral to Lua. You can't use prototypical inheritance without them, and they're really difficult to wrap your mind around. Furthermore, prototypical inheritance is very useful. If you've actually learned Lua, you've got to learn about that aspect of metatables.

I love Lua for real work. Honestly, actually do. But I don't love Lua for learning.

You need metatables for rolling your own OO system. But, object orientation (particularly when, as in Lua, you need to roll your own) should not be part of a beginner curriculum. Like I said, by the time you need to worry about metatables, you should have already moved on to your second and third languages -- you can learn about OO in the context of Python or JavaScript and then later apply that to Lua.
(comment deleted)
C, on a Unix system like Linux or BSD. It'll make you learn how your computer works, which is an essential skill to have and will help you understand everything else you learn.
That's how it is done in universities. That's how I started. My professor even did not allow compilers which can specify errors. We were supposed to find out errors on our own. And you know, I had to learn vim first to be able to write anything. He just wanted to make it impossible to have any aid. The recommended book was by Ritchie and Kernighan.

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Eb...

I enjoy those days and happy that I scored A in his class. It has given me confidence for sure.

GoLang and JavaScript are pretty important areas to know these days and into the future. Node is still big and will continue to grow on embedded devices I'd hazard. I would also recommend learning Python and if you have time swift (native apps!).
i'd try Go and Elm, although you'd encounter some JS and HTML a little bit with Elm.
Erlang
> Erlang

Genuinely curious. Why Erlang?

(comment deleted)
I think a case could be made based on the relatively straightforward syntax (very few gotchas).

I mean, most functions will look like:

  f(X) when is_list(X) ->
    firstExpression,  % commas separate a sequence of expressions
    secondExpression; % semicolons tell us an alternate sequence follows
  f(X) when is_integer(X) ->
    thirdExpression. % periods terminate the block
Case expressions (similar to switch/case in other languages, cond in various lisps):

  case Expression of
    Case1 when SomeGuard -> Expression1;
    Case2 when SomeOtherGuard -> Expression2;
    _ -> DefaultExpression
  end.
Receive expressions:

  receive
    Case1 when SomeGuard -> Expression1;
    Case2 when SomeOtherGuard -> Expression2;
    Case 3 -> Expression3
  after 1000
    TimeoutExpression % handle timing out
  end.
Note how they're all following the same form (this is also a selling point of python, its syntax is pretty consistent, especially for the features used by beginners).

The conflation of strings with lists and characters with integers can be problematic, as sometimes a list of integers will display as a string if they fall in the printable character range (and this isn't what you want). This creates confusion, but it's surmountable.

Variables don't vary in erlang, but if a new user isn't accustomed to doing:

  x = y
  x = x * x
  x = x + 4
  x = sqrt(x)
then they won't have too big a problem doing:

  X1 = Y,
  X2 = X1 * X1,
  x3 = X2 + 4,
  X4 = sqrt(X4).
It is more cumbersome, but it's also likely that those intermediate variables could have more descriptive names.

The concurrency model is fantastic, and will, IMO, be greatly beneficial to anyone new to programming. Not because it's available in every other language (without libraries it likely isn't), but because it gives a really good mental model for concurrency that can be mapped to other existent concurrency implementations in other languages. It also helps to encourage a design based on loose coupling and high cohesion, which many (most?) of us believe are good design principles.

Syntactic sugar around list comprehensions, maps, binary comprehensions make a lot of algorithms very concise (though perhaps not performant, but this is effective for a first pass implementation when you're aiming for correctness and not speed).

The library ecosystem was less comprehensive last I checked (several years ago now), which made integrating some useful libraries very challenging. But I believe this has changed, and the Elixir project has certainly helped a great deal here as well.

The performance of numerical code hasn't been good. And a lot of people mistake concurrency for parallelism expecting performance boosts that just won't happen. Concurrency is largely a design/communication principle, not a performance one (though it can and does aid performance). So those are areas for concern.

There's probably more pros and cons, but I've got to go now.

Great case you make there! I'm off to look into Erlang :)
HTML, CSS and JavaScript was what I first learnt almost 20 years ago and I still think it is a great path to beginners
I don't mean to sound negative - but the HTML stack is the worst thing on earth to start leaning to program in.

HTML, CSS are not 'programming', and both they and javascript are a convoluted, dysfunctional mess.

Yes - they are easy - but they are all super quirky and those quirks form bad habits.

Python is a great place to learn basic algorithms and syntax. Java is a great way to learn types and basic OO. C is a good place to learn how to deal with lower-level things, but it can be challenging for beginners.

Javascript should be a great place for beginners - but it's full of so many oddities, that I think starters will be misled. Every JS environment is different, and it's hard to tell what works where. It's also managed by consensus and varies a lot.

Yep, I don't disagree with you. I gave a practical approach, which worked pretty well for me, I think. I also think that the native web stack (HTML, CSS and javascript) will keep growing until it becomes ubiquitous. But as it is a personal bet, I don't always use it as an argument.
'web stack' is a natural entry point for newcomers because 'they can do stuff', i.e. make web-pages that do things they can touch and feel. So it makes sense on some levels ...

But for programming proper ... no way. I wish that were not the case.

pascal or haskell
pascal or haskell
Mods, please add the "Ask HN" prefix to this one. I thought this was an article on some other site.
Don't learn a programming language. Learn how to accomplish projects.

Pick a project, whether it's a blog or an automated tea brewer or 3D model viewer or something... and then pick a language that suits your needs.

So many beginners fall into this trap of learning just for the sake of learning; I've found that people with projects and with clear goals make the most progress.

If you want to learn a programming language, read any of the other comments. (I agree with Python being first on the list.) But if you want to learn to program, don't fit yourself into a category just yet. Be pragmatic and use whatever's best for the job.

This is really great advice that jibes with my own experience. A small thing I would add is that when you do this, the following main goals are possible:

1) To finish the project.

2) To learn a language.

3) To finish the project, but also learn a language.

4) To learn a language, but also finish the project.

My $0.02 is that: 1) tends to lead to short term hacks; 2) and 4) tend to lead to dwindling motivation; 3) is ideal. And if it's your first language, do the project while you read a good generic programming book like 'code complete'.

I've long thought that learning an imperative language first hindered my comfort level with FP. I felt like learning a functional language first would prepare me better for learning anything else.

So, maybe Elm or Racket?

There are a lot of great "visual" programming tools you can play with, often right in your browser. Typically designed for kids (Aged 6 to 106)!

MIT Scratch: Create shareable stories, games and animations

https://scratch.mit.edu/

Minecraft: Hour of Code

https://code.org/mc

Processing: flexible software sketchbook for learning how to code within the context of the visual arts

https://processing.org/

Google Blocky: the library for building visual programming editors

https://developers.google.com/blockly/

I learned C when I was 11, an uncle gave me a book as dense as a dictionary covering all the aspects of the language, as well as algorithms. He called it "The Bible".

For 1 year I woke up at 5:00am every saturday and sunday, to use the only computer we had at home before other people were up so I could do every single exercise of the book (there was about 20 per chapter, 10 first trivial, and the rest were small projects of increasing difficulty).

I consider that this experience is what made me the best programming student even at college, and an excellent developer when in the workplace. The lessons I learned from C are still priceless even though I mainly develop in managed and OOP languages today.

This will teach you basics that will never fade, there're certain Google searches you will never make again.

The important thing when you start is to know how a computer works, and how lines of code are interpreted and executed by the computer, as well as how to express and solve real world problems with computer instructions. You don't need to know every detail, but the less magic it feels, the better.

It might be worth pointing out that your ability to do this (learn C from a book at 11 years old) is at least uncommon, if not extremely rare. This may have worked very well for you but is likely to be difficult, discouraging, or down right impossible for most other people.
Forgot the most important part :

>The important thing when you start is to know how a computer works, and how lines of code are interpreted and executed by the computer, as well as how to express and solve real world problems with computer instructions.

And C is both low-level enough and high-level enough to allow you to do all of this at the same time.