Did you recently start coding? I have several guides on learning how to code. But recently, I found out that most people do it the “wrong way.” So, this is a guide about best practices and how to make most of your coding journey.
By understanding the fundamentals of coding, you’ll be better equipped to learn and master any programming language.
Firstly, get clear on why you want to learn how to code. Then, dive into the foundational coding concepts, like variables, operators, and data structures. Next, choose the programming language you would like to start with.
You can use online courses, bootcamps, or formal education to advance your knowledge, but practise often and build projects. This will help you get more comfortable with coding.
Getting Started with Coding
1. Choose Your First Programming Language
The first step in your journey is choosing a programming language. Your choice will depend on your goals, such as the projects you want to work on.
Are you interested in web development, mobile apps, or data science? Each field has its go-to languages:
- Python: Ideal for beginners, Python is one of the most popular languages due to its simplicity and versatility. Whether you’re interested in web development, data science, automation, or artificial intelligence (AI), Python has you covered.
- JavaScript: Essential for web development, JavaScript allows you to create interactive websites and apps. It works for both front and backend development, making it a strong choice for web or mobile app development.
- Ruby: Known for its elegant syntax, Ruby is another beginner-friendly language. It’s particularly strong for building web applications, especially with the Ruby on Rails framework.
- Java: If you’re considering mobile development, especially for Android, Java is a solid choice. It’s also widely used in enterprise-level applications.
Pro Tip: If you’re unsure, start with Python. It’s easy to learn and versatile, with a large support community. Once you’ve mastered the basics, you can easily switch to other languages.
2. Set Up Your Development Environment
Here’s a quick guide to setting up:
Text Editors vs Integrated Development Environments (IDEs):
Text editors like Visual Studio Code (VS Code) are lightweight and easily allow you to write code in various languages.
IDEs like PyCharm (for Python) or IntelliJ IDEA (for Java) offer additional features like debugging and code completion, making them great for more advanced coding tasks.
Version Control: You’ll need to get used to saving and tracking changes to your code, as this is essential to working on projects of any size. Git is a popular version control software used by over 95% of developers.
GitHub is a code storage platform built upon Git and works seamlessly. Git is also essential to working on projects with many people or across computers, making tracking changes easier.
It’s so important that our former teaching assistant, Anna Skoulikari, wrote a book about it! Check out ‘Learning Git‘ to see how you can get started on this essential software. Anna has also designed online courses for Git on Udemy, and a step-by-step guide on freeCodeCamp.
You can quickly get started by installing Git and creating a GitHub account. Practice pushing and pulling code changes from your local machine to GitHub.
Pro Tip: GitHub also acts as a portfolio where you can showcase your projects to potential employers.
Basic Programming Concepts
1. Syntax and Structure
Programming languages may look different, but they share common structural elements. Understanding basic syntax is like learning the grammar of a new language.
Variables: Variables are placeholders for data. In Python, you can create a variable without declaring its type, like so: my_variable = 5.
In contrast, languages like Java require you to define the type of data: int my_variable = 5;
Data Types: Most languages have basic data types, such as:
- Integers: Whole numbers like 5 or -3
- Strings: Text, like “Hello, World!”
- Booleans: True or false values (True, False)
Functions: Functions allow you to encapsulate blocks of code that can be reused. In Python, you define a function using the def keyword:
def greet(): print("Hello, World!") greet() # Calls the function and outputs "Hello, World!"
2. Control Flow
Control flow statements guide the direction in which the program executes.
Conditionals (if/else): Conditionals allow your program to make decisions based on certain conditions. For instance, you can have your code check if a number is positive or negative:
x = 10 if x > 0: print("Positive") else: print("Negative")
Loops (for/while): Loops enable you to repeat a code block multiple times. A for loop can iterate over a range of numbers or items in a list:
for i in range(5): print(i) # Prints 0, 1, 2, 3, 4 count = 0 # A while loop runs as long as a condition is true: while count < 5: print(count) count += 1
3. Data Structures
Data structures let you store data efficiently.
Lists/Arrays: Lists (or arrays) store a sequence of values. In Python, you can create a list like this:
fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Outputs: apple
You can add, remove, or modify items in a list with various methods.
Dictionaries/Objects: Dictionaries (or objects) store key-value pairs, which makes them great for representing real-world data:
student = {"name": "Alice", "age": 25, "course": "Data Science"} print(student["name"]) # Outputs: Alice
Knowing how to use these data structures will help you tackle complex coding tasks, which are foundational in storing and manipulating data.
Writing Your First Program
The first program most beginners write in any language is the classic “Hello, World!” It’s a simple exercise that familiarises you with the basic syntax of your language.
Let’s go over how to write this in Python:
print("Hello, World!")
That’s it! Running this in your Python environment will output Hello, World! to the screen. Here’s how to break it down:
- print(): This function outputs text or data to the screen.
- “Hello, World!”: The text you want to display goes inside quotation marks, making it a string.
Once you’ve mastered the basics, like printing text, you can move on to more complex tasks. Try modifying the program to take user input:
name = input("What's your name? ") print(f"Hello, {name}!")
In this case:
- input(): Asks the user for input.
- F-string: A formatting method in Python that allows you to insert variables directly into strings.
This simple progression from displaying text to interacting with the user is an excellent way to understand how programs work in real life.
Best Practices for Beginners
1. Write Clear and Descriptive Code
Good code is self-explanatory. Use variable names that describe the data’s representation and function names that explain what they do.
Bad Example:
x = 5 y = x * 2
Good Example:
radius = 5 diameter = radius * 2
By using meaningful names, you or anyone looking at your code can quickly understand what’s going on without dissecting it line by line.
2. Comment Your Code
Comments are notes we (developers) write to explain what certain parts of the code do. It’s a habit that will help you in complex projects, especially when you revisit your code after a while. Or when working with different teams.
This function takes a user’s name and prints a greeting
def greet_user(name): print("Hello, {name}")
Use comments to describe the “why” behind your logic, not just what it does—especially when dealing with complex algorithms.
3. Avoid Hard-Coding Values
Hard coding means putting fixed values into your program instead of making it flexible and reusable. For instance:
Bad Example:
total_price = 100 * 2 # Only works for this case
Good Example:
price_per_item = 100 quantity = 2 total_price = price_per_item * quantity # Works for any case
Always aim to write reusable code that can be easily adjusted for different scenarios.
Alternatively, if there are constant values, you should hard code them using a constant variable.
Example:
SKU_PRICE = 100
How long does it take to learn coding from scratch?
Most programmers will say it takes three to six months to be fully comfortable with the basics of coding, but this depends entirely on the person and their learning pace. However, no programmer knows everything.
Coding, by nature, involves many ever-evolving concepts and new developments, so you can never stop learning in this position, even as a professional.
Developing Good Coding Habits
Developing strong habits early in your coding journey will save you headaches later and accelerate your growth as a developer.
1. Practise! Practise! Practise!
Coding is like any other skill—it requires regular practice. Aim to set aside time every day, even if it’s just 30 minutes, to write and experiment with code.
The more you practise, the better you will become at writing code and solving problems. This consistency will help you reinforce the concepts you’ve learned and build your coding skills.
2. Break Down Problems
Before jumping straight into coding, break down the problem into smaller, manageable parts.
This approach is often called “divide and conquer” and is essential when working on larger projects. Write pseudocode or outline your logic in plain English before you start coding.
For example, instead of diving into code for a complex sorting algorithm, first outline what each step will look like:
- Get the list of numbers
- Compare each number with the next
- Swap their positions if needed
- Repeat until the list is sorted
Breaking down problems helps you think more logically and systematically, which is a valuable skill in coding.
3. Review and Refactor Your Code
Once you’ve written your code, take some time to review it. Can it be written more efficiently? Are there sections where you’re repeating yourself?
Refactoring is improving your code’s structure without changing its behaviour. For example, if multiple lines do the same thing, you can refactor it into a loop or function to simplify it.
4. Learn to Debug Efficiently
Debugging is an inevitable part of coding, and learning how to do it efficiently will save you hours of frustration. When your program doesn’t behave as expected:
- Use print() statements to see what’s happening with your variables.
- Utilise debugging tools available in your IDE, such as breakpoints, which allow you to step through your code line by line.
- Read error messages carefully—they often point you directly to the problem.
Developing these habits will improve your efficiency and make you a better, more thoughtful programmer.
5. Coding Challenges
Many online platforms and coding challenges are available that provide practice opportunities. These platforms often have coding exercises and challenges that you can work on to improve your skills.
CodeWars features varying levels of coding exercises available in multiple programming languages. The challenges are free to solve multiple times, and you can access others’ solutions as soon as you’ve figured them out.
HackerRank is another great platform for coding challenges. It also has varying levels and languages to work in. Both sites are often used in professional interviews, so they are great tools to practise your problem-solving skills.