January 22, 2012
Learning a New Language
By: rbomb
Introduction
My name is R*BOMB. I am a Computer Science student at the University of Alberta, and I get asked a lot “How do i learn a language? Well I see many answers to that question but all of them are specific to a particular language. I decided to make a more general guide for users who have some basic experience in programming, probably self taught or from high school, to help them learn how to approach the task of learning a new language. Lets face it… a single language cannot do everything that you want it to do, forcing you to learn a new language. Excuse my poor spelling, poor grammar, and terrible run on sentences – as I already mentioned I am a Computer Science student.. not a English student.
The Goal of Programming
As a programmer your goal is to solve a problem. To solve a problem you often need to figure out what steps are needed to find the solution that you need. Sometimes this involves input sometimes it does not. When you make a function you can think of it as a math function, where an input maps to an output (f(x) = y) where x is your given input. When you make a procedure you are not giving input but rather that procedure is executing an action that is constant throughout.
So lets say you wanted to turn a cat into a dog… you would need to find the differences between the two and within your function you would address each difference individually. Once each difference is found and changed, then you have turned your cat into a dog. Sometimes it can be hard to figure out how to manipulate your cat parts into a dog part. What you need to do is continue to break your problem down into sub problems until it becomes trivial.
So the general approach to solving your problem is not to build up a solution, but rather to break down the problem, then build up the solution to the same level.
How to Learn a Language
There are many ways that people can use to learn a language some people buy books, some people look for tutorials and others just stare at examples. All of these work to an extent but it is hard to take it to the next level. I can show you 100 examples from hello world to capturing packets, but the chances are if i asked you to make a custom control for use without .NET or swing that simply shows some text in a window, you would not know where to start. This is no good… means you have a grasp of the examples and what you were shown but not of the language. There are infinite examples, so no one can show you everything. So instead of learning by example, i find it is far better to actually learn the language for what it is, then apply logic to do what you want to do.
So here is how we are going to do it… I am going to list questions… in a organized form, once you can answer all of them from the top of your head with no reference, then you probably have a pretty good grasp of the language. To learn using this method, all you really need to do is use google to find the solution. However for it to work, you cannot look for the most basic solutions, actually research what you are googling… is there a “However…” a “You must…” or any other note on the side that you need to know about?
Before you start with questions and researching topics the very first thing you should do is find the “hello world” example of your programming language of choice. This program without the Hello World part would be the absolute bare minimum you need for your program in most cases. This typically, but not always includes a main function or entry point, this is where your execution starts. Some languages do not even have this, and simply start from the top of the script working its way down… differs language to language but before you start you have to ask…
1) What is the simplest program possible?
2) Does my language support data types or does it use a universal type?
3) How to I define a variable? Can i set any flags to my variable?
4) How to set and read a variable?
5) When can i modify a variable?
6) Does my language differentiate between procedures and functions? Does it treat both with the more abstract term “Method”?
7) How do i declare a Method?
How can my method have variables and return stuff?
9) Are there any flags that i can set to my method?
10) How do i use the method that i created?
11) Can i used a function as a procedure?
12) Is there more to my application than the entry point? (Class definitions, includes/imports)
13) How can i convert one data type to another data type?
14) Does my language support objects?
15) How can i create a new object? How can i destroy it?
16) How do i define a new object?
17) How do i used the object i just defined in a different context?
18) do my code blocks need to be encompassed in any way?
19) What functions can I do with strings, if strings are a supported datatype?
20) Can i treat strings like an array?
21) What types of loops are there, and how can i use them?
22) do for loops need to be based on an integer?
23) how can i exit a loop before it is done complete execution?
24) Where can i use loops?
25) What types of control flow features are there?
26) How does the switch statement work if supported?
27) are there different ways to define a “else if” statement? (such as pythons elif)
28) Is my language procedural, functional, or object oriented?
29) is my language low level or high level?
30) if low level how can i use pointers?
if object oriented…
31) How can i declare an interface?
32) How can i implement a interface?
33) how can i used abstract classes?
34) is there a form of packages/namespaces?
35) how do i navigate the objects and source files that i created in code?
36) How can i represent associations in this language?
37) How does inheritance work?
38) Can i implement multiple interfaces?
back for all languages…
39) What API’s and libraries come standard with my language?
40) What can i do with these libraries? (A lot of research if not most research will be done here)
41) How do i include different libraries?
42) How can i compile my code (if it can compile)
43) How can i run my code?
44) What are the limitations of this language? Advantages?
45) When my code is compiled, what is it compiled too?
46) is my code run directly on the cpu? if not, what is running it and is there any addition features i can exploit with that?
47) How can i make comments?
48) Is there a documentation engine that i can use?
49) Can i multithread?
NOTE: I am probably forgetting many aspects here, ill add them as i remember them. If you see something missing, PM me the subject
If you research each of those questions to their full extend you should understand how to use the language, its just a matter of using it correctly… which i shall help with next.
Some languages have no answer to some of these questions, it is important to note these cases, as this may be a game breaker for what you are trying to do. Though there is usually a few ways to work around these missing bits. So i would not worry too much about what you cannot answer.
Code Documentation
Code documentation is not nearly done as much as people should, especially in new programmers. Documenting code would be the act of commenting your code or writing about what your code is doing elsewhere. It is not hard to do, but most people see it as unnecessary as they know what their code is doing when they are coding it. Unfortunately 6 months later when they go to make some changes, the code that they used to know what it was doing now looks like gibberish.
An example of this would be this line from one of my old C++ project
(((RIRC*)this)->*msgParm[8])((Msg)this.outQueue.front());
I know that it is clearly a function call... but i have no idea which function. I can find out via some leg work.. but this problem should never of existed in the first place.
So that is why it is important to document your code, however be weary of over documentation. If you have a area where you document your code a great deal, rather than perhaps... 1 or 2 lines in a method... then there is a good chance that you have created hard to read over complicated code.
When you think this is the case then you need need to look at your method and see if you can break it down into a series of smaller methods to keep the system simple and easy to understand. We call this a refactoring and is very common in computer programming. If you stay on top of it, your code will end up being very simple and easy to understand and read, as it is broken down a fair bit. This is almost always possible.
Remember when you are coding, treat your methods like paragraphs, one method for each topic. You should never have a method called CatFood and end up talking about dogFood at the end of it. Simply does not make sense. Keeping your methods simple reduces the amount of documentation you need in your system, and simply... looks better.
Procedural Programming
Most of the languages you are going to be learning are going to utilize the concepts of procedural programming. Procedural programming is basically the concept that your code is broken down into methods and executed in a sequence that solves your problem. What i want to say here is, although it may appear that many things are happening at once, there is always just one thing happening. Your code is executed from top to bottom from the entry point with various go-to's It is not executed in multiple locations at once.
Object Oriented Programming
Object Oriented Programming (OOP) is what modern programming has come to. It has its roots in the 70's however did not really take off to what we know it as today until ~2000. The basic idea of oop is that data structures and related methods can form objects that can be created dynamically and destroyed dynamically. These objects can interact with each other and be managed at run time. OOP is a very very large subject and is the primary focus of software engineering. But we shall cover the basics here.
The main concept around it would be object inheritance. This means that objects of one type which is a subset of a parent type can inherent the features of the parent object. For example lets say the Object "Plant" has the method "Grow()". We want to make the object "Tree" and the object "Flower". Tree has branches so it has the method "getBranches()", a flower has petals so it has "getPetals()". Both Tree and Flower can grow so instead of making the "Grow()" method in both, we inherent the object "Plant" which gives Tree and Flower the "Grow()" method.
There is not way to determine when it is appropriate to use inheritance, however there is a way to determine when it is not. One way would be a IS-A test. kinda straight forward... two examples: "A tree IS-A plant" this sounds right so there is a chance that the relationship is appropriate. However, "A car is-a plant" sounds wrong... and the relationship probably isnt a good one. Another method would be called Liskov substitution principle, this is a idea where a object that is in the subset of another object can replace any object where the parent is being used. If this can not happen, then this is a bad relationship. Read on that one more: http://en.wikipedia.org/wiki/Liskov_...tion_principle
Design Patterns
A design pattern is a pattern that makes your code more standardized in design, this is more of a OOP concept. By making your code more standard it is easier for people also familiar with the standard to help you with problems, or even to pick up right where you left off. It also simplifies your design allowing for a lower chance that there will be bugs.
There is a fair number of design patterns but the main ones (http://www.blackwasp.co.uk/GofPatterns.aspx) are very prevalent almost everywhere. I recommend that you read up that link and research it. Would help a lot. Another thing to look into is called "UML" Unified Modeling Language.
Designing your application before actually coding it will help a lot, and actually speed things up in the long run... trust me on that.
Design Principles
A few principles i thought i would mention..
1) Simpler code is faster code
People often believe that writing clever code that on paper is faster than simple code is then as their work on paper suggests... is faster. This is not the case, computers are far better at optimizing code than humans are, but for this to work well the code has to be simple.
2) Liskov Substitution (as i said before)
3) Learn some basic algorithms such as sorting algorithms and searching algorithms (i will probably add a section on this later)
Understanding What Your Code is Actually Doing
Most languages compile their code directly to assembly, but not all. The important thing is, your code will always reach some sort of low level assembly like state. This assembly can be encoded to binary by hand (with ease actually) and decoded in the same way. This is how you can read some binary executables as assembly. Your binary is stored in 32-64 transistors and run through the processor and executed based on which transistors are on. A the machine level is is actually a very simple machine. Your code is executed using a combination of 3 logical operators, OR, AND , and NOT. This allows your computer to do math and everything it needs to do. It is amazing how the screen you are looking at right now was produced using only these three operators. Math these days...
Microsoft Standards vs Other Standards
Microsoft feels that they are better than everyone and developed their own standards with their own compilers. I strongly recommend that you try to avoid the microsoft standards, because they will not work on other platforms and other compilers. Whereas the more general standards associated with linux and mac, and windows applications not in .NET will work with all, even on the microsoft compilers. Not reason to limit your self to one system.
How to Find Reference Material
Most languages have websites that are dedicated to documentation of the standard language. PHP for example has php.net and Java has Javadocs the whole standard language right on oracles website. These are usually not hard to find. Every API and library you use should also have similar documentation for you to use. A simple google search is usually all you need.
How to Find Information on Systems Other Than Your Own
This can be a bit more tricky. Sometimes you can find documentation but other times it simply is not there for you to use. When this is the case you have to put your thinking cap on full speed. I usually research the system type a lot until i have a basic idea of how to make the most basic version of it. The chances are the system you are using is based of what you have designed yourself, and you just have to see how they have built onto it.
So What Language is Best?
There is no "Best" language. But there is a best Korea (North). The language you decide to code first should be well thought out about the future, if you want to do this in the long term you will probably have to learn another language later. Languages are often based off of other languages, so the farther down the way you go, the more languages that will come naturally.
For example Java and C# are considered to be C based languages, their syntax is similar and after learning C, java and C# come naturally. However C is a old language, and is not an OOP language, so i wouldn't recommend that language unless if you wanted to learn C++. Learning Java first is a huge advantage if you are going to go to school for computer science, as it is the only language built with modern object oriented concepts in mind. Also one of the most popular in schools and in industry (believe it or not). After learning Java, you can almost walk right into C#, and think its the same language. That does not go both ways. You can pick up C# after java. That is good.
However some 14 year old is going want to be "1337" and learn C++ first. He will fail. But if you insist all i can recommend is... Learn C first. Then C++.
Comments (0)