Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 311 312 [313] 314 315 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907148 times)

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4680 on: July 18, 2013, 10:57:47 pm »

The order in which you write function declaration matter in C/C++? O_o That sounds like an oversimplification...

That's intentional.  This is a Java oriented lesson.  I'm not particularly concerned with the specifics of other languages beyond generalized concepts.  I try to focus on conceptual detail to help the lessons translate to other languages through programming concepts.  But if I want to talk specifically about another language, that will have to be another lesson in itself.
Logged

P(ony)SI

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4681 on: July 18, 2013, 11:48:17 pm »

You used the word "function" where you were supposed to write "method" 79 times. :P
Not including the times where you said, "function" in your code.
« Last Edit: July 18, 2013, 11:50:39 pm by P(ony)SI »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4682 on: July 19, 2013, 12:20:17 am »

You used the word "function" where you were supposed to write "method" 79 times. :P
Not including the times where you said, "function" in your code.

LMAO!  Really?  Wow...  And only half of those were intentional.  I considered doing a blanket substitution.  It's a really easy operation (%s/function/method/g).  I thought it'd be entertaining to leave them there.  That and I don't think anyone except Java calls them "methods".  If there's really that many, I should probably consider fixing them...the concepts are still correct, though, and that's what's important.

I need to count how many times I said "method" versus how many times I said "function".
« Last Edit: July 19, 2013, 12:22:05 am by Stargrasper »
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4683 on: July 19, 2013, 12:24:44 am »

The order in which you write function declaration matter in C/C++? O_o That sounds like an oversimplification...

Yep, especially when you consider declarations are not the same as definitions. I probably should have touched on that a bit in my previous lesson...

But if I want to talk specifically about another language, that will have to be another lesson in itself.

I got your back ;)

Lesson 3.1: Things To Consider While Writing Functions

Rule #1: The compiler is lazy and stupid.

If you call a function, it must have been declared prior to that point in the code. This will not work:

Code: [Select]
void a() {
    b();
}

void b() {
    // do something maybe
}

The compiler has not found a declaration for b at the point where it is called in a. The compiler, being lazy, won't go out of its way to find b. Because it is stupid, it will just assume b does not exist anywhere in the code. There are two ways to get around this. The programmer could simply not call functions in code from before they are defined. If they want more flexibility, then they can use a neat mechanism called function prototypes:

Code: [Select]
void a();
void b();

void a() {
    b();
}

void b() {
    // do something maybe
}

That code will work. Prototypes are made by only using the function header (see Lesson 3), followed by a semicolon. The function body is left off. This tells the compiler that the function does exist, but it is defined elsewhere. Even though b's function definition (which contains the function body) is after a's, its declaration (with the prototype) is still before a's definition, so it works.

Every function with a declaration must also have exactly one definition somewhere within the compilation unit.

Rule #2: A function should do one thing very well.
Corollary to Rule #2: A function that does multiple things should be split into multiple functions that each do one thing.
Another Corollary to Rule #2: A function should have an accurate and descriptive name.

Writing the entire code of a program into the main method is legal. That does not mean it is good. The modular programming paradigm came about as a response to the fact that code needs to be split up into reusable pieces in order to be effective and efficient when performing larger, more complicated tasks.

How much or how little a single function does depends on how broad/narrow your definition of a "thing" is. At the most extreme side of modular programming, the main function would exist only to call other functions. There are some who believe this is good practice. I'm not here to preach about what practice is best. I'm here to teach about good practices, so that you, as the programmer, can decide for yourself which practice you prefer.

Consider the following code:

Code: [Select]
#include <iostream>

using namespace std;

int print(int a) {
    return a+1;
}

void addOne(int a) {
    cout << a << endl;
}

int main() {
    cout << print(5) << endl;
    return 0;
}

Is this valid C++ code? Yes. Is it good C++ code? Hell no. The print function returns 1 + the integer argument, and the addOne function prints the integer argument. Meanwhile, the main function prints an integer, even though there is already a function that does that. This code fails Rule #2 and both of its corollaries. Let's make it better:

Code: [Select]
#include <iostream>

using namespace std;

int addOne(int a) {
    return a+1;
}

void print(int a) {
    cout << a << endl;
}

int main() {
    print(addOne(5));
    return 0;
}

There, much better. Granted, it's a bit overkill to have a method that adds one to a number, but this is example code, not practical code. It makes a point: functions should do what they say they do, and nothing more or less. There's a name for this concept, but I can't for the life of me remember it. If someone could help my brain out, it would be much appreciated.

