Coding practices - Arghh

Posted By: pararealist

Coding practices - Arghh - 07/27/09 05:09

This i hate:

void SomeFunction() {

...bla bla

}

I would like to meet the person who came up with that and
the reason for it.
But then again i am an old timer used to
void SomeFunction()
{

...bla bla

}
Dont know why, but it is one of these (little) things that
drive me MAD.
Sorry for the ranting, but are there any other "Normal"
people out there?

Posted By: EvilSOB

Re: Coding practices - Arghh - 07/27/09 05:46

Me too, but some say I have even worse habits.

Code:
...
if(something)
{   blah;
    if(something)
    {   blah;
        if(something)
        {   blah;
            while(1)
            {
                blah...
}   }   }   }


Posted By: pararealist

Re: Coding practices - Arghh - 07/27/09 06:31

Well let them talk, it looks ordered to me.
Posted By: Joozey

Re: Coding practices - Arghh - 07/27/09 07:41

You'll hate me grin
Posted By: Damocles_

Re: Coding practices - Arghh - 07/27/09 12:08

I also dont like brackets shifted like this.
Its a freak habbit.

Especially when there is no "bracket detection"
its hard to see wich opening-bracket belongs to wich closing one.

Basically: the opening bracket should have the same x position
as the closing one. And the contest should be shifet (tab or just
a defined amount of spaces)
The only difference is a one-liner
if(me==god) { ..do almighty things.. }

Well formated and commented code is often more important than
super-optimized algorythms, as the programmer has it easier to
rework the code month later, and find bugs.
Posted By: DJBMASTER

Re: Coding practices - Arghh - 07/27/09 12:15

EvilSOB's way is what i prefer. You can see which logic belongs where instead of having to find each brace. I use 'Switch' 99% of the time because i can read it better.
Posted By: Michael_Schwarz

Re: Coding practices - Arghh - 07/27/09 15:15

whoa, finally some sane people here!

I really hate those two things too. I really have to a agree with Damocles.
Indention is a ABSOLUTE MUST!

What drives me crazy, when comparisons have no spaces in between.. umm... well just see (assuming it's a short, one liner):

Code:
if(me>=god){...}



for me it _MUST_ look clean and neat like this:

Code:
if(me >= god) { ... }



you can see my coding style if you take a peek at my code snippets you can find in my blog *cough**shameless self-advertising**cough*
Posted By: ventilator

Re: Coding practices - Arghh - 07/27/09 15:44

this is one reason i like python. smile
Posted By: mpdeveloper_B

Re: Coding practices - Arghh - 07/27/09 16:07

I agree with pararealist to a point, but I also agree with Michael. My general code is:

Code:
function blahblah()
{
     if (blah >= blahblah)
     {
          //insert alot of code
     }
}



but, if it can be a one-liner, I go this way:

Code:
function exit_() { while(key_any) { wait(1); ) exit; }



I generally don't put alot of ifs as one liners, but whiles i do and functions i do. It's just a pet peeve. I just go for organization...I can't stand out of place code and brackets that are put right after the function name. There's nothing wrong with people who do it that way, but I don't and I think it looks tacky....

edit: I also normally use a Tab indention, I don't like a 5 space like i have in my examples.

edit: yes evilSOB....that's a reaaaaaaaly bad habit.....
Posted By: Joozey

Re: Coding practices - Arghh - 07/27/09 18:22

This is my style, shiver already:
Code:
void test( int arg ) { <- I can't stand code without space between parameter and brackets
  
  displayNumber( arg, 14, 23 );
  
  if( arg < 10 ) {
    arg = test( arg + 1 );
  }

  else if( arg == -1 ||
           arg == -3 ||
           arg == -5 ) {
    return -1;
  }

  else { return 0; }
}


Posted By: Machinery_Frank

Re: Coding practices - Arghh - 07/28/09 06:56

Hm, Joozey and Michael, if you love spaces then you should be consequent and use them after every bracket and after tags like "if":

Code:
displayNumber( arg, 14, 23 );
  
  if( arg < 10 ) { ...



should consequently be:

Code:
displayNumber ( arg, 14, 23 );
  
  if ( arg < 10 ) { ...



I personally think that it does not help and the eye has to move too much to get the info. But I also prefer the pascal style and the "begin, end" commands instead of brackets. It is better readable for a human being.

Code:
if a>b then begin
  a:=...
end else a:=0;



reads better than:

Code:
if (a>b) {
  a=...
} else a=0;



But in the end all this does not matter if we can make a job done with each of them wink
Posted By: Damocles_

Re: Coding practices - Arghh - 07/28/09 12:03

I think a lot of the

if(...){
...
}

type of using bracket is from
the times when screensizes where small.
So The programmer saw maybe only 15 Lines of code at once
in the small window.

I prefer to see the code-list as long as possible nowerdays.

More importantly than the formatation is the
NAMING and COMMENTS in the code.

using too short and non-meaning variable / class names is
terrible.
Modern IDEs also have helpers to find attribute and function names of classes quickly, so even long names can be insterted quickly. (Eclipse etc.)

And I also wonder, when obsering code of other, how
little comments they add to it.

There is no reason to leave out comments about what the code is
dooing and what it is for. Having to "parse" the code manually
every time when looking at others code is an unnessesary waste of time.
The comments are usually shown in another color, so they dont really distract from reading
the doe itself.
Posted By: DJBMASTER

Re: Coding practices - Arghh - 07/28/09 12:49

What notation you guys prefer when you're defining variables/functions...

1) foo_bar
2) FooBar
3) fooBar

