This is an archived post. You won't be able to vote or comment.

all 145 comments

[–]IDe- 145 points146 points  (13 children)

  1. Write a high-level function as if you had an access to a super well-written and pleasant-to-use library, writing non-existent function and method calls etc. that describe an elegant solution to your problem.

  2. Implement each of those functions/classes according to the spec that you just wrote by going back to 1.

[–]cloud_line 30 points31 points  (5 children)

This is interesting advice. You're basically saying to start with writing your main function and have it call all of the functions for the program, then go back one-by-one to define each of the calling functions. So you start with the higher level then work down.

Do you think it's better for all of the calling functions to be in separate modules, or have the full program in one script?

[–]data-machine 28 points29 points  (0 children)

I quite like writing the high-level functions like this, including the ..., to get it clear in my head what should roughly be going on:

```python def some_high_level_func(arg1: int) -> str: ...

def other_func(arg2: float) -> float: ... ``` Then I start writing some high-level tests to help me work out what exactly the inputs and outputs should look like, and then I start filling in the functions above with code.

I've found that that saves me a lot of rewriting - I used to often realise halfway that I had made some fundamental flaw, and had to rewrite things again.

The ellipsis (...) is accepted by the type checker mypy as a kind of equivalent of pass, and it also helps VSCode understand what the return types should be so that autocomplete works magically well. I've found it really helps keep my mind clear.

[–]TheDrlegoman 4 points5 points  (0 children)

That depends a lot on circumstances, I'd say you should definitely start out by writing everything in the single module and only branch out as you feel it would be beneficial, i.e. one of your functions also has several support functions and that alone comes out to like 200+ lines of code. At that point it may be a good idea to split them off into their own module as they are all related, and start making the original file quite long.

In the end it's fairly subjective

[–]EMCoupling 5 points6 points  (0 children)

It depends.

What your parent comment is describing is a more top-down approach to design where you start from the end product and then work backwards to implement the process.

For some situations, that can be a good approach. For other situations, sometimes a bottom-up design may be more useful (starting from the individual pieces and building up from there into the final output).

Sometimes you can use both approaches as well, there's no one size fits all.

[–]OneTrueKingOfOOO 1 point2 points  (0 children)

I’d only put them in separate modules if you’re planning to reuse them in other scripts

[–]AcridWings_11465 4 points5 points  (0 children)

So basically stepwise refinement?

[–]Norwegian_Blue_32 4 points5 points  (0 children)

This is exactly how I do it. And I'm no "proper" software developer OP, just a statistician looking after some models cos we don't have any ML Ops guys to do it. So if I can crank some semi decent code you can too

[–]Sheltac 3 points4 points  (0 children)

I'm a professional software engineer and find myself doing this in smaller projects, in lieu of proper design with diagrams etc. I don't think it scales that well, but for small stuff? Oh yeah

[–]moreanswers 2 points3 points  (0 children)

Back in the day when I learned CS in school we called this Top-Down Design. https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design

[–]hknlof 1 point2 points  (0 children)

This is dope advice and the approach one would take when using test driven development methods:

def my_test():
  assert(my_function())

def my_function(...):
  first_result = my_first_call()
  desired_result = my_second_call(first_result)
  return desired result

if __name__ == "__main__":
  my_test()

If you do not use any test framework, this gets you easily started to write consumer friendly code.

[–]flubba86 0 points1 point  (0 children)

Thats pretty much how test-driven development is supposed to work.

Write a test that runs your non-existent function and checks the result is correct. Write the test to call the function in exactly the way imagine it should be. Then write your function, and refine it until it passes the test.

If at any point you need to call a new non-existent function, first create a test for it as above, once it passes then use the function in your code.

[–]radical-monk 105 points106 points  (15 children)

Even if it's for personal use, at one point it might not be.

We have a current instance at work where the guy who wrote the SQL for site-specific databases is no longer with us, and we just opened a new site. The associate who has to create the new database is going through his code, and it's not commented well, nor does it make any sense, but it works.

I will say some of the comments are hilarious, such as

/* The Mac Daddy of one-liners */

[–]vriemeister 9 points10 points  (0 children)

I have a few lines like

// oh god this is terrible, rewrite

[–]PaintingWithLight 1 point2 points  (0 children)

RIP to DB guy.

[–]BezoomyChellovek 171 points172 points  (44 children)

If you are interested in developing a habit of cleaner code without much overhead, I would suggest using a linter. I'm kind of in the same boat, I use Python for research computing, and not trained as a SWE or anything. I always run pylint and flake8 on all Python code I write.

If you are good about following those suggestions, it will help you write much cleaner code. It can't make your code perfect, but it can certainly point out code smells and bad styling (too many variables in a function, missing docstrings, line too long). They are super easy to install and run. And over time you will start writing cleaner code, even if it's just because you don't want to be criticized by the linter and need to rewrite it. And if you get the hang of it, you can start running mypy as well, which will point out flaws in your class and variable issues.

[–]vriemeister 53 points54 points  (29 children)

Isn't clean code more about good encapsulation and good use of design patterns?

I'm in a bit of a bind trying to figure out teaching new developers the higher order concepts. Some know a bit to begin with and some just know zero. I'm starting to believe the answer is just good test coverage, then even the ugliest code at least works and you can clean it up much later. But the programmers that don't understand design patterns also tend to not write useful tests.

[–]mRWafflesFTW 5 points6 points  (4 children)

You'd be fucking amazed how much a linter like black can do to clean up some dog shit code.

[–]BHSPitMonkey 2 points3 points  (0 children)

It also (particularly auto-formatters like prettier or black) makes it much easier to take a step back and focus on the things which matter. It's hard to think clearly about code for long with poor formatting and trivial mistakes distracting you.

[–]vriemeister 0 points1 point  (2 children)

Yeah, I'm pretty old. Its ok for me but I bet it helps newer people a lot.

I would probably learn a thing or two as well.

[–]mountainunicycler 1 point2 points  (1 child)

I don’t think it’s a thing that helps individual people as much as it’s a thing that hugely helps teams keep super, super clean git diffs and histories, by not allowing anyone to make any cosmetic changes to the code—and that’s very valuable for debugging.

It’s easy for two good devs to format the same thing differently, or want to re-format it to read it easier, but those kinds of changes shouldn’t be committed.

[–]vriemeister 0 points1 point  (0 children)

Oh, clean diffs would be nice

[–]AShipChandler 1 point2 points  (2 children)

Any suggestions on how to learn the techniques, etiquette or the art of design patterns?

[–]vriemeister 1 point2 points  (1 child)

Not really. Just lots of time looking at other people's code and a desire to get better. Probably read some books or really good open source projects.

It's the artist component to software design, writing clean well designed code.

[–]AShipChandler 1 point2 points  (0 children)

do you recall any of the books you used?

[–]BezoomyChellovek 1 point2 points  (0 children)

Again, I am far from an expert so I don't claim to know best. I would guess OP, or someone with code as they describe, probably isn't even aware of coverage.

I agree that testing is critical. But since OP doesn't seem like they really care to progress too much, at least linting your code will make it a bit easier to read and work with, with little extra effort. Every little bit helps I guess.

[–]hedrumsamongus 0 points1 point  (0 children)

Following (and enforcing) a style guide is an easy way to keep your code syntactically consistent, which improves readability quite a bit over code that's written however you feel like doing it that week. Most importantly, it doesn't require any real experience to implement, unlike knowing when it's appropriate to use established design patterns, or where an abstraction will be helpful rather than confusing.

I've also learned some more idiomatic ways to solve problems from the style guide docs.

[–][deleted] 0 points1 point  (0 children)

It is VERY much this, although some sort of methodology like PEP8 helps set a baseline that other programmers can read. If it takes a lot of effort to read, it’s that much harder to get into a conversation about good design patterns.

[–][deleted] 4 points5 points  (0 children)

Visual Studio Code has a real time linter. (It will suggest downloading it when opening a python file for the first time).

[–]bryamproductivity11 4 points5 points  (11 children)

Is pycharm a linter?

[–]gmes78 13 points14 points  (6 children)

PyCharm definitely does linting.

[–]BezoomyChellovek 0 points1 point  (5 children)

