DEV Community

Lau Balos
Lau Balos

Posted on

Problem solving

What do you think about before writing code for anything? What's your process? Comment down below, i need some tips :)

Top comments (1)

Collapse
 
shiroihana013 profile image
ShiroiHana013

My process usually begins with plain and simple pen and paper. I have several notebooks that I keep around for brainstorming. There are two benefits to this habit. Firstly you can look back and see how far you have come over the years. And finally, there may be a situation where you can reuse the logic you worked so hard on.

Don't forget we were able to reach the moon with programs written by hand ;)

These are the steps I usually try to follow:

  1. Figure out project requirements:

This boils down to thinking about what requirements are absolutely necessary for the project to be a success. Try to keep them as simple as possible. After you have finished this you can write down your "stretch goals" as well. These are goals you would like to try and implement if given enough time (and energy).

  1. Think about what you need to learn:

It may be useful in the early stages to think about what you may need to learn for the project. If you aren't sure what you need to learn you can begin by doing some research. Usually, your requirements will guide you on what you need to search.

Example: One of your requirements is reading and writing from a file. If you are not familiar with this process do some quick research.

You can always revisit this step later as well.

  1. Decide on a programming approach:

There are several different programming approaches that can be taken. Here are the main ones I can think of:

  • Simple one-file script

When you first start learning Python you are most likely writing all of your code in one file. This is a perfectly good approach for small projects that aren't too complex. However, this approach isn't recommended once you start tackling harder projects. It is still useful in situations where you are creating a simple test script.

  • Breaking the project into functions and separate files

After writing down your requirements it should become clear whether or not your project needs to be broken down into smaller more manageable parts. This knowledge will also come with experience. The easiest way to do this is to utilize functions and split your code into several files. Here is a resource to get you started if you haven't already.

The best part about this approach is reusability. Once you create a Module you can reuse it as many times as you need for other projects.

  • Object-Oriented Approach

I am unsure if you have ever heard about Object Oriented Programming so here is a resource. It is an extremely powerful principle. You may consider using this approach if you are implementing a complex system of "objects". An object can be a number of things but some of the classic examples are real-life objects.

Here is just one example of a case which may benefit from using OOP:

You want to simulate a social network. A social network can be broken down into people and friend groups. In this case, you can create people and group objects.

After playing with this you will get a feel for when OOP will be useful. You will also learn to recognize when it will overcomplicate your project.

  1. Develop the project structure/outline:

:)

Up until this point, you have been thinking mostly about the "big picture" of the project. Once you have completed the above you can start planning the structure of your project. I promise this is a worthwhile step and will save you a big headache down the road.

The goal for this stage is to think about the project in smaller parts without thinking about the how. You might ask yourself what functions and classes (if you are using OOP) will be useful. Another question you can ask yourself is, what files can I split this up into?

Let's revisit the example above; the simulation of a social network. You can probably break the project down into 4 main files.

  • main.py: the main starting point of your program. This will be where you link all the pages and classes you will be using. You can call different functions from here in order to get your results.
  • Person.py: This will be the file where you write code relating to the Person object.
  • Group.py: The same as person.py just for the Social Group object instead.
  • utilities.py: A file to contain miscelaneous functions that are necessary but don't really fit anywhere else. It is considered good practice to minimize the amount of code in this file. As your projects get larger it will become harder to read.

Before I move onto the next step I usually create the skeleton of the project. I create each file I think I may need, organize them into folders, and write the outline of functions and classes. To avoid compiler errors you may want to format them in the following way:

def exampleFunc(params):
    print()
# or 
def exampleFunc(params): 
    pass
  1. Pseudocode:

The last step I take is to write pseudocode. It is a method of writing the logic for a program down without actually writing functioning code. The goal for this is to think about where you may need loops, and if-else statements. I usually write this as comments directly in my function or class. The benefit to this is later on when you come back to look at your code it will be easier to read.