Showing posts with label Computer Programming. Show all posts
Showing posts with label Computer Programming. Show all posts

Tuesday, February 13, 2024

Writing Your First Computer Program

I have someone that I am working with and we need to write a complex computer program. We have settled on Python as the computer language and we get together frequently to work on the code together. While I have done some programming using Python, I am by no means an expert. Basically I know enough to be dangerous.

If I had 30 years slashed from my age and just started writing software, I would start with Python as it is such a good language to do a lot with using a minimal amount of code. That makes the programs easy to write and easy to understand. The only problem is that there are quite a few libraries to learn. Without the help of the Internet to help you look up possible solutions, doing anything useful in Python might not be so simple.

My coworker and I have a rather complex problem we are trying to solve but have broken it down into very simple pieces. The first thing we want to do is take an e-mail address and modify it. It didn't take that long to write the code. Next we expanded our problem so that we passed in multiple e-mail addresses and did the same modification to all of them. Then we worked on saving the results into a database. We have continued adding pieces to our code to ultimately get the program we need.

Our code writing is an iterative approach. We focus on one task and make sure the task is completed as designed before moving onto the next item. When we got the code modifying e-mail addresses and writing them into the database, my coworker felt a huge sense of accomplishment and thought we could be done. Today I reminded him that there are still a few more steps we need to do before we can say we have completed everything.

Should you decide to give coding a try. Here are these simple steps put into an easy-to-follow outline:

  1. Write down what you want your program to do. This is often called a specification or spec.
  2. Break the task down into very small steps.
  3. Write the code for each step in the process and test it to make sure it works before moving to the next step.
  4. When you think you are done, go back to your specification to make sure you have completed what you said you would.
Interestingly enough, these are the same steps to follow in trying to accomplish any goal you have. They work for more than just writing computer software.

Tuesday, August 20, 2019

Everyone Should Learn Python

If someone who wanted to learn a computer programming language asked me which one to learn, right now I would recommend Python. Back when dirt was new, dinosaurs roamed the earth, and I first started learning how to program a computer, BASIC was the language everyone learned. Then I moved onto Pascal before tackling C and C++. Nowadays I find that most of the coding I do is using Python and I love it.