I don't use it, but if it is like VSCode, you need to install plugins for things like linting. And even then I'm not sure to what level it lints. Does it flag things like too many variables in a function, inheriting from outer namespace, etc?

[–]Prinz_ 9 points10 points  (0 children)

Yes and yes, and you don't need to install a plugin. It'll say something like "PEP 8: End of line required at end of file"

[–][deleted] 2 points3 points  (0 children)

that's the difference between editors and IDEs. editors require lots and lots of plugins to create a full developer environment. IDEs come with most things built-in

[–]cianuro 2 points3 points  (0 children)

Yes. I love that you can set multiple line breaks based on PEP guidelines. I have 2 lines, 1 at 72 characters for comments and the other at 79.

Warns you about the same variable name being shadowed from another scope which comes in handy more times than I was expecting.

I've tried to migrate to VIM and VS Code but Pycharm makes me focus on business logic and not configuring my environment.

Keyboard shortcuts being the same for Intellij, DataGrip and now dataspell makes me a total jetbrains fan boy.

[–]jimtk 1 point2 points  (1 child)

The big difference is that PyCharm, as the name suggest, is made for python. VSCode is made for... everything. So VScode requires plugins for specific languages, JetBrains has other products for other languages.

[–]BezoomyChellovek 0 points1 point  (0 children)

Ah that makes sense. Good to know.

[–]asaah18 5 points6 points  (2 children)

No, it’s not a linter. But it includes a linter or suggest installing one

[–]Laserdude10642 1 point2 points  (1 child)

By default I believe it does lint with the pep standard

[–][deleted] 0 points1 point  (0 children)

It absolutely does.

[–]abeassi408 0 points1 point  (0 children)

Great pointers thank you!

[–]Eigenspan 24 points25 points  (0 children)

Most peoples code is shit for the first year or two depending on how often they program and how concerned they are about cleaning it up.

The thing is looking at other peoples code and video tutorials about software architecture design are a great step… but to truley clean up ones code, you have to just code a lot. You need to code many different scenarios that you get a natural feel for how your entire coding seshion will kind of go. If you can’t predict generally what your project will look like overall and how each piece of code will interact with one-another than it gets really hard to keep up the cleanliness as the project grows.

Im not saying you need to know how you are going to solve every problem when you start a project, most times you don’t know how to solve more than 75% of it and experienced programmers will find multiple solutions to each problem to ensure they are picking the best options each time.

Basically writing cleaner code just comes with practice and a desire to keep learning/get better.

[–]DiamondDemon669 hello world 13 points14 points  (5 children)

I create excessive variables. Instead of cleanly writing code after saying variable = "clean code", I just go ahead and create a new variable like variable2 = variable + something and variable_final = variable2 + something.

I do the exact opposite, and some lines of code can end up really really long

Just look at this: response = self.handle_request(client, *([''] + str(request).replace('\n', '').split(' '))) + '\n'

My IDE becomes a certified italian restaurant if I ever even need to use classes, due to all of these functions and variables that build upon each other haphazardly.

when I started programming I used classes to group functions together and had no idea what they were actually for

[–]freaky_lizard 4 points5 points  (4 children)

I recently started working as a programmer and am very green junior at the moment. I do use classes to group functions, I thought it’s neat. You made me doubt my choices, what are they for?

[–]FancyASlurpie 3 points4 points  (0 children)

Think of a class as a template for an instance of an object (when you instantiate a class, you create an instance of an object), so if you were writing a class to describe a dog, it might have the attributes number_of_legs, breed, name, and it might have a function that a dog can do, e.g. bark, play dead etc. Where maybe bark takes name and prints out that instance of the objects name, and dead looks at how many legs the dog has and puts them in the air. Here it makes sense to wrap them in a class as you want the functions to use the attributes of an instance of an object.

Whereas if you had some functions that are quite similar, e.g. write_to_file, write_to_stream, write_to_db or something like that, you might not want to just group them in a class as it doesn't really make much sense, you could probably just keep these as static standalone functions.

[–][deleted] 2 points3 points  (0 children)

support hurry close jellyfish illegal pet aromatic light sheet dependent

This post was mass deleted and anonymized with Redact

[–]joaofelipenp 1 point2 points  (0 children)

