r/learnpython • u/brushygiraffe • Mar 30 '21
How are you ‘planning’ your code?
There’s a thousand videos on Youtube on how to code Python - but they seem to be more like function references - which you can look up in the documentation?
How you guys and girls think through what you’re about to write?
Should I write 90 lines of code then reorganise it into functions? Or start writing functions and then finally ‘use’ the functions at the bottom of your code/script?
Is there a best practice way of doing this? I’m trying to make it easier for myself so everything works like ‘lego’ - otherwise I find I get really confused
Should I write down the steps as comments and then fill in the code after?
‘’’ getjson() Function will accept json string and return TSV def getjson():
‘’’ getinputs() function will read argv[1:] and return the first parameter, or exit with error message if none specified def getinputs():
‘’’ writetosql() function will accept TSV and write it to final.db def writetosql():
‘’’ Run the functions inputs=getinputs() tsv=getjson(inputs) writetosql(tsv)
184
u/IlliterateJedi Mar 30 '21 edited Mar 30 '21
My step one is to open Notepad and write out what I need to do.
Process these 10,000 images to new image and file sizes with new file namesI try to set the specific outcome.
I then write out a rough guide of what steps I need to take and in what order to have a semi-'optimal' approach.
- Read in images
- Check dimensions
- Resize (if needed)
- Save new version
Once I have a rough outline, I'll translate that to what I expect my functions to be with type hints:
E.g.,:
def get_images(fp: pathlib.Path) -> Generator[Image]:
pass
def is_dim_exceeding_max(input: tuple[int, int])-> bool:
pass
def calculate_new_dims(dims: tuple[int, int]) -> tuple[int, int]:
pass
def resize_image(img: Image, dims: tuple[int, int]) -> Image:
passetc.
Depending on what I'm doing I may write out the code at this point in PyCharm or I may work through the steps in a Jupyter Notebook.
I also tend to write my 'tests' as I go under 'if name is main' then move those over to a test directory once things work. That way you can just run your tests as you modify things down the line.
37
u/Banishlight Mar 30 '21
This is a very good way to work through code. Coming from experience I recommend doing it this way. Start with your higher level concept and work your way down
16
u/ingwe13 Mar 30 '21
I’m still very much a novice, but this is the way I work. I think of it as taking passes at the program and each time you take a pass, your implementation gets more detail. The size of the project will affect how many passes are needed. Sometimes you have to back up if you can’t do something you believe you can (like a module doesn’t work the way you thought it did)
5
u/jerodg Mar 30 '21
Things like this seemed like a huge waste of time when I was a new code monkey. But looking back this is the same process I have developed in my mind and that I follow every time I write. The difference is now I don't have to write anything down because I have memorized my process.
I do on occasion make flow charts for complicated applications especially when orchestrating several different platforms at once. It helps keep track of the data flow. Also, management at any level can easily understand a flowchart but probably have no idea what you're saying when you explain it verbally because their brain screeches to a halt once you use terminology they aren't familiar with. It can be a big help when you're new or new to a team to help others understand that you actually do know what you're talking about/doing. But don't always do it unless you know it always gets you points with the boss.
So yeah, develop your process using pseudocode or paragraphs of text with explanations of what you want to set out to do. Refine it over time and when you've got something that works for you, you'll eventually commit it to memory. And your brain will automagically layout and organize a complex program for you before you even start.
Organizing into functions... We use functions/methods when that bit of code is or could be reusable. Creating a bunch of functions for a simple script might be more than needed. If your code gets more complex separating into functions can be helpful just to move a section of complex code out that seems to interfere with the logic.
e.g. I tend to keep my main method/class the holder of logic. It has just the steps that organize the flow of the program. If any parts of the flow become too large and start to muddy up the flow I pull that section out into a method. This allows any other person to look at your code and see right away what your code is doing without getting bogged down in how it's doing it. This works great when jumping into someone else's complex program as it allows you to find the section you need to make changes to more quickly.
2
1
u/coconutxyz Mar 30 '21
I just watched the video thinking like a programmer and this should be the best way
1
u/PanTheRiceMan Mar 30 '21
Doing the same. Breaking down your task into simpler, more explicit formulations definitely helped me making projects manageable.
1
u/gootecks Mar 30 '21
In what situations would you use PyCharm vs. Jupyter?
3
u/IlliterateJedi Mar 30 '21
Technically I do all of it in pycharn, but I run a jupyter instance through the project I'm working on when I'm working. Usually I use jupyter when I'm dealing with Pandas and need to see data displayed readily as I work. If I'm doing actual algorithm writing I'll use pycharn so I can more easily step through the code as I go.
1
1
u/MisterMoo22 Mar 31 '21
I am just starting to learn this so thank you for walking through your normal steps.
34
Mar 30 '21
Weeks of coding can save you from hours of planning.
I never sit down to code unless I've got a pretty good idea as to what I'm going to do. Even if it's an experiment or tinkering. Having clear goals up front is critical. That means thinking through your project, writing notes, saying it out loud, and scribbling diagrams.
Then there are principles to follow. Functions should do one (and ONLY one) thing, and they should do it well. Functions are like digital tools. You have a hammer to drive nails and a saw to cut wood. You don't have one tool to do everything.
For more principles look up PEP8
1
u/TSPhoenix Mar 31 '21
Agreed, but for tasks you've never done before you don't want to fall victim to the planning fallacy, which is where the version of you who has no idea what they're doing is the one who writes the plan.
Sometimes it is better to just have a rudimentary plan, see what goes wrong, then write a new plan when you actually understand what you're doing.
If you're really a beginner, sometimes it can be better to try and fail, I've seen beginners just sit there planning and basically avoid writing code because they think if they sit there long enough they'll eventually figure it out when sometimes you just gotta get your hands dirty.
-5
u/Ran4 Mar 30 '21 edited Mar 30 '21
Functions should do one (and ONLY one) thing, and they should do it well.
People keep repeating this, but it's so obviously false.
Any useful program must do more than one single operation, thus there must be multiple functions and there must be at least one function that call those functions, and that function thus does more than one thing.
It's much better to understand that a function should contain related code, and if there are parts of a function's body that's not at all related to the rest of the function body, then that function should probably be split up (a good function length is typically around 1-25 lines of code).
9
3
u/duffer_dev Mar 30 '21
It depends on how you define the "one thing". 'process_image' should only process the image. Maybe if you want to draw stuff on it, you could have 'draw(text, rect)'. Which internally could have separate calls for drawing text and drawing rectangle. At all levels, it's doing the one thing it was 'defined' to do
2
u/RazzleStorm Mar 30 '21
That line seems similar to the S in SOLID principles, which IMO is a better way to put it.
0
14
u/fergal-dude Mar 30 '21
I learned a lot about planning my code from taking a single ‘Talk Python To Me’ course. He shows you how to plan and structure code well. Something lacking in most youtuber tutorials.
Also, the more you code the less you’ll have to comment. Comments are for when the code isn’t obvious, the great thing about python is that the code is obvious most of the time.
I came from an html/css background where you use big honking comments to section out your code, I still kinda miss that.
4
u/inglandation Mar 30 '21
Which course exactly?
4
u/fergal-dude Mar 30 '21
I've done quite a few, but Python Jumpstart was my first. Choose one you are interested in, if it's Michael Kennedy that's the presenter, you'll be fine. (not that the others are bad, it's just that I've only taken his)
1
u/inglandation Mar 30 '21
Thanks. I don't think I'll start with a beginner course, but they offer a few more advanced course that I'll probably buy.
1
u/ScotchLeSaint Mar 31 '21
Python Jumpstart
Is this the course you're talking about?
1
u/fergal-dude Mar 31 '21
You bet, that's the first one of his I've taken. I've picked up others cheap in humble bundles, I think he's great for learning how to structure files and build things for scalability.
7
Mar 30 '21 edited Mar 30 '21
What works for me is I tend to write down on paper a high level overview of how the program will work.
When writing the code, I put some info at the start of a function to tell me some useful stuff like
def get_some_shit(a,b):
'''
Descr: This function takes two integers and adds them together
Parameters:
a -> int
b -> int
Return:
dict
{'final': a+b, 'other': 'test123'}
'''
c = {'final': a+b, 'other': 'test123'}
return cNot sure if it's just a facet of my plugin for VSCode but when typing the function name, it then shows me the information detailed at the function header for quick reference
You can always specify the param type in Python3 for function args such as:
def get_some_shit(a: int, b: int):
passBut Python2 does not support, for ease I use my method to remove the chance that it won't work on Python2
7
u/Ran4 Mar 30 '21
for ease I use my method to remove the chance that it won't work on Python2
Supporting Python2 is almost always a bad idea today.
1
Mar 30 '21
As far as I'm aware there's no harm in doing so
1
Mar 31 '21
The harm is you lose the potential benefits of type hinting (and other features not supported in 2)
11
u/brews Mar 30 '21
A bit more advanced but it might be worth checking out Test-Driven Development.
2
u/baubleglue Mar 30 '21
Yes, probably it is not for beginners. I've tried it for my first big project, completely broke my code to make it testable. I think when the code is already generally structured, then you may give it a shot.
2
u/ffrkAnonymous Mar 30 '21
I'm learning ttd and the more I practice, the more I feel it should be taught to beginners, those with short simple code, not starting with big projects. Literally, test does the code output "Hello world".
2
u/DoozerMarch Mar 30 '21
There is a real knack to writing tests. When I started out they often got in the way when I wanted to change code structure. This still happens sometimes but it seems like less with experience. Keeping them really thin and simple seems to help. Avoiding the temptation to write integration tests muddled along side unit tests seems to be key. The mock library is good for this.
2
u/ffrkAnonymous Mar 30 '21
I've become quite a fan of ttd Https://Obeythetestinggoat.com
Im still a beginner, but it's helped me to organize, each "step" is a test (expected failure), and helps me extend my code. I can put in placeholder code and replace it without worry.
At the very least, if nothing else , I have documented code that explains and show what each function does.
I'm practicing TDD on advent of code and I have no idea what I'm doing, especially the latter exercises. ttd (and version control) let's me experiment.
1
u/brews Mar 30 '21
Yeah, +1 for the goat.
I think the testing goat guys have a pretty good book on python architecture patterns that came out this year or last year.
4
u/GarbageCleric Mar 30 '21
I'll write an outline in psuedocode and then that will often provide a reasonable structure for the different functions that I need to create.
2
4
u/LeoJweda_ Mar 30 '21 edited Mar 30 '21
Perhaps I can shed some light on this as a professional developer who’s dabbled with making a Python Snake game tutorial and planning a hangman solver one.
When I’m just developing regularly, my personal style is to write it all in one place then extract functions but, in the back of my mind, I know which parts I’ll extract into functions. The reason I don’t do it from the beginning is that the added mental load of managing parameters is too much and breaks my concentration.
For the two above, I put extra care into code organization both because they’ll be open source that future employers will see but, also, because I’ll need to explain them and, if they’re too messy, they’ll be hard to understand. But, let me tell you, they did NOT start that clean.
One thing I’ll mention is that, if you want to use Test-Driven Development (TDD), you’ll have to plan our your code ahead of time and smaller functions are easier to test.
At the end of the day, do what works best for you. I wish I could do it right from the beginning because extracting functions later is slower. If you can do it in one sweep, go for it. There’s no right answer here, honestly.
4
u/duffer_dev Mar 30 '21 edited Mar 30 '21
Over the years I've developed this procedure when starting with a new module or project. I call it the "Top-Down" approach or main first approach. This is very similar to what most of the other comments have suggested.
I first start with the main file that is going to be executed. It helps to organize the code organically. Once you have the main written with essential setups or function call, I then implement the internal functions/classes. I avoid the temptation of implementing internal functions, classes initially. Just create an empty template
Always have the end result in your mind. A few question that you should ask yourself
How is this module/project going to be used
How is it going to executed/deployed
What are the command line arguments, configurable parameters i want in my module
Start with a template setup.py and an empty start start script with just a main function in it. For e.g , If i am building an application that fetches some data from a website, processes it and then stores the processed data , the code would be
python data_processor.py --url www.reddit.com --interval 30I've added an additional parameter just for demonstration purpose. If I want these parameter in a config file, I'll create a config file, say config.json
{
'url' : 'www.reddit.com,
'interval' : 30.0
}My code would then run like
python data_processor.py --config config.jsonThis already gives me an idea of what the command line arguments would be. I then implement the main file . which could look something like this
from argparse import ArgumentParser
import json
def main(config):
...
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('-c', '--config') # and some other params
parser.add_argument('-u', '--url')
args = parser.parser_args()
with open(args.config) as f:
config = json.load(f)
main(config) I have not implemented the main function yet, but i already have a working code for running and testing my script. From here on, start implementing how the execution would proceed. For example you want to parse some data and store it in a file or database. The main function can thus look like
def main(config):
url = config['url']
db_connector = DataBaseHandler()
data = get_data(url)
processed_data = process_data(data)
db_connector.store(processed_data)Even at this point, the functions are not implemented. But the design of the code is getting created on it's own with this "top-down" approach. I could have empty functions in a separate file which look something like this. Lets call it, data_handler.py
class DataBaseHandler:
def __init__(self):
...
def store(self, data):
...
def get_data(url):
...
def process_data(url):
...I can still run my code and test it. Although nothing happens, the steps are becoming more and more clear. If I want to just test a small function in a separate file, i add an if __name__ == "__main__" block in that file and call that function with some predefined parameters. for e.g the file data_handler.py can have something like this
if __name__ == "__main__":
data = "some data"
processed = process_data(data)
print(processed)You can the run just the data_handler.py to test how this function is behaving.
The top down approach builds your code is a more systematic manner. Also, the imports and parameters are added consistently. You also reduce going back and forth between function calls and modifying parameters and other parts of implementation to fit the function caller. This is a very basic approach that i follow for my work
1
u/backtickbot Mar 30 '21
1
u/fergal-dude Mar 30 '21
This is a really well written into. You've got to have a bit of intermediate knowledge to know what it means, but you've got me googling config files now. I've seen yaml config files before, but I didn't understand them.
Can you explain this line please?
python data_processor.py --url www.reddit.com --interval 30I'm assuming this begins a python file (data_processor), but what purpose do the --url and --interval serve? are they command line commands?
Thanks for any help understanding this.
2
u/duffer_dev Mar 30 '21
These are command line arguments for the code. Think of them as input parameters for the script that you can pass when running the code. The library ArgumentParser gives a convenient way of adding such arguments with flags in python
1
u/fergal-dude Mar 30 '21
Interesting, so you are literally filling in variables with those flags when you run the program?
1
u/duffer_dev Mar 30 '21
Kind of. Think something like sys.argv, but with more options. For example if I want to process another website's data, I can simple change the parameter, '--url' with the website I want. That way , I don't have to change the code. It's like passing variable into a function. Only this time, you are passing variable to your script from the command line
I've updated the code to include the URL parameter
3
u/99OG121314 Mar 30 '21
I tend to write down on paper what steps I need to take/new ideas alongside my code. Sometimes I just do markdowns in Jupyter notebook but I find pen and paper helps me organise my structure/ new insights better.
3
u/Farmher315 Mar 30 '21
I've been learning with ATBS, and in there he teaches you to start out by writing comments that explain the different steps you need to do to get to your end goal. Then go back and fill in the code for each task. It's helped me a lot but I haven't done any major projects outside of the book just yet.
3
u/cryptickoan Mar 30 '21
theres this book called how to design programs, you can find it here --> https://htdp.org/from-first-to-second-edition.html It goes through a bunch of techniques to plan code based on the problems nature. Its quite long but i think its amazing and can save you a lot of time
5
u/Noli420 Mar 30 '21
I tend to work inside out, starting with something small and building on it. When I get stuck, I sketch it out using a pseudo-code type thing... I will lay it out like normal loops, but just wrote in English what I intend. Then work out the syntax one I actually write the code
4
u/ivanoski-007 Mar 30 '21
I just wing it and get a general idea in my head start typing ah put a bunch of hashtags with comments everywhere.
2
u/chatziiola Mar 30 '21
This is a magnificent thread idea, at least from my point of view. Having spent a lot of time working on projects with different languages (for example, I hadn't studied python at all, yet I developed a handy script to upload my uni pdfs to Google drive in less than 1 hour), I am a strong believer than you should first take 5 minutes without writing anything on your computer but simply making a template. Of course, it could be just me that does this the old-fashioned way (with a pen and piece of paper) and you could, instead, utilize any fancy (or not) modern tool you can find, if that is what makes you feel most productive and effective. After that you should devote another 5-10 minutes to think of the implementation of each "step" you wrote down in the previous step. If you do so, you will not write so much code, but it will be on point right from the beginning and, something that I find beneficial when the time comes for documentation, you have in your hands the algorithm used in your application, meaning that you do not need to overthink what you just typed: Your code will be beautifully commented and you could just write in a concise matter your notes in your program's docs.
P.S. I think that the "timeframe" I gave can easily be misunderstood. Take the time needed to nail a specific goal and then simply move on.
2
u/PB_Dendras Mar 30 '21
download pycharm and use it's TODO function (which is assigned to a specific point in your code). Works for me.
2
u/baubleglue Mar 30 '21
You aren't finding videos about planning code because it is not a Python question. If you search "how to design program" instead of "how to code Python" you will get results (but still probably not the results you are looking for). It is a huge topic. It will be impossible to cover it in single answer.
You can start from https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design - that is related to your question.
> Should I write 90 lines of code then reorganize it into functions?
Not a good idea. 90 lines is a lot of code - you already put some logic to make it working.
> Should I write down the steps as comments and then fill in the code after?
that is better approach, I do it sometimes when I have many steps
> Should I write down the steps as comments and then fill in the code after?
That is bottom-up design - very valid method, but the way you described it is not correct: "getjson() Function will accept json string and return TSV". Look Java's interface concept and how it is done
interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
} Python has type annotation now - you can use it
def greeting(name: str) -> str:
return 'Hello ' + namebut most important, name inputs and outputs
def getjson(data_as_json: str) -> str:
'''
Function will accept json string and return TSV
'''
passNow about the names - common beginners' mistake. "getjson" is a bad name - really bad. My English is not good, but as I understand "get json" should return json and get it from somewhere - "convert_json_to_tsv" is a better name but still probably is not good. Why it is not good? Can it convert any JSON to TSV? You probably writing code for some specific reason "convert_[user_statistics_api_data]_json_to_tsv" - something like that:
class tsv(str): pass
class json(str): pass
def convert_user_stats_api_json_data_to_tsv(data: json) -> tsv:
passThere is a concept of domain specific language, if you think of coding as process building a new vocabulary, that may simplify your task a lot. There is a lot of very important things to consider like I will just put links:
https://en.wikipedia.org/wiki/Separation_of_concerns
https://en.wikipedia.org/wiki/Coupling_(computer_programming)
https://en.wikipedia.org/wiki/Cohesion_(computer_science)
https://en.wikipedia.org/wiki/Side_effect_(computer_science)
but using domain specific language (DSL) is a good shortcut to start with.
It is a bit different when you design your code top-down, but it mostly applicable for bigger code base (I think). I personally combine methods:
When stuck and not sure where to start - write few lines of working code, make them a function. Repeat for some other tasks.
When it starts to be clear what is end-to-end flow - make POC (working code).
Then make top-down thinking: define components (ex. parser, api, configuration, main_app) .
Then - rewrite code. Ex.
split methods to relevant modules/classes
parametrize all hard-coded values, add them to configuration
move ALL global variables to configuration
make main_app to operate only DSL
1
2
u/eliasliavaag Mar 30 '21
I consider myself as a programming novice, but, inspired from my background in Systems Engineering, I prefer to follow a top-down approach. I usually start by defining the functions I want and just populate them with pass statements, then I start working on them one-by-one. I think this is beneficial since it allows me to capture the scope of my work before I start doing it.
2
u/dogs_like_me Mar 30 '21
Describe what Im trying to accomplish in plain English
Write down a short description of each step in the process
Transfer the list of steps to my script as comnents. Use this as an outline, fill in the gaps between comments with code.
Delete comments if the code is self explanatory, add comments as I realize missing details I want to take care of.
2
u/Hikaru2000 Mar 30 '21
Step 1: Any note taking software (or pen and paper, whichever you prefer) and write the problem asked and then refine it into a workable problem statement.
Step 2: List down the steps or processes required to achieve the goal.
Step 3: Write a rough pseudo-code (it can be as simple as just going into the details of the steps or as much as just noting the various functions you'd use at various steps)
Step 4: Start writing your code (this part depends on the programmer themselves). I usually just list down all the functions I need with initial parameters and outputs, then go to the main code. As I keep adding to the main code, my functions may change (I work with datasets for a Pattern Recognition course and this happens at times). So I wait before writing any function. At this stage, I also import any libraries etc that are required.
Step 5: I split the code into sections (separate them based on the steps in Step 2). As I finish writing one section and verify there's nothing more to be added there, I write the functions used in that part. Similarly for all sections. Then I run and debug the code section-wise (Jupyter Notebook makes this easy).
Step 6: (This is the most important step) Once it works properly, I stay away from it and don't touch it at all. (Just ensure it works properly, make it readable, add comments if necessary. Ensure that if you look at the code at a later time, you should understand what you are doing).
2
u/SnooLentils1243 Mar 30 '21
use flowcharts
1
u/OrangeLegoBrick Mar 30 '21
flowcharts and pseudo-code .
example.
input name
input age
input heightconvert inputs to list
2
u/jasonkunda Mar 30 '21
I usually make a to do list and order the things I need to do in chronological order and then just treat it like a bunch of small questions and I figure out how I make it so the code is more readable.
2
u/RcNorth Mar 30 '21
Write down in simple steps what the program needs to do i.e.
Open CSV file
read through file and process each row
for each row insert record into SQLite database
etc.
Once you know what the program needs to do You can determine what needs to happen at each step i.e. how do I get the name of the file to process? (start with it hard coded then move to command line parameter or something else later)
Opening the file doesn’t need it’s own function as it is a single line. If processing each row is going to be complex move It to a function, if it is simple maybe a For loop is enough.
Start with displaying the results of each step print commands or looking at variable values in the editor. Only move to the next step when you are good with the process so far.
I’ve started with things in a for loop then had to move them to a function later to make the code easier to read or to provide better error handling than I thought I was going to need at the start.
2
u/memes-of-awesome Mar 30 '21
Semi- Intermediate python coder here. I would suggest having a general idea of the structure of any script you're about to write before starting. If you realise that your script is flawed, change it instead of trying to fix what you've already written with even more complex code. ive fallen in that trap and it gets REALLY hairy if you're not careful.
2
u/ItsmeFizzy97 Mar 30 '21
I use a sheet of paper and a pen. I start writing details about what my program wants to do, then I break it into smaller tasks that can be achieved through simple functions. A UML schema might help at the beginning, even if it isn't accurate or you won't follow it through the end
2
u/ciskoh3 Mar 30 '21
So I recently got to second place in Hackathon also thanks to the fact that, despite most of us being data scientist and newbies, we had a clear project structure and could organise well.
And I'll tell you my secret: I got a premade folder structure (in my case from cookiecutter) and just fucking sticked to it!
For the first time I didn't invent weird helpers, structures or classes and I trusted what is generally accepted as a good structure: and guess what? it worked! Admittedly the app is far from finished but we had exactly 0 problems figuring out how the modules could interact and so on !
Every module has max a couple of wrapping functions that are taken up by main.py, no interaction between packages, clear names for folders and a good readme.
I cannot believe how many weeks I have spent reinventing the wheel and making a mess out of it.
2
2
u/smurpau Mar 30 '21
Unpopular opinion that thankfully someone reputable espouses (Raymond Hettinger): "The first mistake Python developers make is starting their code with def". Over-planning, trying to imagine all the function arguments (and their types) and returns etc. is usually a waste of time at best. Just solve the atomic problem, messily, prove it with a test, and then refactor in waves while maintaining passing tests as you deploy increasingly convoluted levels of fancy abstraction.
1
u/temisola1 Mar 30 '21
There are different ways I approach it.
Sometimes I write pseudo code... I know exactly what I wanna do and how I wanna do it, so I’ll put the functions down along with whatever parameters I might need then fill in the details later.
Other time I just start writing, and I refactor as I go. I start in the main def, and when I come across a snippet of code that I would most likely need to reuse the functionality I’ll turn it into a function. And also if I come across something that behaves like a physical property I’ll turn it into a class. Sometimes I decide I might need this object/functionality in other files, so I’ll convert it to a module.
1
Mar 30 '21 edited Mar 30 '21
use pseudocode (google it) e.g.
REPEAT
OUTPUT 'What is the best subject you take?'
INPUT user inputs the best subject they take
STORE the user's input in the answer variable
IF answer = 'Computer Science' THEN
OUTPUT 'Of course it is!'
ELSE
OUTPUT 'Try again!'
UNTIL answer = 'Computer Science'Basically, this is a simplified version of your intent representing a logical workflow - notice it can be converted into any programming language, Java, C, Python
You plan your approach before writing any code.
A similar concept is called "requirements engineering" which a whole discipline in itself... finding out what the actual requirements are BEFORE coding anything - even clients sometimes don't know what they require e.g. attributed to Henry Ford - "if I asked what people wanted, they'd have said faster horses"
2
u/baubleglue Mar 30 '21
That is procedural approach, if you have pure flow - it helps a lot, but you can't build a project that way. Just introduce to your example few more things to consider for answer as
user.topic.competency_level, user.age, user.occupationit may branch to so many directions.
1
u/brigitvanloggem Mar 30 '21
It doesn’t matter! Just start! Really! Eventually, you will settle on a style, an approach, a method that’s right for you. In the meantime, for god’s sake, just start!!!
1
u/Cayde-6699 Mar 30 '21
Usually before I start a project I think about what that project is like my blackjack project the first I knew I had to do was define the cards so I used OOP which simplified that a lot and then I thought about how blackjack works dealing player cards,dealing dealer cards sum up the hand totals ,and who wins based on hand totals. After I basically divided up how the game works I worked 1 at a time making a function to do them
1
u/jaycrest3m20 Mar 30 '21
Pre-optimization is a programming sin.
Generally, at the beginning, you are gonna throw down 90 lines of code, and then start organizing. After enough experience, you are going to recognize patterns and pre-plan the code for those patterns.
Writing down comments for functions you need and how they should work is a really good idea, I think. Feel free to change your mind part-way through the code if some other design pattern emerges.
Avoid having a function that does too much. For example, instead of having a single, monolithic function that reads from a file and writes to a database, make two functions that each do one job, then make a third function that simply calls those two other functions. In this way, things start building up like Lego blocks. This way, your third function has an option of calling different read and write functions, depending on what is needed, freeing you up to think about more important things.
1
u/menge101 import functools Mar 30 '21
In my junior days, this is what TDD helped me greatly with.
The answers to your questions all depend on how much code there will be and your personal tolerance for scrolling through code.
Similarly, you didn't mention creating classes for your domain objects, that too is up to you if or when you want to do it.
I personally break things down early, I'm happier with more files than code in a given file.
I create an entry point into my code in a top level file that will be named for the the domain, or potentially generically something like app.py.
That is the file that gets run, and it includes everything it needs from modules in a ~/lib folder.
1
Mar 30 '21
I basically plan my code on the fly, I start to code, walk around the room thinking about the code and then code it and repeat. If it's an algorithm that I have to implement I either use a pen and paper to see the concept and draw a diagram or I just use my imagination or I use both.
"Should I write 90 lines of code then reorganize it into functions? Or start writing functions and then finally ‘use’ the functions at the bottom of your code/script?"
Ideally you should think about how you're gonna code before you do it
1
u/DrMaxwellEdison Django and stuff Mar 30 '21
Big libraries that everyone uses with complex algorithms and data structures were not written top to bottom that way. We build what works with good intentions, we find flaws in our designs, and we iterate.
If I have a script that does a sequence of events one time only, that can stand on its own.
If I find I need the functionality of that script somewhere else in a program, I wrap the logic into a function and call it. The script can still work as it did, and I can reuse that effort.
If the same function needs to be used in a slightly different manner, add some optional arguments, adjust the logic so it can do both, and keep it going. Write automated tests to ensure that logic works both ways so you have confidence in the results, then move on.
If I find I'm passing around a lot of common data, or there's a complex set of arguments that usually go together, I might then design a class to contain that data and related logic.
Sometimes I'll start diving into a problem that's secondary to the problem I want to solve and then I have to ask "did someone else already solve this?" Then it's time to go searching for helper libraries, SO answers, etc., to speed up the process. Even when those solutions are too generalized or not a perfect fit, I can adjust it to fit my needs.
So, how do we think through what we're about to write? Honestly, that's a trick question, because there is no "about" to write: I just start writing it, knowing I can refactor it into something workable later.
1
Mar 30 '21
I normally write comments out of what I have to do in order of how they need to be done. Once I have a rough outline its pretty easy to fill in the specifics
1
u/MightbeWillSmith Mar 30 '21
I usually start with a list of #TODOs. For a new project it is a couple bullet points.
TODO get data from warehouse
TODO convert x,y,z to time series
TODO get correlations between a,b,c
Or something like that. Then within each of those, create subheadings of the specific step.
TODO get data from warehouse
#TODO get x,y,z from table j, dates 01/2020 - 04/2020 When I have it mostly mapped out. I get to work. If I notice there's something I'm repeating several times, I'll write a function to clean it up. Although that might be in later stages after I have something that works as is.
1
Mar 30 '21
I usually make the simplest version I can think of then start polishing and making it fancy. It takes a long time but hey I’m still a newbie. Yesterday I used coded some stuff just for some answers as a glorified calculator. That code wasn’t commented or nice to look at, but it worked. I wouldn’t do that if it’s meant to be seen by someone else though.
1
1
u/umognog Mar 30 '21
I scanned through and didn't see a single offering of this;
UML
Start with your use case diagrams, use that to generate your activity diagram and from there class/state diagrams as necessary.
Usually these are enough for most projects and you can get pretty efficient at creating them during requirements gathering.
1
u/smurpau Mar 30 '21
I don't know anyone who uses UML anymore. It costs more in overhead than it saves.
1
u/umognog Mar 30 '21
I'd disagree. I can sit in a meeting, the notes taken are in UML and immediately afterwards the team can start dev with a structure available, no further discussion is needed.
That same UML can be understood by less technical purse holders without creating new proposals and write up especially for them and easily used for progress updates without having to write anything new for this purpose either.
That said, I am open to learning better ways; what is your suggestion that can fulfil these requirements in the same or less time?
1
u/Invictus996 Mar 30 '21
Take time a create a proper flow of logic top to bottom This will help you : 1.find places to improve logic 2. Spot any redundancy and create functions (never code the same thing twice is possible) 3. Also, it works as a great bookmark, if you wanna take a break ;)
1
u/iggy555 Mar 30 '21
Analyze the problem
Spec the solution (use ipo method) Input Process Output
Write pseudo code
Write real code
Test
Refactor
1
Mar 30 '21
I started working on an Instagram bot with selenium, first the .py file dedicated to the bot object and its methods and functions, for example I set the 1st init method that initializes the bot with username and password as arguments, then I started the Log method, which is dedicated only to log into IG, other methods will follow and they will be dedicated to follow people and comment, etc. and each has its own mission.
1
1
u/gdledsan Mar 30 '21
What I do is start writing, then realize some bit can be a function, so I take it out.
The more you do this, you pavlov yourself into writing the functions first.
Most of the time you have defined actions you want to happens so those start as functions already.
You can also diagram your idea first to get something defined from the start, the more you do this, you pavlov yourself into thinking like that too.
What I am saying is, start, practice, you'lll get better
1
u/dieth Mar 30 '21
Check out PEP8 or it's song
Usually I write some big terrible monolith, and then chop it up into happy functions.
1
u/spudzo Mar 30 '21
I don't have a ton of time to go into detail, but I often start off just trying to break down a problem to its components. Usually I'll also go about making some UML diagrams. Its very nice to have a flow chart that goes all the way from program input to output.
Planning along with some other skills like teamwork and work management are what separates software engineers from programmers.
1
u/thedjotaku djotaku Mar 30 '21
Depends how rigorous you want to be. And the scope of your project. The "best" way is to write your unit tests first and then write code that passes those tests. When all tests pass, you're done. And you know everything will work well if you wrote good tests. I've done some projects like that and it can be satisfying to see your tests pass one-by-one as you code your solution.
But, most of the time, I just have an idea of what I want to do (I need to automate signing up for a spot at the gym, I need to automate backups, I need to generate graphs of focal lengths of my photos) and then I start coding. Once I have something that works, I start refactoring. That is, now that I know how it works, I start making it "good", "elegant" code. Maybe I realized there's something I need to do in more than one place, so I make that into a function. Maybe I made something into a class, but that's too complex so I take it out of the class. Or as I add type annotations I realize I was being too lazy with my inputs or something. The longer I've been coding in Python the less of these refactors I need to make when I'm done.
1
u/Poddster Mar 30 '21
I do all of those options.
Whatever we did best for the ideas I have.
Most of all, however, I rely on just typing in random crap / psuedocode and then making actual python code to make it work
1
Mar 30 '21
Open a notebook ( physical or some app like evernote, onenote, etc.) and write down your targets and requirements. Follow that up with a flowchart of how you want to proceed with the program. Go back to the notebook and start writing on how the steps in the flowchart will be actually done (some pseudocode). Return to the flowchart if necessary and keep going through this loop until you are quite certain what your code should look like. I don't think it is a good idea to have to think something when you are typing your code unless absolutely necessary (even then I feel that you should go back to the notebook and sort out everything before getting back to typing your code).
1
u/rufaloinn Mar 30 '21
I plan things on paper first. Helps me to understand the dependencies and the flow of work: what do I need to do before I do this etc...
Sounds simple but I like having the paper because the sketches me to understand previous versions of the workflow that I had in mind as well.
In any case: plan before you code, it cuts the time you need to code a lot!!
1
u/POGtastic itertools evangelist Mar 30 '21
I tend to do "bottom-up" programming, writing a bunch of basic functions and then combining them or reorganizing them as necessary. This is less likely to work with larger applications, but the vast majority of the work I do in Python is scripting work.
1
1
1
u/bsmdphdjd Mar 31 '21
I write a series of comments at the top of the file, outlining what I want to do. Then I duplicate it.
Then I write the code to do it directly beneath the duplicate comment, leaving the original list at the top, to later let me jump to the respective code section below.
I also try to make the program syntactically correct and executable even when it's still mostly null. That way I can use the interpreter to check for errors as I add code.
1
u/SOG_clearbell Mar 31 '21
Think about what you need to do and break it into smaller steps. Then write pseudocode and make it organized
1
1
u/_G0D_M0DE_ Mar 31 '21
How I start depends on the paradigm I have chosen (or one that has been chosen for me), whether its procedural (imperative), object-oriented, functional, or even combination of paradigms. At work, I'm mainly operating under OOP requirements. So my code is compartmentalized into classes and instantiated into objects. This can get rather complicated because it involves design patterns.
For beginning programmers, most simple programs are mostly going to involve procedural programming. And in that case, I would think about the logically separable components of the program and abstract those out to be functions. The most abstract and simple way to consider procedural programs are in terms of IPO (Input, Processing, and Output). You will have a portion of your code gather input from a client/user. That data is then processed into information based on some business rule or algorithm, and then the results are presented to the client/user. Client or "user" could be an actual person entering in data through a console or GUI interface, from a file, or even from another program (see UNIX philosophy).
Python makes it really simple to produce function stubs thus allowing you to fill in the details of those implementations later. And this is very helpful when employing an incremental and iterative development process. I would definitely make use of that feature. For example:
def someFunction(x,y):
passAnd at the top-level you simple put in a function call when necessary. And the name of the function should reflect the isolated logic you want peformed.
Good luck!
-10
69
u/IDontLikeBeingRight Mar 30 '21
Start by doing whatever workflow you want. But be aware of things that aren't going well, and what you could change, and why any particular element of any best practice might be there.
Don't expect gold from your first programs, and expect to have a lot more to learn the first time you share a project rather than just coding by yourself. Even start a project in a deliberately different way just because, to see where which things go wrong. (Uh, maybe don't collaborate for that one). Don't get attached to any particular project (also: have projects that you can avoid being attached to), learn from the mistakes & inefficiencies, and try to understand the best practices rather than simply implementing them.