r/learnpython • • Feb 13 '19

Some Lessons from 16+ Years of Development

Inspired by /u/ skiutoss 's post, I thought I'd add my own lessons.

Some other people probably got more from their years than I did - I started young(12-14) and I'm stubborn. But I somehow managed to bootstrap a 25+ person company, and have avoided working for anyone but myself and a few clients since I was 15. The only office I've really set foot in is the one I ran. Here are some things I've learned.

Most of this is related to SAAS services written in python.

  • Your best programs and projects will be solving problems that you yourself have. If you're lucky, other people will have that problem too. If you're really lucky, they'll pay for your solution.

    • Your best libraries will be solving problems you have while programming. Write them, learn them, love them.

  • Your past work is your best resource.

    • Because of how you code, you're likely to encounter similar issues. Standing examples of "how I did this" are tremendous resources.

    • Especially if you're a contract worker, developing your own helper libraries that each contract explicitly gives you the rights to (I offer a transferable license to my prior work, and myself ownership) is worth it's weight in gold. It grows and grows and grows.

  • Don't re-invent the wheel, but don't use a dumptruck to move a toothpick just because it exists.

  • Coding structure (classes, inheritance, etc) are not for your software as-is, it's for what it will become.

    • You will always be hung from your largest monolithic function.

    • When one function is made to do X and you already have a worse function to do X, the old function is to be deleted immediately.

  • Misleading variable names are to be exterminated on sight.

  • Consistent variable names will save you huge amounts of time, reduce bugs, and save time for coders you onboard.

    • Example: product_title in the function that determines the title, product_title in the dict it writes to a queue, product_title in the database, product_title in the json from the ajax call from the website, product_title as the javascript variable storing it on the site.

  • Every piece of code should be written with a thought to how hard it will be to replace some day.

    • This is where well defined objects, microservices, and redis queues of tasks/objects shine.

  • If you can't code to scale(because time constraints), code to buy yourself time to do so in the future.

    • As an example: If you are directly writing to a SQL database and it has the potential to be slow in the future, write the needed data to a redis queue and have workers execute it. When it starts to get hairy you can tick up the number of threads while you figure out how to speed up the DB or migrate to a new one.

  • "Clever" code should be readable. If it's not, it's a detriment not a benefit. Coding is not your opportunity to show how much smarter you are than everyone else(who will have to maintain your shit)

  • No, you won't remember. TODO lists and comments are vital.

  • It is rare that you have a legitimate reason to be handwriting SQL queries.

  • You will always need a dev environment. Develop scripts for setting them up.

  • One of the greatest skills a programmer can have(especially one who works on early stage start-ups) is figuring out which corners can and can't be cut, and setting up the project to be easily fixed in the future.

  • The less billing code you are writing, the better.

    • Significant issues in billing and backups are unforgivable tier errors. Clients and users will forgive downtime, they will not forgive their card being billed at random.

    • There are companies who handle things like subscriptions. Use them. Do not write your own.

  • Don't just have backups, have an environment for testing those backups. Know how you're going to pause new incoming data when they're applied. Have a system that blows your phone the fuck up if they fail.

    • In many cases, a failed backup is a company-ender. It's that serious.

    • A master/slave configuration is not a backup. It will save you from hard drives roasting, not a sloppy "UPDATE" query.

    • Come to terms with the mortality of your hardware.

  • Do not trust user input. Not their cookie, not their form input, and never their uploads. Javascript validation is for telling the user their input is wrong, not for keeping your data clean. That is done server side.

614 Upvotes

124 comments sorted by

View all comments

107

u/otfjokes Feb 13 '19

No, you won't remember. TODO lists and comments are vital.

Having to go and fix a bug on an old file... "Oh man, this code is trash! I don't understand any of this, who wrote this garbage!!! ....Ohh wait, I wrote this. Damn. What was I thinking!?!? And why are there no comments!?"

Great list! Thank you!

23

u/[deleted] Feb 13 '19

If you aren't using pycharm (or another IDE with TODO lists built in), do yourself a favor and switch to one!

22

u/Ran4 Feb 13 '19