There are more things to consider about functions, but they will have to be addressed after more lessons, because they involve language features that I haven't covered yet.

P(ony)SI

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4684 on: July 19, 2013, 12:27:13 am »

I need to count how many times I said "method" versus how many times I said "function".
You said "method" about 52 times, give or take a few.

Also, thank goodness function declaration order doesn't matter in Java. :D
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4685 on: July 19, 2013, 12:47:19 am »

I need to count how many times I said "method" versus how many times I said "function".
You said "method" about 52 times, give or take a few.

Also, thank goodness function declaration order doesn't matter in Java. :D

I just copied the entire post into a text file and ran the following two commands on it.  Ran word count on the whole thing, too.
Code: [Select]
stargrasper:~> grep -o method post.txt | wc
     49      49     343
stargrasper:~> grep -o function post.txt | wc
     93      93     837

stargrasper:~> wc post.txt
     356  4079 59138 post.txt

First number is newline count, second is word count, third is byte count.

The reason function order "doesn't matter" in Java is because the Java compiler actually reads your code several times.  C/C++ reads your code four times, I believe.  And each for very different reasons.  First for pre-processor commands, then for compilation, then linking, then assembly.

I got your back ;)

Good to have friends.

That code will work. Prototypes are made by only using the function header (see Lesson 3), followed by a semicolon. The function body is left off. This tells the compiler that the function does exist, but it is defined elsewhere. Even though b's function definition (which contains the function body) is after a's, its declaration (with the prototype) is still before a's definition, so it works.

Worth noting that frequently these get placed in a header file.

Also worth noting that the Wishful Thinking technique I described follows Mego's function rules if you approach it correctly.  Especially the naming, because you're writing the function calls before you're writing the function declarations or definitions.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #4686 on: July 19, 2013, 01:11:51 am »

fuck extJS. Its the ass end of javascript.

And javascript is already the ass end of programning.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4687 on: July 19, 2013, 01:41:44 am »

Ah, ExtJS.  We used to use it extensively where I work now.  All of our intranet sites were coded in it.  At first we thought it was great, but we slowly grew to realize it was big, slow and clunky.  Making anything in it took ages, and it was a resource hog.  That was back in ExtJS 3.  In 4 it was even worse, last I saw.  The most horrifying thing I ever coded in it was a website that didn't use any of the ExtJS interface widgets at all: they were all panels with the HTML or template options set to generate regular HTML code.  I still don't know why we chose to do that.  Just for the utility stuff?  No clue.

We ended up dropping it in favor of using Bootstrap, jQuery and a set of JavaScript libraries we coded internally.  We never looked back.

And yeah, JavaScript sucks much of the time, but I've grown rather fond of it after years of coding in it for money.  It has surprising power and flexibility, it's just not afraid to let you strangle yourself with it ("lol, I see you forgot to put a new operator there, bet you won't find that bug for two hours!").
Logged
Through pain, I find wisdom.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4688 on: July 19, 2013, 01:50:57 am »

The order in which you write function declaration matter in C/C++? O_o That sounds like an oversimplification...

Yep, especially when you consider declarations are not the same as definitions. I probably should have touched on that a bit in my previous lesson...

I don't think the function order matters when they're in classes. >_> I've written one function over another in the header and then in opposite order in the cpp file many times.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4689 on: July 19, 2013, 01:56:46 am »

All that matters to the compiler is that the function declaration was in the header somewhere before it was ever invoked.  So even if you write class methods that rely on other not yet defined methods it will work fine if both methods are declared in the class in the header, and the header is included first.

If you tried writing the function definitions inside the class definition in the header, it might cause problems if they're not in order.  I've actually never tried that.
« Last Edit: July 19, 2013, 02:05:02 am by Telgin »
Logged
Through pain, I find wisdom.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4690 on: July 19, 2013, 01:57:55 am »

More detail into how C/C++ compiling works:

Step One: Preprocessing

There are 15 standard preprocessor directives: #include, #define, #undef, #if, #ifdef, #ifndef, #error, __FILE__, __LINE__, __DATE__, __TIME__, __TIMESTAMP__, #pragma, #, and ##. Their use will be covered in a later lesson. These get processed first, by the C preprocessor (even if it's C++ code, it's still the same preprocessor as C). The output from this step is a C/C++ code file with a ton of code in it that you didn't write, along with the code that you did write.

Step Two: Compiling

Lots of stuff goes on in this step (building an abstract syntax tree, doing rules checking, instantiating templates, etc) that each deserve their own separate lesson to be explained fully. The end result is an assembly code file for each C/C++ code file input, that is (more or less) equivalent programmatically to the respective input file. After this point, the input C/C++ files are no longer read from.

