loading...

Why cant I code???

alokozai profile image Qaiss Alokozai ・3 min read

This question bugs everybody. I have been wondering why is that and why that you know the fundamentals as well as syntax then why is it? I have been thinking about this question from day 1 of my studies in computer science where I had no idea of int i in java. My hypothesis has been changing over the years and I want to share it with you my todays 95% confidence hypothesis i.e. lack of use case. For example you studied insertion sort but did you create it the algorithm? No! So what is required is we should have our own use case in order to start coding. For example you want to write a function that takes two inputs n1 and n2 and returns result = n1+n2. Now you know well that is quite easy. Ask yourself why? Because you can think in your own terms/use case.

def addition(n1, n2):
    result = n1+n2

Why was this so easy? Because you understood it very well. You had this use case properly understood. It is something that you do every day. Now you want to make it dynamic and ask the user for input instead of inserting n1 and n2. No problem that is just a logic just like we do e.g. what is addition of 2 and 2 and what we want now is ask what is your n1 and what is your n2 and then we give the result.

def addition(n1, n2):
    input1 = int(input("what is n1")
    input2 = int(input("what is n1")
    #now result will not be n1 and n2 because now the values 
    are held by input1 and input2 so we do the changes. 
    result = input1 and input2 

Now this function will run for one time and then the next time you run it you don't have access to these values. So for that we can use data structure. Remember data structures to me are just short term databases. Because you can use database for saving these values but then it gets expensive to hit database every time.

def addition(n1, n2):
    myarray = []
    input1 = int(input("what is n1")
    input2 = int(input("what is n1")
    #now result will not be n1 and n2 because now the values 
    are held by input1 and input2 so we do the changes. 
    result = input1 and input2 
    myarray.insert(result)

Now you see, you can code. We have extended our simple function even more. Now you can extend this function as much you like no matter what, sky is the limit. You are the creator of this function.

Second use case: you want a function to take student information from name and other details. Normally we think of it as student1 : [name:s1, section:1, subject: Data Structure]. You can do this as well, why not you are the creator.

def studentinformaton(name, section, subject):
    return name, section, subject 

Well, this is right! But not optimal. Not well designed. You can use dictionary in python to have that table structure that you are imagining.

mystudents = {}
def studentinformaton(name, section, subject):
    mystudents[name] = section, subject
    return mystudents

I don't want to extend giving examples but that if you want to code you need to understand and have your own use case. You are working and you have a project which gives you a sense of mentality and you think in terms of that use case. Now if you are confused in data structures just always remember that they are just mini databases i.e. array is a mini database lives till the function is running but its expensive to do certain things in array, well even that does not matter, that even is related to your use case e.g. if you want to save list of employee names then yes go ahead for array, but now if you want to add certain details to these employees then dictionary is a better choice. End of the day all is related to your own design.

Lets have another use case of atm withdraw function. Remember, you are writing the function so you design it.

  1. when the function is called do you want to ask for anything, any input you required?
  2. do you want to confirm anything, like balance i.e. customer can withdraw only if withdrawamount <= balance.
  3. You can keep going on and on as you design. Add anything you like no matter what, you are the writer of this function. You can even ask for customer pictures whenever money is withdrawn. You want to be extra cautious. You can ask password every time someone withdraw.

Conclusion: confusion and vague idea of use case causes frustration and that is why we just stare at the screen :). So go ahead and design a use case then start coding. You will see that as you keep writing you will have a feeling that YES code is easy.

Posted on by:

alokozai profile

Qaiss Alokozai

@alokozai

01100011 01110101 01110010 01101001 01101111 01110011 01101001 01110100 01111001 00100000

Discussion

pic
Editor guide
 

Please, what is the purpose of your examples?

 

Hi Pavel, well just writing my own hypothesis of how can we make things easier. Just to have a different look at writing code.

Please share your experience, it will help greatly as you have so much experience.

 

How to put it mildly. Have you ever heard of side effects? You code is littered with them.

I saw no return expression in your Python. You do not even shy away from global variables.
Basically, such a code is doomed to unpredictability and errors.