Message Board


Message Board > Programming > Programming cleanly?

July 5, 2012, 09:51
Zomg
None
641 posts
How do you do that?
How do you learn it?
Is it something that's in you? Rofl.

I know that my university provided books on such topics but.. it's just crazy. I need to read two books: one on advanced UML and another one on patterns.. bah. It just makes my brain fry.

I know this other guy that can read those books like it was a cup of soup. I don't get it. Programming is difficult for me. Meh. Some people just 'get' it.

I have an example of this 'learn how to write clean code'.
I wrote code that passed on 20 arguments per method. My elite friend said: "No, you may only have a maximum of 3 arguments per method.". In Java you can't nullify parameters, so what did I do: I wrote this:

Code:
private doStuff(param param1, param param2, param param3){
  if (!param1.equals("0")) {
  }

  if (!param2.equals("0")) {
  }

  if (!param3.equals("0")) {
  }
}

My professor gave me only half the grades (which means barely passed) because he thought my code was 'dirty' and "it's pushing too much on the stack, making your program less robust". Wtf?

So I went and asked my friend what could be done about this. My friend said: "make a wrapper class with getters and setters for each property. This way you only set the properties that are needed.". Man, why didn't I think of that? I don't have those programming reflexes.

I guess some people are naturally better at programming. I'm terrible at programming. lol

[Edited on July 5, 2012 by Zomg]
____________
#
July 5, 2012, 21:52
PB
Defender of the faith
630 posts

Don't be intimidated by other people. Some people are really good at something and really bad at other things. They have just learned to communicate their strenghts to you.

There will always be people better than you in doing stuff, while at the same time other people are worse than you in doing the same stuff.

Just find out what your strenth is, and build on from there. And I do not think you are a terrible programmer, since you've got the thing working anyway...

And you might find out that when you walk out of the classroom, you're not judged by the same standards your teacher judges on you to give you grades.
____________
#
July 7, 2012, 13:49
DTM
Earthling!
821 posts

I think the key is, try to simplify everything. Make stuff easier to use. Imagine if you are reading documentation and find a method with millions of arguments, it's quite confusing.

Also consider if you want to change doStuff at a later date, for example adding another argument. Then you might have to modify every place where you have called doStuff() because of these changes.

Of course for a small number of -unrelated- arguments then that's probably fine. And for something where there are not likely to be any changes. e.g. SetScreenSize(1024,768), there's not really likely to be any future changes in that function signature.

But for a lot of arguments, especially if they are all highly related, it makes sense to put them in an object and then pass only that object.

Then you can add the equivalent of a new argument later just by modifying that object in one place. (Perhaps with a sensible default value too that avoids you having to look at all places you use that object)

Also you want to keep different functionality separate. It makes things easier to comprehend when reading the code. So it seems very unlikely any single function would really require so many different bits of input data. Perhaps doStuff() can be split into several different functions with fewer arguments each. (but of course don't arbitrarily split it, this only makes sense if there are clear logical ways to separate different functionality)
____________
:o
#
July 7, 2012, 22:26
PB
Defender of the faith
630 posts

I agree on splitting up functions to keep them small, speaking and reusable.

As for function arguments, I think Java needs something like this http://msdn.microsoft.com/en-u … y/dd264739.aspx

[Edited on July 7, 2012 by PB]
____________
#
July 7, 2012, 23:07
Zomg
None
641 posts
I ran into that changes problem all the time. My example was where I had to use the JDOM API to create XAML elements. If you know this, a XAML element may contain many arguments. So I guess the solution is to wrap it all in a class with getters and setters yes.
____________
#

Message Board > Programming > Programming cleanly?

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!