Author Topic: I wrote my first bit of source code!  (Read 12923 times)

0 Members and 2 Guests are viewing this topic.

Re: I wrote my first bit of source code!
Outside the placement of the "{" and your spacing [especially around the ";" and "(", it looks very much like my work.
Yes I sometime leave off the {} from a single line, depending on how i feel.  I see what you mean about the () around the !. 

It could be worse... he could be doing Haskell AND Hungarian notiation  :lol:

Worse yet.... it could be Perl style

Code: [Select]
void DoSomething () { int loop=0;int processed=0;bool done=false;while(!done){done=StartProcessing(loop,&processed);done=(processed==MaxLimit);}}
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: I wrote my first bit of source code!
Ugh.  I hate Hungarian notation.

Can't say I blame you.

I agree with you on the braces too.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
Outside the placement of the "{" and your spacing [especially around the ";" and "(", it looks very much like my work.
Actually the spacing (like "while (" instead of "while(") is done more for functional reasons rather than readability reasons, in my case at least.  It makes word selection easier in many editors so that you could get the "while" but not the "(" if that was your intention (you can probably double-click on my examples there to see what I'm talking about).  I think that I started always doing that a few years ago and I do it now without even thinking about it.

It could be worse... he could be doing Haskell AND Hungarian notiation  :lol:
Dude, don't even joke about that.  :shaking:

 :D

Worse yet.... it could be Perl style

Code: [Select]
void DoSomething () { int loop=0;int processed=0;bool done=false;while(!done){done=StartProcessing(loop,&processed);done=(processed==MaxLimit);}}
We affectionately refer to that as "Bobboau" style.  Well, except that everything is spelled correctly in your case.  And of course you need to use comments below the code in question sometimes, and above the code in question other times.  And use different indention for random code blocks, or no indention at all for random lines in the middle of a function.  Oh, and there should have been at least 5 bugs in that code snippet.

(...waits a few mintues for Goober to pick himself up off of the floor...)

Sorry, couldn't resist the joke at your expense there Bobboau. ;)
« Last Edit: October 03, 2006, 04:22:34 am by taylor »

 
Re: I wrote my first bit of source code!
HAHAHAHA
 :wakka:
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Goober5000

  • HLP Loremaster
  • Administrator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
(...waits a few mintues for Goober to pick himself up off of the floor...)

You misspelled "minutes" there. ;)

I agree with on everything else though, except the flame war.  If this were a flame war then you wouldn't have replied, for fear of death. :D

This poses a moral conundrum, because my SCP philosophy "Taylor Is Always Right" has the potential to spectacularly conflict with my general code formatting philosophy "BSD/Allman Bracing Is The Way To Go".  So far I've managed to avoid the issue. :D

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
You misspelled "minutes" there. ;)
Yeah, luckily I managed to go back and spell "up" properly before posting though.  That would have been rather embarassing.  But at least you haven't (yet) pointed out all of the other stuff that I've managed to misspell, or just get grammatically incorrect, in this thread.  ;)

This poses a moral conundrum, because my SCP philosophy "Taylor Is Always Right" has the potential to spectacularly conflict with my general code formatting philosophy "BSD/Allman Bracing Is The Way To Go".  So far I've managed to avoid the issue. :D
Hmm, that one does pose something of an issue.  Just go with "Taylor Is Almost Always Right", and then that "BSD/Allman Bracing Is Not The Way To Go".  That way everyone wins.  Well, I win at least.  But that tends to be normal, you get used to it. ;)  :D

Though techically I have no problems with "BSD/Allman Bracing Is The Way To Go", just the "I'm Going To Use BSD/Allman In An Existing Code Base That is Almost Entirely K&R" part.

Wait, why the hell are we doing the every-first-letter-is-capitalized thing again?  I'm sure that there must be a reason, since all of this is rather strange otherwise.

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: I wrote my first bit of source code!
To be honest I've used both K&R and BSD/Aliman in the past. I can quite happily use either but on a personal level I find the latter the easier to read.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Cyker

  • 28
Re: I wrote my first bit of source code!
...

You are in trouble for the Haskell remark however, I just threw a shoe at you.  Not really sure when it will get there, but when it does at least do me the courtesy of not ducking.  ;)

Erm... PASCAL! Yes! I typed PASCAL but... erm... I was... using... a Dvorak keyboard! Yes! That's the ticke-OOF!



 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Er, does my code look "pretty?"   :nervous:
« Last Edit: October 03, 2006, 12:07:23 pm by Freespace Freak »

 
Re: I wrote my first bit of source code!
It's readable... thats the key thing... weither it's K&R or GNU thats irrealavant.  As long as your not becoming a perl programmer on us  :lol:
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Goober5000

  • HLP Loremaster
  • Administrator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
Yeah, luckily I managed to go back and spell "up" properly before posting though.  That would have been rather embarassing.  But at least you haven't (yet) pointed out all of the other stuff that I've managed to misspell, or just get grammatically incorrect, in this thread.  ;)