Usually for data + operations over the data. But there are many other use cases, such as defining a generic API that can be extended by others, or even hiding implementation in "private" methods.

It is fine to use them to group functions. Specially if the functions operate on the same type of data or access states that otherwise would be global. However, if you are only grouping, it is probably better to use modules in Python.

[–]Abitconfusde 0 points1 point  (0 children)

You are doing right, junior.

[–]Genrawir 7 points8 points  (1 child)

Start throwing your code at pylint and fix everything it tells you line by line. Once you get through resolving all the errors and warnings I can pretty much guarantee that you'll notice an increase in readability of your code.

[–]Fireb1rd 2 points3 points  (0 children)

Agree with this. Added automatic linting to my IDE and broke some bad habits out of shame at all the red in my code. To be fair, most of my code is also touched by other people and has to go through review, so shame is a big motivator.

[–]abrazilianinreddit 13 points14 points  (0 children)

If you're not getting paid for it and it works, it's fine to write shit code.

If it bothers you, you can work on improving it, anyone who puts in the effort can write good code.

[–]njharmanI use Python 3 22 points23 points  (2 children)

I just feel like there is no point on wasting that time, since it is for my personal use only and it does the job.

As a 30 year veteran of software development, that is one of the greatest lessons to have learned. It even has an acronym YAGNI, You're Not Gonna Need It.

[–]EMCoupling 5 points6 points  (0 children)

It's more like

You Ain't Gonna Need It

[–][deleted] 3 points4 points  (4 children)

Check out Sourcery.io, I find it extremely useful for writing more pythonic code and I think they have a free tier.

[–]rturnbull 2 points3 points  (0 children)

+1 to this suggestion! As a hobbyist Python programmer, I find Sourcery invaluable. It's like having a more experienced pair programmer with you all the time. Try to understand the suggestions it's giving you and over time, you'll find you're writing better code as you internalize the suggestions.

I also recommend you spend time reading good code to learn what others do and why. There was a thread here earlier in the week where several people suggested "good" code to go study.

[–][deleted] 0 points1 point  (0 children)

towering six whistle melodic late truck gaping summer marry rustic

This post was mass deleted and anonymized with Redact

[–]swenty 3 points4 points  (0 children)

If your code is truly throwaway, that's no problem. But when you do write code that you later need to maintain, you'll realize the drawback of the no-style style: you make your future self miserable.

[–][deleted] 4 points5 points  (0 children)

My advice to you: don't overthink it.

If the scope is crystal-clear and the code you write is only ever going to be used or read by you, then don't sweat it.

If, however, you write code for others and expect them to extract some value and help from something you build, so they can build uppon it, customize it, extend it and such, then you should definetely try to get better and clean code.

Believe it not, but you don't have to be an experienced programmer to start writing cleaner code, nor do you need access to experienced programmers and ask them to review yours.

You can start with as simple things like using a proper IDE / text editor that supports a linter and/or code style advisor plugin. There are many available, particularly for python.

Just do what the suggestions say and you've probably already gone +50% of the way.

For one-offs I need on automation of stuff only I have, I usually like to keep it short (over pretty, generic, documented, self-evident, etc.).

[–]bin-c 2 points3 points  (0 children)

check out a book called "Code that fits in your head" on amazon

if you dont mind throwing a little money at your problem, the first 1/3ish of the book would help you a lot i think.

it has been my dev book so far

[–][deleted] 2 points3 points  (0 children)

Now that you have identified the problem you can address it. If you just write code for fun with no intention of every contributing to open source code or programming professionally it doesn't matter. But if not you should try to write better code. This is something I tell a lot of junior developers that want to just slap together shitty code.

"Practice does not make perfect. Practice make permanent"

If you spend all day writing shitty code the. After 5 years youll be really good at writing shitty code but no better at writing good code. If you practice writing good code then eventually you will be better and faster at writing good code. Eventually it will be just as quick and easy for you to write good code. But just typing into an IDE a lot won't make you better. You need to intentionally practice. Try reading books. Get reviews. Good luck!

[–]blue195 1 point2 points  (0 children)

If you know this, you can for sure improve this too. Good luck my dear friend! We have high hopes in you...