I prefer the first one for variables, but always use the second one (Pascal notation) for functions.

Also does anyone prefer the ternary operator to if/else? I hardly use it.
Posted By: Joozey

Re: Coding practices - Arghh - 07/28/09 15:43

My screen is still not large. I see about 30 lines in my screen. Perhaps that's indeed the reason why I prefer brackets in front of statements.

@frank:
I prefer to attach the ( to an if, because it looks more like the statement is part of the if, like encapulated. I think very visual in these styles. It's like a tiny machine that holds content, and processes it.

I wouldn't mind a language made out of ascii art grin

if( a + b = c ) {
}

[(> a + b = c <)]::>
<::


@DJBMASTER
There are actually common rules for markup, though not strictly followed.

Classes and constants get a capital letter:
class HelloYou {};
const int HelloYou;

Variables and methods a small letter:
int helloYou;
void helloYou();

Header definitions all capital:
#ifndef __HELLOYOU_HPP__
#define __HELLOYOU_HPP__

and I'm sure there are more which I can't think of right now.


I tend to use an underscore _ when names consist of categories and a function description.

void playerInventoryItem_create();
void enemyWeapon_remove();
Posted By: Michael_Schwarz

Re: Coding practices - Arghh - 07/28/09 17:01

Originally Posted By: Machinery_Frank
Hm, Joozey and Michael, if you love spaces then you should be consequent and use them after every bracket and after tags like "if":

Code:
displayNumber( arg, 14, 23 );
  
  if( arg < 10 ) { ...



should consequently be:

Code:
displayNumber ( arg, 14, 23 );
  
  if ( arg < 10 ) { ...



I personally think that it does not help and the eye has to move too much to get the info.


That would be overkill. I just want variables separated from operators, and arguments separated so you can "count" them quickly.

Originally Posted By: DJBMASTER
What notation you guys prefer when you're defining variables/functions...

1) foo_bar
2) FooBar
3) fooBar

I prefer the first one for variables, but always use the second one (Pascal notation) for functions.

Also does anyone prefer the ternary operator to if/else? I hardly use it.


Depends, as we have no "IntelliSense" in SED like in Visual Studio, I use different naming conventions for c-script than I use in C# using visual studio.

example from c-script:

Code:
function vDialogReadFrom(sFilename)
{
    if(ibDialogInProgress == true){ return; } else { ibDialogInProgress = true; }
    sysDialogHandle = file_open_read(sFilename);
    while(file_str_readto(sysDialogHandle, sDialogCurIn, sDialogDelimiter, 1000) != -1)
    {



the prefixes:

v - void
s - string
sys - something engine internal, like file handles
ib - internal boolean

etc...

helps while reading the code, you know exactly what variable stores what kind of information.
Posted By: JibbSmart

Re: Coding practices - Arghh - 07/29/09 02:13

Quote:
Code:
if( arg < 10 ) {
    arg = test( arg + 1 );
  }

Do you guys ever omit the brackets?:
Code:
if (arg < 10)
	arg = test(arg + 1);

...is how I will do it whenever there's only one line in the "if". I like it clean and minimalist. I know I could go further and have "if (arg < 10) arg = test(arg + 1);" in one line, but I like going down a line and indenting because it means there's always an indented line after my "if"s, whether they're one-liners or not.

Also, I like a space between the "if", "else", or "while" and the opening parenthesis, but no space between parentheses and arguments. I would do Joozey's example like this:
Code:
void test(int arg) {
	displayNumber(arg, 14, 23);

	if (arg < 10)
		arg = test(arg + 1);
	else if (arg == -1 ||
		arg == -3 ||
		arg == -5)
		return -1;
	else
		return 0;
}


I wish Lite-C supported ternary expressions. I quite like "x = x < 0 ? -x : x;" for finding the absolute value, for example (though since there's a built in function, I'd use "abs" anyway, but that's just an example).

Jibb
Posted By: lostclimate

Re: Coding practices - Arghh - 07/29/09 05:43

Code:
variables:

type varName;

Functions:
function/type DoWhatever (arg1, arg2.... )
{
   return CallOtherFunction();
}


Conditionals:

if(someThing >= 0)
{
    DoWhatever(42,42);
}
else
{
    youSuck=True;
}




also I like to keep my variables classes, functions and objects declared in the .h file and the processes in the .c file, but that is just so that I don't get myself confused switching back and forth from vc++
Posted By: Machinery_Frank

Re: Coding practices - Arghh - 07/29/09 07:22

I find Jibb's style very comfortable to read. And as far as I can remember I did most typing in a very similar way.

But I probably would try to write a short if-command without a break. If you have 10 thousands of lines in your code then you know what I am talking about:

Code:
if (arg < 10) arg = test(arg + 1);



This is good to read, compact and does not waste too much space.
Posted By: PHeMoX

Re: Coding practices - Arghh - 08/19/09 16:53

Quote:
using too short and non-meaning variable / class names is
terrible.


I tend to use that all the time and it doesn't seem to bother me much. Of course, my code usually is commented properly, so it's actually not meaningless stuff.

Short(er) names do make me program a lot faster.
© 2024 lite-C Forums