Eh, I mostly do that to tweak newbs, or other people getting on my nerves. ;) I adapt myself to the forum grammar, except where I have an opportunity to refactor it. :p

Quote
Wait, why the hell are we doing the every-first-letter-is-capitalized thing again?  I'm sure that there must be a reason, since all of this is rather strange otherwise.

It's a common convention on the Internet to put Things That Are Important in title case, isn't it?  Occasionally with a ™ symbol at the end of the line? ;)

Let me revisit some things I didn't have time to address before:

I generally prefer K&R in the first place since I hate too much white space.  I like to see more code on the screen at one time, not more "{" and nothing else on a line.  Screen real estate is important to me and BSD/Allman wastes it like nothing else.

Ya, but IMHO less white space makes things more cluttered.  When the code is all bunched up like that (unless it's in a nice orderly if... else if... chain) it runs together and becomes hard to follow.  When scrolling is as simple as a flick of the mouse wheel, or a tap of the Page Up/Page Down key, whitespace is A-1 SUPAR.

Quote
MSVC uses BSD/Allman by default

As an aside, I have a hunch that MSVC uses BSD/Allman not from any evaluation of the merits of the code style but simply because they want to be "different" than Java, which uses K&R in their style manual.

Quote
Actually, that's a big reason why there aren't more Linux coders involved with the SCP.

That's... surprising.  Interesting.  But apparently Linux coders prefer mailing lists to forums too, so we've got several strikes against us. :p

Quote
A good coder will adapt to the code, not adapt the code to themselves.  :)

Now I both agree and disagree with that.  I agree that a coder should be able to mentally adapt to whatever situation he finds himself in.  If you're not flexible enough to work within the constraints you have -- whether it's a matter of code style or design style or programming paradigm or library or what have you -- you're not going to achieve much.  But the coder should definitely adapt the code to themselves, or at least to proper design guidelines.  Basically, if you find crappy code, you should refactor it.  If you find a bug, you should fix it.  If you find a design flaw, you should redesign it.  If you find a huge function that converts uppercase to lowercase using a gigantic switch statement, you should refactor that into a function.  (I actually ran into a similar situation in the localization library in FS2 once.)  And, IMHO, if you find a section of code that looks messy and hampers readablility and productivity, you should reformat it.  You should keep your code clean for the same reason you should keep your room clean. :)

Now I have one last parting shot to make. ;) Go look at your formatting example.  Explain to me this