Step Three: Assembling

Next, an assembler assembles the assembly code into object files. An object file is a binary file that is a piece of a larger binary executable, containing the binary code and information needed for linking it with other object files.

Step Four: Linking

Finally, a linker takes all the object files and static libraries needed and puts them all together into a single executable file that can be loaded and executed by the operating system.

As the steps progressed, my descriptions got more abstract. This is for two reasons. First, compiling, assembling, and linking are fairly advanced and complicated concepts, so they deserve a full tutorial of their own in order to fully and properly explain them. Secondly, my knowledge in these subjects is more theoretical and less practical, as well as fairly minimal, so I can't properly teach them right now. Check back with me in a few years, when I've finished my CS degree.

The order in which you write function declaration matter in C/C++? O_o That sounds like an oversimplification...

Yep, especially when you consider declarations are not the same as definitions. I probably should have touched on that a bit in my previous lesson...

I don't think the function order matters when they're in classes. >_> I've written one function over another in the header and then in opposite order in the cpp file many times.

All that matters to the compiler is that the function declaration was in the header somewhere before it was ever invoked.  So even if you write class methods that rely on other not yet defined methods it will work fine if both methods are declared in the class in the header, and the header is included first.

If you tried writing the function definitions inside the class definition in the header, it might cause problems if they're not in order.  I've actually never tried that.

Skyrunner is correct. Classes have their own rules when it comes to parsing, so their functions are not so restricted by declaration order, even without using prototypes.

Code: [Select]
#include <iostream>

using namespace std;

class Foo {
public:
    void a() {
        this->b();
    }
    void b() {
        cout << "Foo!" << endl;
    }
};

int main() {
    Foo f;
    f.a();
    return 0;
}

Perfectly valid.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4691 on: July 19, 2013, 02:03:38 am »

Interesting.  As I said, I've never tried it to my knowledge, since I very, very rarely include method definitions directly in headers.

Anyway, on the subject of function prototypes, I'm actually not sure why C++ compilers require functions to be declared before they're used if the definition is in the same translation unit.  It doesn't really make sense as a requirement.  It should certainly be possible to give it declarations of functions that exist outside the translation unit, but shouldn't be required for those defined inside it.  Another pass through the source file could easily find any functions that were defined. I'm guessing it's just something that was adopted early on and was deemed not worth the trouble of changing.
« Last Edit: July 19, 2013, 02:05:34 am by Telgin »
Logged
Through pain, I find wisdom.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4692 on: July 19, 2013, 01:05:26 pm »

Interesting.  As I said, I've never tried it to my knowledge, since I very, very rarely include method definitions directly in headers.

Anyway, on the subject of function prototypes, I'm actually not sure why C++ compilers require functions to be declared before they're used if the definition is in the same translation unit.  It doesn't really make sense as a requirement.  It should certainly be possible to give it declarations of functions that exist outside the translation unit, but shouldn't be required for those defined inside it.  Another pass through the source file could easily find any functions that were defined. I'm guessing it's just something that was adopted early on and was deemed not worth the trouble of changing.

It's a design choice in C and C++ that the compiler does minimal work. It reduces overhead in the compiling stage, and goes with the design choices in the language - do exactly what the programmer says. If there is ambiguity, better to do nothing than to try to figure it out and guess incorrectly.

Also, it is possible to put declarations in one translation unit so you can use the functions when the definitions are in other translation units.

Code: (a.cpp) [Select]
extern void b();

void a() {
    b();
}

int main() {
    a();
    return 0;
}
Code: (b.cpp) [Select]
#include <iostream>

using namespace std;

void b() {
    cout << "EXTERNINATE!" << endl;
}

Code: (Compilation and Linking) [Select]
$ gcc -c a.cpp -o a.o
$ gcc -c b.cpp -o b.o
$ gcc -o ab a.o b.o

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4693 on: July 19, 2013, 08:29:18 pm »

Though on that note, C# DOES allow using a function that's declared later on, with no issues. That's neither here nor there, though.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4694 on: July 19, 2013, 11:01:04 pm »

Though on that note, C# DOES allow using a function that's declared later on, with no issues. That's neither here nor there, though.

Yeah, and the C# compiler is way more complex and works a whole hell of a lot harder than C/C++ compilers, too. C and C++ are designed (among other things) to be relatively simple to build a compiler for.
Pages: 1 ... 311 312 [313] 314 315 ... 796