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

0 Members and 1 Guest are viewing this topic.

Re: I wrote my first bit of source code!
Ignore it then.. it must be just strings that you need to worry about buffer overflow then.
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 Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Oh, I forgot, here's the program for those who are interested.

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

{

//Declare Variables
double d; //diameter of garden
double w; //width of walkway
double ti; //width in inches
double t; //width in feet
double r; //calculated radius of garden
double R; //calculated outer radius of walkway
double dei; //extra measurement for garden diameter in inches
double wei; //extra measurement for walkway width in inches
double d_extra; //dei converted into feet
double w_extra; //wei converted into feet
double V; //volume of concrete needed in feet
double answer; //the end result converted into square yards
double R2; //R squared
double r2; //r squared
const double PI=3.1415926536;

//Input Data
printf ("\n *********************GARDEN WALKWAY CALCULATOR*********************");
printf ("\n\n This program calculates in square yards the amount of concrete needed to");
printf ("\n build a circular walkway around a circular garden.");
printf ("\n You will be prompted to input the diameter of the garden in feet,");
printf ("\n then any additional measurement in inches.");
printf ("\n Then you will be asked to input the width of the walkway in feet and inches");
printf ("\n followed by the thickness of the walkway."); 
printf ("\n Please input the inches measurement in decimal form.");
printf ("\n For example, input 6 1/2 inches as 6.5.");
printf ("\n\n Diameter of Garden.");
printf ("\n Feet:");
scanf  ("%lf", &d);
printf (" Inches:");
scanf  ("%lf", &dei);
printf ("\n Width of Walkway.");
printf ("\n Feet:");
scanf  ("%lf", &w);
printf (" Inches:");
scanf  ("%lf", &wei);
printf ("\n\n Thickness of Walkway.");
printf ("\n Inches:");
scanf  ("%lf", &ti);

//Calculate
d_extra=(dei)/12;
w_extra=(wei)/12;
r=(d+d_extra)/2;
t=(ti)/12;
R=r+(w+w_extra);
R2=pow(R,2);
r2=pow(r,2);
V=PI*t*(R2-r2);
answer=V/3;

//Output Data
printf ("\n Radius of Garden: %4.2lf feet.", r);
printf ("\n Radius of Walkway from center of Garden: %4.2lf feet.", R);
printf ("\n\n Amount of concrete required: %4.2lf cubic yards.", answer);
printf ("\n\n");
return (0);
}

 
Re: I wrote my first bit of source code!
Looks like we have a future SCP coder  :nod:

(btw personally I'd give those variables a better name than just "d", "w", "t", like (gardenDiameter, walkwayWidth).. but thats just me  :D )
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 Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Looks like we have a future SCP coder  :nod:

(btw personally I'd give those variables a better name than just "d", "w", "t", like (gardenDiameter, walkwayWidth).. but thats just me  :D )

Yeah, I would for longer programs, especially ones with more than one int I guess they're called.  I haven't started on stuff like that, though.

 

Offline Cyker

  • 28
Re: I wrote my first bit of source code!
Ahh... wait 'til you start writing code for Windows or to do graphics!!

That'll have you tearing your hair out :D

I learned Windows API in C, and to create a simple window needs about 5 pages of code!  :eek:

Luckily there are lots of 'helper' libraries for that, but unfortunately they're all C++ which means they're brain damaged

(For the record, I hate C++ - I think the class system syntax was actually designed to Kill Programmers via brain haemorrages or something. Java and C# are so much nicer to wrote code for!! Its just a pity they both only run via virtual machine emulation, and are thus damned slow...)



BTW... do they still teach people to strcture functions like:

void function(int param)
{
//stuff
}

Instead of :
void function(int param){
//stuff
}

?

Why is that? I've seen that cause so many problems, Like the famous:

while(i<50);
{
printf("%i",i++);
}
//Why won't this code do what you'd expect it to do? ;)

Yet everyone still uses that style except (it seems!) me!!
(When I coded that is ;)  I haven't done any serious codeing in ages :()

 
Re: I wrote my first bit of source code!
Personally I hate:

void function(int param){
//stuff
}

It's bad for indenting, you lose balance doing it that way..  Where's the "{"?

Something like this:

Code: [Select]
while (i != 10){
     work = computeValue(i) ;
     while (work != 0)
              force = computeStrength (work);
              if (force > MAXforce){
                     ShowAlert () ;
                      return ;
              }
              work-- ;
     }
}

Java seems to be the worst offender.
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
  • Moderator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
Ahh... wait 'til you start writing code for Windows or to do graphics!!

That'll have you tearing your hair out :D

I learned Windows API in C, and to create a simple window needs about 5 pages of code!  :eek:

Luckily there are lots of 'helper' libraries for that, but unfortunately they're all C++ which means they're brain damaged

(For the record, I hate C++ - I think the class system syntax was actually designed to Kill Programmers via brain haemorrages or something. Java and C# are so much nicer to wrote code for!! Its just a pity they both only run via virtual machine emulation, and are thus damned slow...)



BTW... do they still teach people to strcture functions like:

void function(int param)
{
//stuff
}

Instead of :
void function(int param){
//stuff
}

?

Why is that? I've seen that cause so many problems, Like the famous:

while(i<50);
{
printf("%i",i++);
}
//Why won't this code do what you'd expect it to do? ;)

Yet everyone still uses that style except (it seems!) me!!
(When I coded that is ;)  I haven't done any serious codeing in ages :()

Not sure what you're asking here.  If you're referring to brace alignment style, people should IMO teach the BSD/Allman style (where the braces are aligned in the same column), as it's superior in both readability and syntactical consistency.

Incidentally, you added an erroneous semicolon in your second example.

 
Re: I wrote my first bit of source code!
Well Goober agrees with me  :D

Btw how do you guys feel about the old Hungrain notation? I.E.   

bool bIsFound ; 
int iSpeed ;
char cData ;


I used to like it, but have grown out of it. 
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
  • Moderator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
Ugh.  I hate Hungarian notation.

 
Re: I wrote my first bit of source code!
We agree again  :D

bInfo   what b byte/boolean?
How do you do class instances?  Thats why I quit using it..  Plus FXCop flags them as out-of-date.
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 taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
If you're referring to brace alignment style, people should IMO teach the BSD/Allman style (where the braces are aligned in the same column), as it's superior in both readability and syntactical consistency.
Speak for yourself.  I hate it, primarily for readability reasons. :)

I hate it more that the three of you (Goober, WMC, kara) tend to change existing :v: code to also use that brace style, but whatever.  I'm from the "porting" school of coding, which means that you have no coding style except for the style of the code your working with.  If this was an icculus.org project then all of your code which doesn't conform to existing formatting would be rejected until you did it properly.  Which is why I format my code the way that I do, and will continue to do for all of my FSO work. :D

We do agree on Hungarian notation though.  Yuck.  :ick:

 

Offline Backslash

  • 29
  • Bring Our Might To Bear
Re: I wrote my first bit of source code!
Heh, now I'm confused... what's the :v: code style then?  I thought I had figured it out but it sounds like most of the code I've been looking at has been changed.  Or which style should I use when committing?

Is there a particular name for the style used in FSO code?

 

Offline Goober5000

  • HLP Loremaster
  • Moderator
  • 214
    • Goober5000 Productions
Re: I wrote my first bit of source code!
I hate it more that the three of you (Goober, WMC, kara) tend to change existing :v: code to also use that brace style, but whatever.  I'm from the "porting" school of coding, which means that you have no coding style except for the style of the code your working with.

Well, I like my code neat and organized, thank you very much. :D

Incidentally, every now and then I come across a snippet of :v: code which is actually incredibly hard to read until I format it properly.  The rest of the time, it's either out of habit or because I'm in a spring cleaning mood. ;)

Quote
If this was an icculus.org project then all of your code which doesn't conform to existing formatting would be rejected until you did it properly.

No kidding?  That's a good way to... if not drive certain coders away, at least create the potential for a lot of conflict.  Hmm...

EDIT: I sound like a noob.  Of course it creates the potential for a lot of conflict.  I need to read up on software engineering standards a little bit more than the advantages and disadvantages of certain styles. :)
« Last Edit: October 03, 2006, 12:03:26 am by Goober5000 »

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
Heh, now I'm confused... what's the :v: code style then?
K&R is what most (+95%) the :v: code is in.  Usually only when readability greatly benefits from it does the code deviate from this (like with a multi-line if() statement for instance, it's just easier to read with BSD/Allman style).  And of course using (4-space) tab indention is the FS norm.

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.  I do prefer BSD/Allman for switch() statements though since that is the one thing that I feel benefits greatly from it, readability wise.

I also use Anjuta (on Linux obviously), which is great for stuff like this since it allows you to collapse blocks into a single line, and it shows a vertical line to help you link up the code blocks with a quick glance.  IDE/editor does make a difference on how readable the various formats are.  MSVC uses BSD/Allman by default, so it's likely easier to read that.  MSVC also sucks, so take that how you want. :)

(see http://en.wikipedia.org/wiki/Indent_style if you want to see more about the various indent styles)


Well, I like my code neat and organized, thank you very much. :D
I do too, but I don't find those sorts of changes neat or organized.  :p  ;)

No kidding?  That's a good way to... if not drive certain coders away, at least create the potential for a lot of conflict.  Hmm...
Actually, that's a big reason why there aren't more Linux coders involved with the SCP.  You changing the formatting style for a particular block of code irks us seriously.  Using different formatting styles hurts overall readability, which is why when working with an existing code base, you stick to the existing style as much as possible.  A good coder will adapt to the code, not adapt the code to themselves.  :)

 
Re: I wrote my first bit of source code!
On the wiki list:
BSD is the best, followed by GNU and K&R, BSD/KNF are the second worst, Pico is hidieous.

I hate formatting, coding that is one big paragraph... break it up Please! Screen space isn't that important anymore

This also includes 80 col. Theres nothing worse than having a long section and it cuts down to a second line only to have a little piece  :rolleyes:

Code: [Select]
     Result = SomethingTakesALittleMoreThan80 (parameter1, parameter2,
                                               parameter3) ;


And MSVC does BSD GNU great, since you can collapse code sections very nicely.  I also like C#'s regions, you can also use comments on those regions.  It helps hide the unimportant information.
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 taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
On the wiki list:
BSD is the best, followed by GNU and K&R, BSD/KNF are the second worst, Pico is hidieous.
What's "best" is irrelevant and 100% coder preference.  You use what works for the code, which means that you don't change formatting in an existing project.  Personal preference isn't worth a damn unless it's your own project.  K&R is the most common C formatting style, BSD/Allman is the most common C++ formatting style.  Being from a C background I greatly prefer K&R because it's what I'm use to.  But I'll use whatever any existing code base is formatting with regardless of my personal preference.

I hate formatting, coding that is one big paragraph... break it up Please! Screen space isn't that important anymore

This also includes 80 col. Theres nothing worse than having a long section and it cuts down to a second line only to have a little piece  :rolleyes:
I hope that you re-read those two lines and realize how stupid that is.  The first sentance totally contradicts the second.  If vertical screen space isn't important, then neither is horizontal screen space.  Especially when you consider that there exists more horizontal screen space than vertical screen space.  But it's irrelevant regardless since in an existing code base you should use the same formatting style as the existing code.

I prefer to conserve vertical screen space and use more horizontal screen space (which is usually something that you can get away with regardless of other formatting).  Horizontal spacing issues hinder readability far more than vertical spacing does.  I don't stick to 80 column (except usually for comments), but I don't like to go over 160 either.

And MSVC does BSD GNU great, since you can collapse code sections very nicely.
Like I said, MSVC caters to BSD/Allman since it's the default formatting style in MSVC.

  
Re: I wrote my first bit of source code!
This is kinda what i mean by spacing:
Code: [Select]

void DoSomething () {
    int loop=0;
    int processed=0;
    bool done=false;
    while (!done) {
         done=StartProcessing(loop, &processed);
         if (processed == MaxLimit){
            done=true ;
    }
}


I don't know about you, but I'm quite visual when it comes to coding.  Clustering ideas together apart from each other.

Code: [Select]
void DoSometing ()
{
     int loop = 0 ;
     int processed = 0 ;
     bool done = false ;

     while (!done)
     {
           done = StartProcessing (loop, &processed) ;

            if (processed == MaxLimit)
            {
                  done = true ;
            }
     }
}


albiet if (processed) and done= true can be collapsed into one statement, but you get the idea.

As for the 80 column, i totally agree with you, but i know people that'll chew us out for evening going to 81. I'd rather not have my code look like a bad cross between cobol and asm. LOL  I'll keep my bad cross mixes to my models  :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 Cyker

  • 28
Re: I wrote my first bit of source code!
Aww crap... did I just start a flame war?!  :eek:

Erm....

So what about that Haskell 'eh guys? Erm...

*runs and hides*

while(i<50);
{
printf("%i",i++);
}
//Why won't this code do what you'd expect it to do? ;)

Yet everyone still uses that style except (it seems!) me!!
(When I coded that is ;)  I haven't done any serious codeing in ages :()

Not sure what you're asking here.  If you're referring to brace alignment style, people should IMO teach the BSD/Allman style (where the braces are aligned in the same column), as it's superior in both readability and syntactical consistency.

Incidentally, you added an erroneous semicolon in your second example.

Very good! That is the correct answer to the problem! :D

The mark of an experienced coder ;)

That is the problem with the in-column {}'s - Its very easy to do stuff like that and miss off a semi but still have the code compile and run because it is perfectly valid code.

If you miss off a brace, the compiler will usually throw up at least a warning about an unmatched brace somewhere...

If you're used to it, there are no readability problems - You look for indentation blocks, not tiny curly braces ;)
I don't know what syntactical consistency means...

I use KATE (The KDE editor) to write most of my code - Most IDEs really suck at the resolutions I work at (800x600 - *Much* more comfortable on the eyes for staring at text for long periods.), but KATE handles long-line wrapping very well and intelligently.


At the end of the day, its coder preference, but that can be problematic in a team (Once in uni, we got so hung up about it we wasted time making utilities to switch between the two styles before we actually started working on the project proper!!!  :eek: :D)



Code: [Select]
void DoSomething() {
    int loop=0;
    int processed=0;
    bool done=false;

    while(!done) {
         done=StartProcessing(loop, &processed);

         if (processed == MaxLimit){ //An unneeded brace...
            done=true;
         } //... or missing a brace :D

    }
}


 
Re: I wrote my first bit of source code!
Do you use GNU or K&R? Do you UVmap during or after you've finished the model. 

The equivalent of a religous war for coders/modelers  :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 taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
My preferred style is closer to this:

Code: [Select]
void DoSomething()
{
    int loop = 0;
    int processed = 0;
    bool done = false;

    while ( !done ) {
         done = StartProcessing(loop, &processed);

         if (processed == MaxLimit)
            done=true;
    }
}

Notice the spaces around the =, and the spaces on the while () as I do that with ! checks since it's usually easier to see the ! that way, and I don't normally like to use brakes on a single line if or ifelse.

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

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.  ;)