Code: [Select]
void DoSomething()
{

in light of this

Code: [Select]
    while ( !done ) {
:p
« Last Edit: October 03, 2006, 07:47:22 pm by Goober5000 »

 

Offline Taristin

  • Snipes
  • 213
  • BlueScalie
    • Skelkwank Shipyards
Re: I wrote my first bit of source code!
From a non-coder's perspective after reading too much of this; It seems to me that unless one is willing to rework the entire structure of the code to the format he deems correct, then one should leave it in the format that it is largely or originally in.
Seems the most logical to me...
Freelance Modeler | Amateur Artist

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
But the coder should definitely adapt the code to themselves, or at least to proper design guidelines.  Basically, if you find crappy code, you should refactor it.  If you find a bug, you should fix it.  If you find a design flaw, you should redesign it.  If you find a huge function that converts uppercase to lowercase using a gigantic switch statement, you should refactor that into a function.  (I actually ran into a similar situation in the localization library in FS2 once.)  And, IMHO, if you find a section of code that looks messy and hampers readablility and productivity, you should reformat it.  You should keep your code clean for the same reason you should keep your room clean. :)
I totally agree.  A coder should always write good code (as much as possible anyway, doesn't always happen), if there is crappy code or buggy code then it should be fixed.  If there is messy code then it should be cleaned up.  If code is badly formatted then it should be reformatted so that it's easier to work with.  That seems totally obvious and I think that most everyone will tend to do that will little or no thought to the contrary.  But that isn't what we are talking about.

We are talking using formatting styles in new code which differ from the style currently used in the rest of the code base.  Changing formatting styles hurts readability unless ALL of the code also gets changed to use the new style.  It's basically the same thing is having a car which has been painted by two different people.  The car is painted blue, except for the hood which is red.  Why?  Because the person painting the hood liked the color red better.  Or it's like you handing in a group project for school, and everyone used a different font in the paper because it was their preference.  You don't mix styles, that's not only basic coding etiquette but also basic design/engineering standards.  You simply don't do it. 

I don't have a problem with with BSD/Allman or with anyone using it if that is their preference.  But you just don't go into an existing large code base, formatted with some other syle, and start using your own.  You change your style to match the style of the code that you are working with, whether that's BSD/Allman, K&R, GNU, or some strange combination of them.  THAT is a coder adapating to the code.


Now I have one last parting shot to make. ;) Go look at your formatting example.  Explain to me this

Code: [Select]
void DoSomething()
{

in light of this

Code: [Select]
    while ( !done ) {
I don't see the problem.  The top snippet is a correct function definition regardless of formatting style, and the body of the function is K&R.  K&R does not put "void DoSomething() {", that's considered wrong.  Sorry, but you'll need to take a better shot than that. ;)

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
It's readable... thats the key thing... weither it's K&R or GNU thats irrealavant.  As long as your not becoming a perl programmer on us  :lol:

Interestingly enough, in my Calc III class, I saw a guy who had four windows open on his laptop working on what was obviously some sort of source code.  Turns out he's a programmer for a local company.  I asked him what language he was using and he said perl.  He said his favorite languages are Python and Ruby.  Python is more efficient, but Ruby is easier to use.  He said he thinks C++ is one of the worst languages ever made.  It's easy to use, but it takes up a lot of processing power.  C on the other hand, is not so easy to use, and can be a bit confusing, but is much more efficient and powerful.  He recommended CYGWYN as a good free compiler for C.  What do you guys think?

 

Offline Goober5000

  • HLP Loremaster
  • Administrator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
[snip]
THAT is a coder adapating to the code.

Okay, gotcha.

Quote
I don't see the problem.  The top snippet is a correct function definition regardless of formatting style, and the body of the function is K&R.  K&R does not put "void DoSomething() {", that's considered wrong.  Sorry, but you'll need to take a better shot than that. ;)

Well, here's the thing.  The central difference between K&R and B/A is "where does the opening brace go".  K&R puts it on the same line as the control statement, whereas B/A puts it on the following line.  However many K&R advocates put function braces on the following line, as you did, which is inconsistent.

Now I've seen some die-hard K&R advocates put the function brace on the same line.  Although I disagree with the style in general, at least they're being consistent.  But I can't fathom why the rest of the K&R coders regularly break their own style that way.  I was hoping you could provide some insight into that.   How is it "considered wrong" if it's consistent with the style?
« Last Edit: October 04, 2006, 11:17:46 am by Goober5000 »

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
My prof. doesn't explicitly teach K&R nor B/A, however, his examples for the code he writes appears to be consistent with B/A.  I really kinda copy him, but I also organize mine in a manner that I think is most easy to read.  I completely isolated the brackets, like such:

Code: [Select]
#include<stdio.h>
int main ()

{

        function...
        return (0);

}


That way I can say to myself, "Hey, the function starts here, idiot!"  and "The function ends here too, stupid!"  So there is no confusion.  My method "wastes" even more notepad space because I'm putting in lines with absolutely nothing in them.  Not that that'll make a difference in execution, since the compiler completely ignores spaces and such.  It just makes the code files take up more space on your hard-drive, but with 250GB hard drives as the norm, it really doesn't make that much of a difference, IMHO since it won't affect processing speed or anything, ASAIK.

Here's my professor's class outlines.  You can see how he writes code, it seems fairly consistent with B/A.

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
But I can't fathom why the rest of the K&R coders regularly break their own style that way.  I was hoping you could provide some insight into that.   How is it "considered wrong" if it's consistent with the style?
The official/original C book did it that way.  It is subsequently considered the "official" K&R style, since they wrote it obviously.  So technically K&R dictates that you put the opening function brace on the first clear line, by itself.  The closing brace will also be on a line by itself.  And the body of the function must be indented past that point.  Those die-hard K&R advocates are actually wrong since they aren't truely following K&R rules.  They probably just got sick of all of those people who questioned the inconsistency of it and changed in order to shut them up. ;)

From the CodingStyle doc:

Quote
The other issue that always comes up in C styling is the placement of
braces.  Unlike the indent size, there are few technical reasons to
choose one placement strategy over the other, but the preferred way, as
shown to us by the prophets Kernighan and Ritchie, is to put the opening
brace last on the line, and put the closing brace first, thusly:

        if (x is true) {
                we do y
        }

However, there is one special case, namely functions: they have the
opening brace at the beginning of the next line, thus:

        int function(int x)
        {
                body of function
        }

Heretic people all over the world have claimed that this inconsistency
is ...  well ...  inconsistent, but all right-thinking people know that
(a) K&R are _right_ and (b) K&R are right.  Besides, functions are
special anyway (you can't nest them in C).

This also goes with my preferred formatting style for switch statements:
Code: [Select]
switch
{
    case SOMETHING:
    case SOMETHING_ELSE:
    ...
    break;
}
since switch statements are considered nested functions (though perhaps not technically).  So in that case you would format a switch statement the same way that you would format a function.

 

Offline Goober5000

  • HLP Loremaster
  • Administrator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
K.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Hmmm, I just saw that my prof does something different than all of that.  He does something like this:

Code: [Select]

        struct CurrentList

            { COMPLEX Node[50];

               int MaxNodes;

        };


Look, he has the left bracket, then a tab, then some stuff.  Totally different that K&R and B/D as far as I can see.

 
Re: I wrote my first bit of source code!
that looks a bit like the Pico format  :no:

The { shouldn't be the first thing on a line, same as "," and operators except "(".  It looks rather unnatureal.

Edit: by first thing on a line, excluding it being by itself...
« Last Edit: October 04, 2006, 08:59:42 pm by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"