The Hackety Blog!

Planning Your Program

Posted August 02, 2012 at 2:02 PM

(this article is aimed at the very new programmer, just looking at programming and not sure where to start)

One of the most important things I learned about programming is that the computers that run the code will do exactly what you say - not what you mean, but what you say (or code). Computers are very simple translators of the code you write into actions and if those actions are incomplete or not precise then the computer could do something unexpected, wrong, or possibly destructive.

To help you make a good, useful program it is vital that you plan it. The simplest way, and I think the best, of starting a program is to get some paper and a pencil and plan it on a page. Whiteboards are nice too. Don't sit at the keyboard until you have some plan of what you will do. If you want to think of it as an engineering problem, then you don't build a bridge until you have figured out how long it will be, what materials it will be made from, and how many cars (or people, or foxes) it will carry at one time.

I recently made a small function to help me pad out strings (all sorts of numbers and letters) to certain lengths with zeros (0) - for example to pad '123' to 6 characters would become '000123'.

This problem might be put this way in words:

When a string of characters is shorter than the required length, then add some zeros to the 'front' (left) of the string, to bring it to the required length.

So from that description, we need to know what the initial string is and the required length, and find some way of sticking zeros onto it.

--written out on paper--

inputs: initial string and required length

process:

while the required length is more than the string length

add a zero to the front of the result

repeat the while check

output: display the result

Now, step through the logic on the page with some values - you can write the values next to the code as you go, whatever helps you. Try some weird cases like Zero, -2, 1000000, bacon

While you are thinking about the inputs and the processing, think about those weird cases at the start - what if the initial string is 'bacon' and the length is 'bacon'. Does that make sense? How would you stop (or reduce the chances) of a word turning up when a number was expected?

Some programming languages allow you to set the expected type of data (integers, text, decimals) and may reject a value if it's not what the program expects. Others simply pass the values through and require you to check them yourself in your code. In both cases, you should consider what happens if an error occurs and try to handle it as best you can. Users don't like big error screens full of strange code.

Short Diversion The 'add a zero' is a little more interesting - you are actually adding a string to another string (also known as 'concatenation') and various programming languages handle it in different ways: ruby uses a normal plus sign (+), some BASIC's use ampersand ('&'). PHP for example uses a dot (.) like 'This string is added '.' to this other string'

A variation on the above solution - figure out how many zeros to add then add them all at once, so no loop needed. Probably quicker and less prone to operator error (no repeated checking the length of the string)

inputs: inputstring and required length, and padding character

process:

howmanyextrazeros = desired length - outputstring length

outputstring = "0" * howmanyextrazeros + outputstring

output: print the outputstring

Now, try it on paper with nothing in the input string ("") - What happens?

With -2 in the length?

With 1000000 as the length?

With 'bacon' as the length?

Thinking about these test values when designing the program will help when checking the inputs (only accept integers for length, for example, as a word cannot be 4.5 characters long), and when developing tests (when you start to use an automated Test framework to let the code test for the obvious errors itself)

Hope this helps!

enrique

Welcome to the new Hackety blog!

Posted March 06, 2012 at 2:17 PM

Welcome to the new Hackety blog!

We plan on using this space to share interesting articles with you, talk about learning programming, and write small little chunks about Ruby!

If you'd like to help contribute, let me know in the Support forum!

Expect a real article tomorrow. :)