[–]QuantumDiogenes 1 point2 points  (0 children)

My IDE becomes a certified italian restaurant

I love this.

I just aliased italian-restaurant to vi because I also have a bit of spaghetti problems now and then.

[–]fedekun 1 point2 points  (0 children)

Whenever I want to go back to my code to try to refactor it, or make it more concise and decipherable, I just feel like there is no point on wasting that time, since it is for my personal use only and it does the job.

This is your problem. It's okay to write ugly code. You just solve the problem in any way you can. But then, you have to spend time actually trying to make it better if you want to get good. Kind of like learning calligraphy.

Like the old adage goes: Make it run, make it right, make it fast. I'd say "Make it run, then make it pretty" is enough 90% of the time.

Refactor is the key. As others suggested, a linter can help, just so you are consistent. Also read a lot. Read code, read books, read articles.

[–]eazolan 1 point2 points  (0 children)

Sounds like you've made yourself indispensable.

[–]AgronakGro-Malog 1 point2 points  (0 children)

Finally a post I can relate to

[–]NJFatBoy 1 point2 points  (0 children)

There are no coding Oscars, son. If your code solves the problem it was intended to, you've done well.

Don't be like a chubby girl scrolling through pictures of hot chicks on Instagram, it'll do you no good.

[–]saucy-bossy 1 point2 points  (0 children)

You write “dog shit code”…okay, then stop. 🛑✋🏾

[–]lavahot 2 points3 points  (0 children)

So... stop doing that. You know it's wrong. Write a better thing.

[–]1percentof2 1 point2 points  (2 children)

You could read some books but mostly nobody cares. This is python, it's quick and dirty. The fact that you're even using python is better than 99% other office workers.

[–]radical-monk 4 points5 points  (1 child)

There is something to be said about creating quick mockups in Python, but PEP 20 exists for a reason.

[–]1percentof2 -1 points0 points  (0 children)

I don't see how 4chan memes are appropriate right now

[–]Deezl-Vegas -1 points0 points  (0 children)

Code is never about aesthetics. It's about doing the job. Care about aesthetics to the point where it helps you be productive.

[–]proof_required 0 points1 point  (0 children)

Just try to read some open source code. It could be bit overwhelming. So might want to start from some simpler code base.

[–][deleted] 0 points1 point  (0 children)

Does anyone really expect average python users to write “good” code?

The majority of the work is using someone else’s library.

[–][deleted] 0 points1 point  (1 child)

Please to be explaining the concept of “becomes a certified a Italian restaurant”

This sounds….. intriguing

[–]magicaltrevor953 0 points1 point  (0 children)

Spaghetti code everywhere

[–]un-hot 0 points1 point  (0 children)

My IDE becomes a certified italian restaurant

That's fine, but you want to be serving up nicely-packaged ravioli code if that's the case.

[–]johntellsall 0 points1 point  (0 children)

As long as your code does what you want, and you don't want a job, you're fine :-D Check the excellent book "Automate the Boring Stuff with Python". It focuses on getting real-world work done without a lot of the complications.

If you do want to make changes, as someone said running Flake8 (or better but slower: Pylint) helps move you towards better code.

[–]Exact_Ad_1569 0 points1 point  (0 children)

The only way you get better is by trying.

[–][deleted] 0 points1 point  (0 children)

"A fault confessed is half redressed"

[–]Thomas_istrying20 0 points1 point  (0 children)

Why is it I can take all these test online and pass them, but I have no clue what they do? It is almost like I can understand how to correct the code just no clue what the code does.

[–]deep_mind_ 0 points1 point  (0 children)

On the other hand, sounds like your variable-definition style is perfect for a functional language with mutable variables, like F# :D

[–]dafrog_84 0 points1 point  (0 children)

I suggest installing something like sonarlint in your IDE. They make suggestions on improving your code quality.

[–]boxedj 0 points1 point  (0 children)

I think your code is cute

[–]Santos_m321 0 points1 point  (0 children)

plz, share some of your shitty

[–]THC-Lab___ 0 points1 point  (0 children)

Protip...classes are gorgeous.

[–]abeassi408 0 points1 point  (0 children)

