Jump to content

How to be a less sloppy programmer?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
Here is a sorting algorithm I wrote in Java:


public class Selection{

	static void switcharound(float[] s, int i, int j){

		float m = s[i];

		float n = s[j];

		s[i] = n;

		s[j] = m;

		// switch the values of s[i] and s[m] around

	}

	static void switchMin(float[] t, int index){

		// inner loop. places a single index

		double minValue = 100000000;

		int minIndex = 0;

		for( int i = index; i < t.length; i++ ){

			if( minValue >= t[i] ){

				minValue = t[i];

				// decrease minValue until it reaches the minimum value

				minIndex = i;

				// set minIndex to the index with the minimum value

			}

		}

		switcharound(t, index, minIndex);

	}

	static void sort(float[] t){

		// outer loop. places each index

		for( int i = 0; i < t.length; i++ ){

			switchMin(t, i);

		}

	}

}


That took me something like five hours to write. Five hours of coding and debugging, for three modules and 29 lines of code. Imagine what's going to happen when I start writing application programs that are 500 lines long. I'll need to find a better way to predict what is going to happen with code before I write it, so I don't end up messing up so much. Is there anything I can do to make my coding habits less sloppy? Maybe the GNU Debugger or something along those lines? Or is this something that will come with skill?

#2
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
I think (being not very experienced myself) that to be honest, it's a simple case of practice makes perfect. I mean, now you have done that, next time you could easily halve the time couldn't you? You know what made it good, what you needed to change. The more you implement things, the better you get at fine tuning and implementing the same sort of thing again, for a different situation.

But I am in a similar boat really, programming some things takes a really long time, that shouldn't take that long, just because I'm learning as I go, and making mistakes which need to be corrected. Hopefully though, those mistakes won't happen again - therefore reducing both time and effort. :)

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A lot of the "dumb" errors will work their way out from simple practice. That said, if you can test one function at a time, that will make it MUCH easier to debug the function and determine what the problems are with dependent functions.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Do you outline what your program does, or put a lot of thoughts about how a class or method will look like? Sometimes I do "mental programming" for a simple algorithm, and test whether it did what I wanted or not. For something bigger, I wouldn't recommend that. You also have to consider what you want to do might require some programming concepts you don't know of, so that may also take longer.

#5
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
You can try writing your program in pseudocode to get a good idea of what you are going to do.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#6
oliver.rush

oliver.rush

    Newbie

  • Members
  • PipPip
  • 24 posts
Besides all said above, I suggest you to study boolean logic, perhaps a little of discrete mathematics, and to read the Good programming practices..

#7
Soupdude

Soupdude

    Newbie

  • Members
  • Pip
  • 4 posts
im not sure if this might seem stupid, but, i sometimes draw stuff on a sheet of paper next to the pc, just to "visualize" what happens xP, (ps. doodling helps concentration too)

#8
oliver.rush

oliver.rush

    Newbie

  • Members
  • PipPip
  • 24 posts

Soupdude said:

im not sure if this might seem stupid, but, i sometimes draw stuff on a sheet of paper next to the pc

The stupid part is that you do it sometimes. You should do it MOST of the times. It's a good habit to practice.

#9
NatalieM

NatalieM

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
I use my downtime, such as when I'm travelling on the bus or on the train, to think about how to approach a new program.

During this thinking phase, I make a list of classes I need, as well as listing the variables and methods of each class (including the arguments and return objects), so when it's time to sit down in front of my computer, I already know in which direction to go. I also make copious notes of my mistakes - and more importantly, how I solved them - in a notebook, so I can refer to them later when I encounter similar situations (should my memory fail me).

Also, I maintain a list of favourites on my browser - I add blog posts, articles and forum threads as I come across them. It's an ongoing process - as I get better, I add more and more complex threads/articles and I get rid of the more simple stuff that has now become natural to me.

Lastly, I read quite a lot of programming books. I have a subscription to Safari online books, so this allows me to literally browse through thousands of books very easily online and then I can read them, or just read a chapter or whaveter. It costs about $48 a month - this isn't cheap if you're a student but it is money well spent if you have a day job as an evening programming course would cost more and not fit so well around my day job/social life.