.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   What are the best practices? (http://forums.bots-united.com/showthread.php?t=6832)

stefanhendriks 01-07-2008 20:42

What are the best practices?
 
Hey,

I bet you all have your ways of coding things; I surely have learned a lot from my mistakes. So what are your best practices in terms of coding?

First of all, let me start:
- use classes, and sub classes to override behaviour
- use one convention for all your variables, don't do i1, i2, i3, etc.
- do not do this:
Code:

int i =0; // set i to 0
- use assertions wherever possible, to assure your assumptions are correct

So, what do you suggest?

The Storm 01-07-2008 21:29

Re: What are the best practices?
 
Why not initialise variable to zero ?

Whistler 02-07-2008 03:57

Re: What are the best practices?
 
I think that is about the useless comment "set i to 0"...

for my experience, now I feel by not stuff all parameters in function declarations in one line can sometimes offer better readability, like this:

PHP Code:

int
MyFunc
(
   
int      iParameter1,
   
float    flParameter2


looks better than:

PHP Code:

int MyFunc(int iParameter1float flParameter2

... especially when there are too many parameters :)

stefanhendriks 02-07-2008 07:54

Re: What are the best practices?
 
Yeah it was about the comment ;-) I mean, the comment is more characters then the actual line of code; which brings me to:

- the code should be readable as it is, any comment needed to explain code, means your code is not simple/good enough.

Comments however can explain WHY certain things are done

Since i work with Java for some time now, I have been into classes big time; when i see the RB code, all those FUNC_ methods give me the creeps. Keep things where they belong (ie, a bot can See things, not a util method with a bot pointer... )

Also, i am all with you Whistler about huge params functions, but then again, you might wonder why they need so many params :)

The Storm 02-07-2008 17:14

Re: What are the best practices?
 
Nice idea Whistler but I really don't like this kind of syntax. :)
Maybe is better when there is many arguments to put them in a struct or class and to pass one object or pointer as argument instead of many arguments. ;]

stefanhendriks 02-07-2008 18:53

Re: What are the best practices?
 
I don't know if that is better The Storm; because you hide the functionality from the method signature.

I mean, if you do something a a bot for instance, hence a bot pointer is needed.

But, then again you may wonder if the method you are creating would not be better to be part of the bot class itself.

Methods with an awful lot of variables often indicate too much is done within the same method. Which is a 'bad smell' imo ;)

The Storm 02-07-2008 19:30

Re: What are the best practices?
 
Still I don't like the kind of new line syntax, but here whats looks better for me:

Code:

int MyFunc(int iParameter1, float flParameter2, char szParameter3[],
                    string strParameter4, bool bParameter5)
{
      // The stuff...
}

i.e. when the arguments can't fit good on the screen we put the new line with one tab space after the first bracket. :)

P.S. I can't get it to display well in the forum board but you get the idea. :P

stefanhendriks 02-07-2008 19:43

Re: What are the best practices?
 
Ah yes, thats how i like it as well. I was more referring like putting everything in one class for the sake of reducing arguments ;)

The Storm 02-07-2008 20:04

Re: What are the best practices?
 
Sometimes is good the classes to have static public methods. :P

@$3.1415rin 03-07-2008 14:57

Re: What are the best practices?
 
Quote:

Originally Posted by Whistler (Post 58333)
...
PHP Code:

int
MyFunc
(
   
int      iParameter1,
   
float    flParameter2


looks better than:
...

I think that can be useful if you want to include comments to each parameter. but nonetheless, a header-text for each function describing what it does is nice. there are also some programs around to automatically create documentation from those texts in a bunch of html files


All times are GMT +2. The time now is 15:05.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.