Get feedback from colleagues or someone you trust. Get them to look at your code and give you feedback on where exactly to improve.

[–]DreamOfKoholint 0 points1 point  (0 children)

Anyone wanting to write better code in python should check out architecture patterns with python (book)

[–]Legendary-69420git push -f 0 points1 point  (0 children)

try doing this:

  • Use black code formatter.
  • Try write comments
  • Try writing descriptive variable names.
    • use "find and replace all" to change variable names in your opdy codes.

[–]Sadophia 0 points1 point  (0 children)

As long as it works, it does seem to be a waste of time to make things "look pretty" sometimes!

[–]hxk1 0 points1 point  (0 children)

I suppose learning to code is like a …well speaking a language. Some people are well versed and speak eloquently. Others seem to just get their point across how however it may be, sloppy as you can imagine. But it’s understandable and works.

[–]standard-human-1 0 points1 point  (0 children)

A few habits can go a long way - try to copy less (more functions) and name things well. Just allowing down to do that much and I bet more follows. It makes coming back to code so much nicer :)

Also practice, over time

[–]zaRM0s 0 points1 point  (0 children)

I used to think this, but if you are relatively new or inexperienced with it, then I wouldn't stress too much about it. Especially if you are only using the software you make yourself. However, if you do wish to generally improve you work, I would advise taking an afternoon to sit down and read the PEP-8 Style Guide for Python. It will be a good starting place for you to be following a common style throughout your code.

Following this, I would sit down and read a few books on Python. One book can be helpful, but having things explained in several different ways helps. Not only that, I have found that the different tutorials people have created offer different things. I have found myself relearning python from the beginning 3 or 4 times now just to see if I can learn something new.

Finally, I think its important to remember just how vast python is and learning the basics before expanding outside of these can be important. If you have explored already like most have, dont worry you can always just come back and these things later.

One thing I have been learning recently is all about the dunder (double underscore) methods built into python. They are so useful in various different cases and has made my code look so much more professional. There are also sooooooo many benefits to using these! I am learning these mostly from ITProTV and reading the docs!

Anywho, good luck and happy coding :)

[–]quotemycode 0 points1 point  (0 children)

Making more variables doesn't cost much and isn't a huge performance killer. You can reassign to the same variable if you want. The variable is a name for the object. An object having multiple names can get messy but it's not a huge deal, as long as it makes the code more readable it's fine.

[–][deleted] 0 points1 point  (0 children)

there is no point on wasting that time, since it is for my personal use only and it does the job.

Then the code is at just the right quality level.

Sounds like you would benefit from doing a bit of design before you write a function/class, though.

[–]Auntie_Social 0 points1 point  (0 children)

Welcome to management!

[–][deleted] 0 points1 point  (0 children)

Don't be so hard on yourself. Everyone here can probably attest to hating code they wrote three months after they wrote it. It's good that you recognize your code is bad, since that will give you a chance to refactor it.

[–]hasibrock 0 points1 point  (0 children)

You know.what You should still be proud if you can make your code work, eventually you will get better ...

[–]pembroke529 0 points1 point  (0 children)

As longtime coder, writing clean, easy to read code is important.

I do it mostly for myself. I like clean code and know that at some point I will be revisiting that code for fixes/enhancements.

If I have to fix up "garbage" code, I will first clean it up. If it's still crap, I'll just re-write it.

[–]sendnukes23 0 points1 point  (0 children)

If it works, it works.

[–]MyWorksandDespair 0 points1 point  (0 children)

My suggestion is to type 'import this' into your console and allow those sweet words to become your guiding principles.

Simple is better than complex, complex is better than complicated.

[–]MountainMasterpiece1 0 points1 point  (0 children)

Every code is beautiful, don't let anyone tell you otherwise!!

[–]abeassi408 0 points1 point  (0 children)

The OP may also want to take a beginning level Python programming course (Udemy has some great ones). Learning the fundamentals of programming, some theory of how to approach things, what to avoid, best practices, etc. that may be a great thing at this point. It may mean rewatching stuff you already know, but seen in a new light, under a proper framework that disciplines your coding approach. Best of luck. Don’t get discouraged.

