Programing Tips

From OpenCircuits
Jump to navigation Jump to search

These are my personal tips for programming, they work for me, not sure all programmers will endorse. Some of these tips overlap with each other, hope this is not confusing.

Copy Work Is Good[edit]

This is key, if you can copy code instead of writing it you can save a whole lot of work. There is nothing wrong with this given that you understand the code that you copy. I copy mostly from myself, I actually keep little pieces of code around to copy from or go into my collection of past projects. This may also give you code that works sooner. If not from yourself then use google. This is not like getting homework from another person in the class ( unless that is what you are actually doing ). The point is to get good code working and be able to do it again in the future. One note about copied code: If you copy code, you own it; that is you must take full responsibility for it. Using code you do not understand makes you a script kitty.

Start Small and Expand[edit]

You will typically break a complicated task down into steps. It is a good idea to program and test each individually and then put the pieces together. Each time through the code should get bigger ( functionally ) and better. Some components, like error management, I sometimes leave until the basic code is running. There is a whole philosophy of system development where successive improvement of code is a chore idea.

Put Working Components Together[edit]

This is part of the idea of Start Small and Expand, see above.

Learn More Python[edit]

From time to time make an effort to learn. Pick up a Python book, read a blog, actually read the documentation. Python is big and getting to some of the advanced topics can take a long time. Any time your coding seems tedious, stop and question your approach. Learn a bit more and the task may not be so tedious.


Test your code. Writing special sections of code just for test is a great idea. Learn about the whole concept of unit testing. Google "Python Unit Testing"

Keep Track Of Your Own Code[edit]

Since you want to copy from your own code, and build on old projects, keep your code organized.

Study from Time to Time[edit]

There is a lot to learn, from time to time look for online resources to study. I have been doing this for years, still have a lot to learn. Many habits from other languages are not so good in Python which has its own way, some very nice, of doing things.

Google Is Your Friend[edit]

Got a Python problem. I can almost always find an answer on Google ( or Bing..... ). Usually the process it quick. Use It.

Get Used to Writing Good Comments[edit]

There is a lot to say about good comments. I will only say a small amount here.

  • Comments should tell the intent or purpose of the code. Do not tell what the code does, your reader ( usually you ) can read the code.
  • Comments form the internal documentation of the code. There are some Python standards. I should put a link here......
  • Comments sometimes warn about something unusual in the code.
  • Comments are a good place to keep track of enhancements that might be made in the code.

Use Libraries[edit]

If you are trying to do something big and complicated, something that is not too far off the beaten track, someone may have written a library for it. Want a web server, there are a bunch of libraries for it. Images, yep there is stuff out there. Look before you code up a storm.

Good Coding is Good[edit]

Writing bad code in a hurry will often bite you. Write good code as much of the time as you can. May well be faster in the long run. Please use good names, make it a habit.

Lots of people use 2.7, this includes me although I have recently added 3.6 to my environment ( you can have multiple environments on one computer ). But when you program try to be as 3.6 compliant as possible. Some of this includes:

  • Always inherit classes from object.
  • Use the new style print as function. Use from __future__ import print_function if < 3 to enable this.

Do Not Invent Stuff Unless Necessary[edit]

I developed a nice little logging system. But Python has one ( several ). Try to use the standards that are already out there.


We have more in the area of tips, look at the following: