This content originally appeared on DEV Community and was authored by DEV Community
Jump To
- Syntax
- Reading Docs
- Pseudocode
- Commenting
- I Want It That Way
- Eek! Errors :( Tell Me Why
- Practice Challenges
Coding can be as easy as typing exactly what you see in a book, video, or tutorial and hoping it works. This will work at first, but itâs better to have some tools to help you learn and really understand what youâre doing along the way. These tools will help you better understand what youâre doing and how things work. All of these tools are things professional programmers depend on.
Understanding Syntax
Whether youâre writing in English, Pashto, Hebrew, or a programming language they all require you to use combinations of characters, numbers, and symbols. These combinations of characters, numbers, and symbols mean something. When you were learning to read you were programming your brain to understand these combinations as different sounds. Then you used these sounds to make words. Once you could read words, you could read sentences. There are rules or syntax that tell us how sounds and words work together. Syntax tells us what the correct format is to write certain things.
In programming, syntax is used to define the correct combinations of characters, numbers, and symbols to make the code work. The syntax is different for each programming language just like it is for written and spoken languages. For every new code thing you learn, you want to make sure you look at the syntax and refer back to it if you donât remember. If you type the wrong syntax, the computer will not understand and it will give you a SyntaxError
. If you get a syntax error, look up the syntax to verify you have the right thing.
When looking up the syntax the best option is to look at the documentation. What is given in a tutorial, video, or book is probably correct, but sometimes things change or there are typos. Documentation is the go-to source for all your syntax needs.
Reading Documentation and Searching the Web
As our go-to source of information, documentation or docs as most programmers call it, is incredibly important. You can go directly to the documentation website (docs.python.org), but sometimes the website isnât the easiest to navigate when you donât know all the terms that are on it. Personally, I choose to go to my search engine (e.g. DuckDuckGo, Bing, Google) and type in python docs
and the thing I want to look up. Most of the time, one of the first results is exactly what I need. Notice, I included the language Iâm working with and the word docs
.
Reading documentation can feel daunting at first. Letâs look at an example and figure out whatâs what. There are several things we need to look at.
- Syntax - the name and required symbols to use, in this case
print()
- Required things - anything inside the
()
without an equals sign=
- Things Not Required - anything inside the
()
with an equals sign=
These things may look like gibberish, but I promise, no one expects you to understand what any of this means. In the documentation there is a description of each thing in the()
underneath this line of code.
Pseudocode
We all program every single day of our lives whether or not we realize it. When you get up in the morning, you plan out your day. This act of planning is creating a script or program for your day. This may not be in a computer programming language, but it is absolutely programming. Itâs an informal list of steps or a to-do list. In coding, we actually do something similar to this. We call it pseudocode.
Sounds goofy, right? Why are we learning something that basically means âkinda sorta codeâ? Believe it or not, this is an integral step. Pseudocode is basically a to-do list. We use informal plain language to describe what we are about to code. You may think of it as an outline, plan, or to-do list.
Once you have a pseudocode outline of what you want to build, you can use the steps to help guide what you want to code. In What is Programming, there was an example about how to build a watch. That was pseudocoding. The steps were written with all the focus on how it should work and not on the programming syntax. Though, in real pseudocode, each line would be a comment instead of a bullet on a list.
If I were going to pseudocode for the watch, it would look something like this. Do know that there is no right or wrong way to pseudocode. You may choose to write it differently than someone else. Thatâs okay. Itâs just a guide.
# Repeat steps forever and ever until end of time or battery dies
# Every 1 second up to 59, add 1 to the seconds on screen then
# Add 1 to the minutes on the screen up to 59 then
# Add 1 to the hours on the screen up to 12
Commenting
In the pseudocode watch example, every line starts with a #
. Whenever you use a #
youâre saying the computer should ignore what comes after it. What comes after the #
is meant for humans. Typically, comments are on the line above your code or at the end of the line. It should be an explanation of the what and why of your code. Think of it as a reminder to a future you and an explanation to anyone else reading your code. Oftentimes, your pseudocode will be repurposed and used to comment your code.
You can also use comments to leave yourself to-do things with pseudocode notes.
print(âThe Henna Warsâ) # output title of book
# TODO: output author of book - Adiba Jaigirdar
I Want it That Way
One of the best and worst things in programming is the fact that we have to name things. Naming can be a lot of fun. Later on, youâll see that I named something cat_summoning_spell
. I promised I giggled when I came up with that. As fun as it can be, naming can be hard too.
Names have to be unique. They canât be the same as anything else or your program will get confused. Imagine having a cat named âChickenâ and a pet chicken. That would quickly get confusing.
Names should also be memorable and descriptive. Using x
or foo
doesnât explain what something is. Instead, you could describe what the thing is. If youâre counting stars, you could use something like star_count
, star_num
, or num_of_stars
. Having descriptive variables will help you avoid errors.
Eek! I got an Error. What now? - Tell Me Why
If your code didnât work, that is okay. This happens ALL the time. You have a bug and you need to debug your program. Here are the steps we want to go through. Weâll go into each of them in detail shortly. While they are in order, there is nothing saying you have to do them in this order.
- Read the Error
- Find the Culprit
- Make sure the syntax is good.
- Names match (eg variables, functions, classes, etc)
- Comment out code
- Ask for help
- Rubber Duck
- Google it
- Actually ask someone
- Take a Break
Understanding the Error Messages
I know, errors are scary and look like you broke something. Thereâs no need to worry though, itâs trying to help you. Errors tell you where the error is and what the problem might be. Letâs look at an example and figure out whatâs what. There are several things we need to look at.
- Type of error
- Description of error
- Line number - which line the error is on
Find the Culprit
Now that the error message has led you in the right direction, letâs look where it said the problem is. When youâre first learning to code, your most common errors will be a SyntaxError
and a NameError
.
Youâll get a SyntaxError
when your syntax is messed up. Verify you have it right. I promise, even if you think you have it right, verify anyway. If youâre not sure, you can always look at documentation. Oftentimes, we assume we typed something right and we made a tiny mistake.
Speaking of tiny mistakes, we get NameError
when names donât match. For example, if you have something called dog_feet
but you later tried to use dog_paws
or dog_foot
youâll get this NameError
.
If youâre having trouble finding the error, you can do what we call âcommenting out codeâ. This means you can put a #
in front of that line so the computer will ignore that line. Youâll do this with lines that work perfectly and you know arenât the problem. This helps narrow down the offending or broken code.
Ask For Help
If the other steps didnât help, try asking for help. There are lots of ways to do this. You can do what we call ârubber duckingâ or ârubber duck debugging.â This just means that you are explaining your code and the problem (to the best of your ability) out loud to something like a rubber duck or maybe your cat. You can also do this by typing out your problem as if you were going to ask a question. Oftentimes, this will solve your problem because youâll be reading off your code and realize what was wrong.
If explaining your code to a rubber duck, your cat, or your programmer friend hasnât worked, try googling the problem. Typically this would mean you want to search the language youâre working with and the error. You might search something like python NameError: name 'cats' is not defined
. Generally the results will lead you in the right direction. You may have to look at a few different results to get the answer you need.
Take a Break
If all else fails, take a break. Yes, seriously. Get up, walk away from your code. Take a walk, get some water, go play fetch with your dog, anything. I know youâre thinking this wonât work, but try it anyway. When you go back to your code later the problem may be glaringly obvious. It also might not be, but youâve cleared your mind and are in a better position to find the pesky code. The goal here is to not sit there getting frustrated for a long time. Learning to code should be fun. If you find yourself getting upset with yourself, take a break.
Do you remember?
Letâs practice what weâve learned so far. Go ahead and comment on this post with your answers. If you don't remember, no worries, you can always go back to read sections again.
Write a Pseudocode example or three
Try writing out the steps on how to do or build a real-life thing. Ideas: make a sandwich, use a TV, ride a bike, use a keyboard, or how water flows through kitchen sink plumbing
Pseudocode Examples |
---|
This content originally appeared on DEV Community and was authored by DEV Community
DEV Community | Sciencx (2021-11-21T17:23:23+00:00) How do I code?. Retrieved from https://www.scien.cx/2021/11/21/how-do-i-code/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.