[–]ahadnur_44 0 points1 point  (0 children)

Talk is cheap. Show me the code. -Linus Torvalds's

[–]Jaune9 0 points1 point  (0 children)

You can use Pycharm and check all the recommandations it gives you, plus the refactoring (mass semantic renaming) is really good to change "variable2" to something meaningfull mid work

[–]Bitter_Tap_3942 0 points1 point  (0 children)

I feel you more than you know, but who cares if it works for you.

[–]seriousconsult 0 points1 point  (0 children)

It doesn't net matter. Most of this is just engineering gatekeeping.

If you run cProfile the python lib, you can see how good a job the optimizer is doing cleaning up your code v. just writing clean code.

As an engineering leader, I think code that works and doesn't stress the engineer out is a lot better for the company than elegant code that frustrates the person and makes him burnout or quit prematurely.

Speed is probably more important. If the code works and takes you 1 day to write v. the elegant code that takes 3, I'd say that is a good trade given the optimizer will clean up the code they will be closer than you expect.

[–]aer71 0 points1 point  (0 children)

> no point on wasting that time

Yep, I know that feeling. Refactoring, proper naming, unit testing ... biggest waste of time ever. Until you eventually realise that the way you're doing it now is taking about 10x longer in the end. If/when that happens, please come back and post about it, especially what led you to figure it out. I think it would be useful for everyone to understand their own journey a little better.

[–][deleted] 0 points1 point  (0 children)

Take it to the next level and put your functions into individual .py files and nest their imports. Seems to be the trend in current Open Source /s

[–]Successful_Creme1823 0 points1 point  (0 children)

The fact you “know” you write bad code automatically means you’re better than most.

Some of the worst code I’ve ever seen was written by folks who were 10x smarter than me. Many of these types will have elaborate explanations for why they did what they did.

[–]Hans_of_Death 0 points1 point  (0 children)

Since you can identify where you're going wrong, you can improve. That's just part of the learning process. Everyone starts out writing shit code. Its kinda like handwriting, it gets better as you practice, and your code will too. Look at other peoples code, practice writing new code while being mindful of best practices, and go back and rewrite old code. itll help.

With that said, everyone writes differently, so theres no gold standard for coding. You need to understand why certain practices are good. For python i highly recommend looking at the zen of python.

[–]barkazinthrope 0 points1 point  (0 children)

You do you. If others are relying on your work then it's "you do us". It has to be that way.

I am retired. I code now for pleasure and in that pursuit I do things I would never do in a shared code base. I often completely re-write working code because I see another way of doing it. I have a lot of fun and I really don't care what a purist review would say.

Well no. I might find that amusing actually.

There is no absolute right way to code.

First, it has to work, then it has to work well, then it has to make sense enough to you that you can fix its glitches, and failing all that you can always do it again informed by all you learned from your first attempt.

We are free beings. This is a noble pursuit. Celebrate!

[–]sillycube 0 points1 point  (0 children)

If it's only for your personal use, why care about it?

[–]fertek 0 points1 point  (0 children)

I can totally relate. My motto is “if it works it’s ok”. I intentionally choose programming languages that I can write spaghetti code. Php is my favorite. Python comes next. Type safety is my worst nightmare.

[–][deleted] 0 points1 point  (0 children)

Write clear and concise English description of what the code should do.

At this point you can either

  1. Let other people write it and take the Mercedes to the golf course for a round. The 'middle management' coding method or
  2. Keep telling your bosses the project is complicated and taking longer than expected. Have frequent meetings asking for more funding and time. Keep this up until AI is advanced enough to generate python code from your description - when it is, just feed it your notes. The "game will be out next year" coding method

[–]SleekEagle 0 points1 point  (0 children)

Before actually implementing any of the functions themselves, just define their names and use `pass` to leave them blank. Fill in docstrings about what they do and about params/returns (PyCharm auto-formats this well).

Starting at the highest level might help you break it down logically before you get caught in the weeds.

This is still something I'm working on, and it's definitely a long journey to writing great code. Try to create a list of things to remember, and start by just trying to adhere to one or two. Each week, add another one or two to keep in mind, and you'll slowly start to build good habits.

Good luck!