My Background with Languages
Don't Worry about Learning Syntax
The message I give my friends and family is that it doesn't matter what language you learn as long as it is interesting and you have a challenging but achievable goal in mind. Use your first language to learn the PATTERNS of programming that you can then apply to any language you take up. Pick a language, it doesn't matter which one, and dive into free tutorials on the web. Focus on the structure of what you're learning rather than the exact syntax. As you go, focus on the fundamentals that ALL languages share.
Then start working towards your goal. Your goal should be a simple site you've devised to learn the language. Having a goal is important because to learn a language you need to apply it. I am about to undertake writing an online game to teach myself some new technologies I've been interested in. Take a second to read the story of the dating site Plenty of Fish. It started as a project for the creator to teach himself ASP.net. The site went on to make millions.
A Core Set of Patterns to Know Exist
Output to the screen, console, or log file.
In order to follow along as a program is running you need it to print status messages somewhere. Whether you are logging data to a console, or printing to a webpage/screen, just know that almost all languages have a way for you to add visual feedback.
Adding comments or "commenting out" code.
Many languages include a way to add comments to the code that are ignored by the program interpreter or compiler. Comments can be used to add notes to the code. This is also very useful when you want to make a program skip a chunk of code while you are testing it.
The slash+star method works in many languages and can be used for multi-line comments.
You can also use the multi-line version to comment out entire chunks of code so the compiler or interpreter skips them.
All languages have a way to store data into the computer's memory so it can be retrieved or acted upon later. This is usually accomplished through creating a named variable. These variables can then be acted on in the future or control future logic branches.
A condition is, and I'm talking about the literal definition here, the state of something. In a programming language, these conditions are used in tests, or "conditional statements" to determine if something is TRUE or FALSE. True and False are the basis of ALL logic in programming. Remember, computers ultimately just know 1's and 0's.
Logic: If, Else, and Else If
All languages have a way to make decisions. You can use this to execute a chunk of code only when a condition is met. This is accomplished through the concept of an IF statement. IF something is true THEN do X. IF's are almost always paired with ELSE statements that then do the opposite. IF something is true THEN do X, Otherwise (else) do Y. And there is usually a concept of an ELSEIF that can be chained in there as well. IF something is true, do X, ELSEIF something else is true, do Y, Otherwise (else) do Z.
I like to joke that if you are smart enough to not touch a hot stove you are smart enough to code. You understand conditions and logic. If the stove is hot THEN don't touch it.
An operator is something that takes one or more values and returns another value. This is getting into jargon here, but there are unary operators (take a single value) and binary operators (take two values) and even a ternary operator which takes three values. And you can string these together to form complex conditional statements. You will want to google common operators for the language you are working in, but here are some common examples:
The unary "logical NOT operator" ! (exclamation point) tests if something is NOT true.
The binary "logical AND operator" && tests if two things are true.
The binary "logical OR operator" || tests if either one of two things are true.
A complex compilation of the three
A comparison operator
There are also Assignment operators that help you when doing math such as incrementing, decrementing. There are also string assignment operators to make it easy to append strings together. There are less than 30 operators in the PHP language and I use 15 of them on a regular basis and a few of them rarely if ever. You can search for a complete list and get to know them for whatever language you are learning.
Loops: For and WhileLoops are where the real magic of programming happens. The idea is that you repeat a certain task forever, until a condition is met, or iterate over a group of values. The most basic example is a loop that will repeat a task 100 times. You wouldn't want to copy/paste the same code line after line.
You can accomplish the same thing with a while loop...
Arrays, Collections, Lists
Most languages have a way of grouping multiple items together. Some languages call these arrays, some call them collections, and others have lists. Some languages have all three but use each slightly differently. To go along with these groups there is usually a foreach statement that lets you loop over the group.
We are starting to get into more complex topics now, but just to come back to the whole point, I did go to the PHP Manual for Foreach to make sure I got the exact syntax right. It differs from language to language, but know it's there so you recognize the pattern and can repeat it in whatever language you are working in.
Functions and Procedures
The last topic for this post is going to be functions. They are sometimes called a method, procedure, subroutine or subprogram. They are simply a named chunk of code that may or may not take values in as arguments and may or may not return a value. Nit-picking computer scientist know the difference between a procedure and a function, but you don't need to know the difference to use them. The key here is that you can use functions to isolate code that needs to be run many times so that you only have to write it once and then you can "call" or "invoke" the function from different parts of code or within a loop to write more compact, efficient, and easy to read code. A general rule of thumb is to have your functions do one thing well.
Here if you wanted to repeatedly find different peoples location in a group you wouldn't want to repeat that function's code over and over again. You'd want to write the function once and use it over and over. It's just an example, but almost every language has the concept of functions and knowing you can put code into functions so it can be repeatedly called is the pattern you want to know.
I haven't covered inputs. Programs are usually acting on some kind of input from an end user, or a database, or some other system. Each language has its own way of getting to those inputs. From a pattern perspective just know that collecting inputs has already been solved and you can use a search engine to find specifics on how to collect data from users or other systems.
Design Patterns and Frameworks
When you get deeper into coding, you will run into the same problems over and over again. They usually have the same solution. There are entire courses in computer science pgorams that discuss known algorithms and Software Design Patterns. Many common problems are already solved and there is no good reason to re-solve them. Even in your own code, copy-and-paste is your friend. I often start new projects by loading up a framework I know has already solved a lot of what I'm working towards. When you get further along you'll know if you want to start a PHP website using Laravel or Drupal, or just roll your own. Don't think you're going to be starting every project form scratch.
It doesn't matter what language you set out to learn, you are going to need operators, variables, logic structures, loops and functions. You'll also have inputs and some type of output. You'll want to add comments to your code. This is not just the tip of the iceberg, this is what programming is. If you've followed along to this point, you know enough to write a program. Now you just need to go do it. Don't be afraid to fail because you will. Over and over again. Search engines and tenacity will get you through.
If there are patterns I've missed please leave a comment below or send me an email.
I have a newsletter...
Many of my posts end up in Digital Ambit's monthly newsletter. It is the best way to keep up with what Dagny and I are doing in the business world. I appreciate your support and will only send you things we think are valuable.