cat **/*.py | grep TODO works just fine. No need for an IDE :)

7

u/guess_ill_try Feb 14 '19 edited Feb 14 '19

Yes but a good IDE highlights todos nice and bright and even has a tool tab to show you where each one is.

Don't get me wrong, you'll never take my terminal away!

6

u/joetinnyspace Feb 14 '19 edited Feb 14 '19

is there an extension for VS coders ?

edit : Look for 'TODO Tree' on the Extension marketplace

5

u/acousticpants Feb 14 '19

and to save yourself a piping cat:
grep -rnw . -e TODO

→ More replies (5)

4

u/[deleted] Feb 14 '19 edited Nov 11 '24

disgusted pot offer vanish carpenter teeny pie act price abundant

This post was mass deleted and anonymized with Redact

→ More replies (1)

5

u/otfjokes Feb 13 '19

For sure! I know better now. I had a time developing in PHP for about 2 years where EVERYTHING had to be done yesterday. When I started time for comments and TODOs were sacrificed to get "just working" code. When i started having to go back to fix things I quickly realized that sacrificing comments and TODOs certainly does NOT save time.

3

u/bitcoin-dude Feb 13 '19

Great recommendation, I just downloaded TODO Tree for VScode

1

u/darez00 definitely not a programmer Feb 14 '19

Are TODO lists just comments defining something that has to be done or what are they exactly?

→ More replies (1)
→ More replies (1)

1

u/[deleted] Feb 13 '19 edited Feb 14 '19

On the other hand most code should be readable without comments. If you like you need comments to understand your old code that's a sign that it should be written differently.

You can downvote me if you want but that accomplishes nothing. I've thought a lot about this based on pretty extensive experience. So if you disagree explain why, and I'd be happy to explain further.

18

u/otfjokes Feb 13 '19

Code that I wrote my first couple of years ABSOLUTELY should have been written differently. No arguments there. Code I write now could undoubtedly be written better...

But even if there was such a thing as perfect code, comments are still going to help. I stand by it.

3

u/Conrad_noble Noble1988 Feb 13 '19

As a none coder the only pipedream I have of a "perfect code/language" is one written in plain English.

Something that will likely never happen or at least in my lifetime.

15

u/RandomPantsAppear Feb 13 '19

As a none coder the only pipedream I have of a "perfect code/language" is one written in plain English.

Funny, to me python felt kind of like that. I was pretty stoked to be able to use "is" and "in" and "not", and even more stoked to not have my code peppered with alien curly braces and semi colons.

To me it feels like I'm writing english.

13

u/Ran4 Feb 13 '19

Once you've spent some more time coding, you'll likely think that code written in english would be terrible.

There's so much ambiguities in natural languages as opposed to programming languages.

5

u/Dorito_Troll Feb 14 '19

I mean thats pretty much python

3

u/otfjokes Feb 13 '19

Isn't that what lawyers do? Write and decipher the logic of "plain" English?

It would be nice to write JUST the comments and the code works. Not quite as fun though.

3

u/rhytnen Feb 14 '19

No - these are literally polar opposites. Lawyers exist to handle ambiguity. Programming languages don't want to have ambiguous language (not the same as confusing language).

I don't want my code compiled by a lawyer in the same way I don't want to read a novel written by a compiler.

3

u/otfjokes Feb 14 '19

I think we are saying the same thing, I just worded it poorly. We might need a lawyer to sort this out.

and I think python is as close to programming in plain English as we want to get.

Wife to program her husband: honey, can you go to the store and get a gallon of milk and if they have eggs get a dozen. Husband comes home with a dozen gallons of milk and no eggs.

→ More replies (1)

2

u/[deleted] Feb 14 '19

Sure you're entitled to your opinion. I'm not going to downvote you for it. Software engineering is never a solved problem, and there's always going to be different views on a topic.

Personally I find if Python code is well written (meaning it is well structured, well formatted, makes intelligent use of whitespace, has good variable and function names, uses type annotations) I have zero problem figuring out what it does without any additional comments. Reading code fluently in the language of their choosing is a skill every developer should strive to have. Not to be snarky, but if someone has trouble doing this, I would say they should read more code. Just like if they have trouble reading English they should read more books.

In my experience comments are more effective if they're used sparingly. One downside is they clutter and decrease readability of code. Another downside is a lot of comments are usually not as well maintained as the rest of the code. If somebody knew comes and makes edits to your code base there's a good chance they won't update your comments. This leads to inaccurate comments that may confuse future readers.

Comments should be used when a portion of your code is unavoidably obtuse. If it's implementing some complicated algorithm or business logic, by all means you should comment it. If your code looks like something like this

def foo(x):
	pass

def bar():
	
	#output is the output of bar
	outputs = []

	#Loop through integers 0 to 99, apply foo, then append 
	#results to outputs list
	for x in range(100):
		outputs.append(foo(x))

	return outputs

Just get ride of the comments. Your code is more understandable without them.

5

u/otfjokes Feb 14 '19

You make very valid points an I agree. There is certainly a balance with comments, just like anything. Your example is better without comments for a professional project, but also an example of GOOD comments on a basic tutorial.

One thing to keep in mind, I think, is that other people are probably going to be looking at your code, and trying to use it, and not all those people are going to have the same competency as you. So not only do well placed comments help those people with their task, but your comments also help them learn how the code works so they can eventually be less dependent on comments.

Reading code fluently IS a skill every developer should strive for, but it's not an instant thing and comments help them become more fluent.

→ More replies (1)

2

u/Bruska Feb 14 '19

I'm not even a programmer and I know not to comment code like that. It should be something about what you're trying to achieve, not writing a how-to python book. So the comment in your example should be

/#do this specific thing to the input so it is in the correct format for some other thing later

If you need to update the comments whenever you change your function then you're probably falling into that trap.

2

u/[deleted] Feb 14 '19

For the most part variable and function names should be adequate to describe what you're trying to achieve. If I slightly modify the above example you probably won't have much trouble figuring out what it does

def append_suffix(input_str, suffix):
    return input_str + suffix

def append_suffix_to_list_of_strings(list_of_strings):	
    strings_with_suffixes = []

    for s in list_of_strings:
        strings_with_suffixes.append( append_suffix(s) )

    return strings_with_suffixes

2

u/Bruska Feb 14 '19

In that case I would personally have a comment that explains why we need to add the suffix. If not in the function itself then at least when the function is called - why am I appending the suffix.

→ More replies (1)
→ More replies (1)

2

u/Amygdala_MD Feb 14 '19

Most definitely agree to this sentiment. In my opinion commenting is not supposed to be there for legibility of code. If you need comments to explain WHAT your code does, rewrite the code.

And most certainly avoid comments along the lines of:

Declaring variable x as integer

Function to add x to y

Etcetera.

Comments should be there to explain WHY the code is there. For example to explain your matrix mathematics are there to compute your camera angle based on mouse input if you are for example creating a videogame.

Comments also serve a purpose to notify yourself or another user of the reasons on as to why you used a possibly non conventional way of coding something.

Comments for todo purposes speaks for itself. But what I personally also like to do is comment in resources to solutions. If I struggled on a concept and found an answer to it via e.g. stackoverflow I tend to comment in the link. This way, when re-reading my own code later on it is immediately clear where to find a more thorough explanation.

→ More replies (12)