I am currently working on a project where I need to display the size of a file on a web page. Most languages would require me to use IF..THEN logic show GB for gigabytes, MB for megabytes, or KB for  kilobytes. In Python, I just need to use the "filesizeformat" filter. It automatically converts the number 2600000000 into 2.4 GB (don't worry about it not being 2.6 GB as 1 KB = 1024 Bytes, not 1000).

Python is a very compact language and doesn't require a lot of coding to get a lot done. Chances are that someone has already figured out how to do something that used to require thousands of lines of code and reduced it to a single line in your program. The only problem with that is you need to know what line to use. That is an easy-enough problem to solve though. Just type a short description of what you are trying to do into your favorite search engine and you will get a number of suggestions. Most of the time one of them will work perfectly.

Python also has the advantage that is the computer programming language of choice for data science and data analytics, which is what I do a lot of. For a while, people used R but now Python has overtaken it.

Yes I still remember BASIC and Pascal really helped me understand a lot of fundamentals for computer programming. But if you are starting out on the path of learning your first programming language, my recommendation would be Python.

Friday, January 22, 2016

No Instant Experts

What do playing the guitar and skiing have in common? Both require many hours of practice to become proficient. This is something I didn't understand growing up. Fortunately being a beginner skier can still be a lot of fun. When I would take guitar lessons as a child or teenager, I would practice for about a month or two and then get frustrated that I didn't know very much. This time I am making the process of learning the guitar a lot of fun and I look forward to practice every day.

So what does any of that have to do with computers and technology? The same rule of practice applies. If you want to get good, then you need to spend some time with the technology. The trick is how to make it fun. Each of us is different and so one solution might not work for everyone.

I once read about someone that wanted to learn how to program computers with a new language. He loaded his computer into his camper and headed into the woods for a week. He then spent one uninterrupted week of running through Donald Knuth's Fundamental Algorithms and turning out computer programs. My wife would never let me get away with that but I always thought that would be the perfect way to learn a new programming language.

I find that the right motivation helps people learn new things as well. I often try to marry learning tasks with something something I really want to do. Back in 2004 I had wanted to learn more about the PostgreSQL database system. I was also part owner in a restaurant and heard a great presentation about customer loyalty systems. I thought it would be fun to use PostgreSQL to help me write a web-based customer loyalty program. So I sat down and designed a system to keep track of customers and reward them with points every time they made a purchase. My program never got used but I did learn a lot about PostgreSQL and made a career of it for quite a while.

If you find yourself frustrated with your computer, just remember that there are no instant experts. It takes time to learn a lot of this stuff. Search engines are your friend and there may be someone who has already solved the problem you are having. If that doesn't work, post your question on a forum and a number of experts may be able to help you out. Who knows. One of them may have learned how to solve your problem by spending a week camping in the woods.

Wednesday, October 22, 2014

Programming Languages

My oldest son is back in college and currently working on a really long programming assignment. The course requires him to use the Java programming language and he is not very happy about it. At the beginning of the project, he was told by the teaching assistants that his code would approach 5,000 lines of code and take 3 weeks to write. Part of the reason for the class is to show the flexibility of Java. My son doesn't see it that way. His preference would have been to use Erlang. He could do everything the 5,000-line Java program does with 25 to 30 lines in Erlang.

This brings up the topic of programming languages. Not all of them are created equal. If you want to write something that runs very fast and only needs to run on one platform, do it in Assembly. If you need something to run as fast as possible but it may run on a number of different operating system (i.e. Linux, Windows, or Mac), then use C. If you are working with statistics then R is your best choice. A good software developer will know the benefits of a number of different languages and apply the best one for the problem at hand.

Does that mean that every software developer should know every language out there? No, but he or she should be familiar with a number of different languages along with the strengths and weaknesses of each. Software developers should also be experimenting with new languages when time permits and not rely on one language to do everything. After all, 30 lines in one language is a whole lot less work than 5,000 even if you have to learn a new language. The 30 lines of code is also a lot easier to maintain.

Wednesday, January 22, 2014

A Little Coding

It has been a while since I have done any real computer programming. Sure I work on a computer all day and spend a lot of time writing SQL statements but I don't really count SQL as a programming language. I figure I have been getting a little rusty and so I have taken it upon myself to do a little coding every evening when I get back to the boat. When I am at home in Utah, there is too much to do each evening. When I am on the boat in California, there is nothing to do but watch television. I figure it is a good time to refresh my programming skills.

It is best to start with what I already know and so I have a simple example C program that converts IP addresses into integers. An IP address is simply 4 numbers separated by a dot. My IP address is something like "101.102.103.104". This can be converted into an integer simply by breaking the numbers up and thinking of them in binary format (that will be in 1's and 0's). If you take 101 as a decimal number and shift it to the left by 24 bits, add it to 102 shifted to the left by 16 bits, add it to 103 shifted to the left by 8 bits, and add it to 104, you are left with an unsigned 32-bit integer that is the equivalent of your IP address. This is a number between 0 and 4,294,967,295.

Now the question you need to ask is why? Isn't it easier to remember 101.102.103.104 than some seemingly random integer? Yes, but it also takes more space. The IP address requires up to 16 bytes when left as a string. The unsigned integer only requires 4 bytes.  With computers that have gigabytes of memory, does 12 bytes really matter? Yes, when you are trying to remember 90 million IP addresses. 4 times 90 million is only 360 megabytes of memory where 16 times 90 million is well over a gigabyte.

My little program is working in the C programming language because that is what I am most familiar with. Now my goal is to rewrite it in a number of other languages so I can become familiar with them. When I am finished, I want to compare execution times and see which is fastest. I also want to see which one is easiest to understand, easiest to code, and fewest number of lines. It may sound like a boring exercise but my hope is that it will help improve my coding skills at the same time I learn about a number of new languages. It also has a practical application to a problem I am trying to solve at work and so that makes it significantly more interesting.

Friday, June 28, 2013

Summer Intern

I managed to get my oldest son a summer intern position where I work. When I was going to college, I never thought about working as an intern. I was too busy trying to make money to pay for school. It turns out, you can actually get paid for some internships. That is the case with my son and he is really happy to be working with Dad as he thinks I work at cool place. It is hard to tell him that this is work when he has managed to get a whole bunch of free swag, leading-edge hi-tech toys, and invited to a number of after-dinner parties. It is a good thing he is staying in the Bay Area this weekend or he might go home to Utah and tell everyone that we play more than we work.

I have found that it is really difficult to break into the video game industry. Everyone is reluctant to hire people from outside the relatively small industry. I still don't know how I found myself where I am, but I am glad to be where I am. An internship is a great way to get started. There are a number of kids that come back after graduating from college.

So what does an intern do? My son has spent a lot of time doing on-the-job training. While he has experience with a fair number of programming languages, he needs to dive deeper into the one he will be using this summer. He is also working with a specific framework and is running through a number of various examples before he gets his proverbial hands wet. Next week that will happen. The idea is to have him complete it before going back to school. That way, we get something from his experience and he gets the experience of seeing something go from prototype to production.

If you are young and want to work in video games, you should definitely think about applying for an internship. The competition is tough as my son was selected from a pile of 51 other applicants. However it is possible to land a really sweet summer job.

Monday, June 27, 2011

Relearning C

The first computer language I learned was BASIC and I was in the 8th grade. It was fun to get the computer to do various things but I really enjoyed my next class which taught me Pascal. When I got to college, I learned a few more computer programing languages including C. I would have to say that C remains as my favorite language for a number of reasons.

Recently I started a new personal project and have chosen to do it in C. I thought about using something a little more modern like Python or Perl but need something that compiles to an incredibly fast program. The tradeoff is that I will have to spend more time writing code. It also means I will have to relearn many of the things I have forgotten about C.

C is one of those languages that has a lot of flexibility but also has a lot of pitfalls. Today I was working with pointers and made a minor mistake on the syntax. Because pointers are so flexible, the syntax was correct, I just wasn't getting the results I was expecting. I used an Internet search engine to look up various examples with no success. It turns out, I needed to include some parenthesis around one small section of code. I came up with the solution after thinking about the results I was getting for a while. Then things started working correctly.

Some might be discouraged by such an experience, but not me. I just realized that I need to do some relearning and that is one of the reasons I started this project. It is my understanding that colleges and universities have moved towards teaching Java instead of C and I think that is a mistake. If it was up to me, everyone would learn C as their first computer programming language. There is something about having to work hard to get your program running. It has a bit of a learning curve, but allows you to write complex programs that run really fast.

Friday, May 21, 2010

Learning Python

One of the nice things about my new job is that I get time set aside each day to learn a new skill. Most of the developers I work with here are using Python and so I figured I would learn this computer programming language. What better way to learn than to work on an actual project. One of my coworkers was more than happy to volunteer a project and after a week, I am mostly finished. There is still some clean-up work to do, but the program works.

I do have to confess that I am not any better at knowing Python now than when I started the project. My program interacts with so many other pieces of the system that I feel like I am working with Lego blocks. When I need to figure out how to interact with one system, I do a search on the Internet, find some sample code, and work it into my program. I know just enough to make changes so that it works. When I get errors, I search the Internet and get an immediate fix.

Today I have a code review and I have to explain how my code works and why I wrote it the way I did. The only problem is that I have no idea what I did. The program just works and does what it is supposed to do. Oh well, maybe I will learn Python next week.

Wednesday, December 2, 2009

Flow Proofs and SSIS

Yesterday I was finishing up a SQL Server Integration Studio (SSIS) project and my youngest son came into my office. He is taking Geometry and mentioned that my computer program looked like a "flow proof." I thought his comment was interesting because computer programming reminds me a lot of proofs from back when I took geometry.

In computer programming, you have a starting point and a final ending. The idea is to get from the start to the end using a limited number of simple steps. That sounds a lot like geometry proofs to me. Unfortunately nobody ever has any fond memories of geometry class. So when I explain the similarities between coding and proofs, it turns people off from wanting to be a computer programmer.

Last night when I told my son that my project was a lot like flow proofs, he laughed at me, called me a nerd, and headed up to dinner. I followed him and thought about what makes coding more rewarding than math homework. While it is nice to get paid for work, there is more to it than just money. I think there is a sense of satisfaction when you build something and computer programming gives you that chance.

I also thought about how much computer programming and math are alike. Before colleges and universities had dedicated computer programming majors, it was the electrical engineers that built the computers but the math majors that would often end up programming them. Even now, most comptuer programming majors are an extension of the math department. My own Dad was a math major and his first job out of college was as a computer programmer.

This afternoon I thought about the relationship between math and software and came to the conclusion that the guys that designed the SSIS really had fun in geometry class. However I'm not sure it is a good method to try and get the computer to do what I want.

Wednesday, September 30, 2009

Geometry Homework

Last night my youngest son came and asked me to help him with his geometry homework. As part of the coursework required for my degree in electrical engineering I managed to pick up a minor in mathematics. I also have a minor in physics but that really wasn't important for last night's assignment.

Geometry is one of those topics that you really only experience at one time during your path to greater mathematical understanding. While you may continue to use one or two geometric formulas, the bulk of geometry is so different from all other topics that I was concerned I wouldn't be able to help my son.

He brought down his homework and we looked at it together. There were three proofs. His whole assignment was three problems that took up a single piece of paper. The first two problems had the hard part done. It was left to my son to label each of the steps required to go from "given" to "proof." He had done a fairly good job at guessing the labels. However the third and final problem required him to develop the steps on his own and that really had him confused.

I pulled out a scratch piece of paper and started working out the proof myself. Half a sheet of paper later, I had gone in a circle and was no closer to a solution than a monkey with a dart. So I put down my pencil and thought about the problem. Geometry proofs are like driving a car. You have a starting point and know your ending point. It is then up to you to use a series of theorems and postulates like roads to reach your destination. While it is possible to get in your car and drive around until you find your destination, a lot of gas can be wasted. It is much more efficient to map out a direct path.

I picked up my pencil and noted a couple of things that would take me from the start to the end as quickly as possible. The whole problem took about three steps and I was done. Then I could explain the problem to my son and help him get the answer on his own.

Even though it has been almost 30 years since I had geometry, I was still able to help my son with his homework. It isn't because I have a great memory and can still remember back that far. It is because geometric proofs teach you how to use simple steps to solve larger problems. That is all computer programming is. Since I have been programming for over 20 years, the skills I learned back in high-school have constantly been used and I was able to help my son. I guess I'd better apologize to my old math teacher from when I said, "I'll never use this."