What is the point of (firstWordSmallLetterTheRestCapitalsLikeThis) in coding?

Solution


It...

Pinhedd

Distinguished
Moderator


It's a matter of convention and is particularly popular in object-oriented programming languages.

Types, especially those that are objects rather than primitives, either have their component words separated by an underscore, or have their component words capitalized. Examples include MyType, and my_type. mytype is of course possible, but may be unconventional; for private usage it is fine, but when writing code intended for use by others it is a good idea to observe convention and be consistent in usage of convention.

Functions, whether global in scope or a member of a class, typically do not start with a capital but have other component words capitalized or separated via an underscore. For example, myFunction(), MyClass.myStaticMethod(), MyObject.myMethod().

In non-object-oriented languages such as C, these conventions are less common as the implementer is expected to become familiar with documentation. The C standard library and POSIX system libraries do not follow either of these notations.
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590


Ok thanks, but what is an object oriented language?
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590


I'm sorry I don't speak "smart". Mind explaining that in a way a peasent like me can understand?
 

Pinhedd

Distinguished
Moderator


Object-orienting is a programming paradigm which emphasizes the concept of objects. An object is an abstract container which can contain traditional data types (integers, floats, references/pointers, characters, arrays of these, etc...), abstract types such as attributes, as well as functions that act upon the data contained within the object.

A quick example,

In the C programming language, an ASCII string is an array of characters, and the length of an ASCII string can be determined through the strlen() function.

C++:
#include <stdio.h>
#include <string.h>
int main()
{
        const char* mystring = "This is a test";
        size_t mystring_len = strlen(mystring);
        int out_len = mystring_len;
        printf("Length:%d\n",out_len);
        return 0;
}
Length:14

In the C++ programming language, an ASCII string is encapsulated in a string object, and the length of that string can be determined through the string's member function "size"

C++:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
        string mystring = "This is a test";
        int out_len = mystring.size();
        printf("Length:%d\n",out_len);
        return 0;
}
Length:14

Both of the programs above are identical in output, but differ in how they store the string and how the length of that string is determined.
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590


No just on my own time. I failed my English class last semester so I had to replace my computer science class with English so I can pass. Guess you have to pass your native language before you move on to computer languages xD
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590


Cool thanks. One more question though, the language I'm learning is kforth, and the way it works is there's a data stack. And in the code the values that come from equations etc are put on the top of the data stack. when you do something like add, the values are taken from the stack and added. And you can run through the program step by step.
ex.
{1 1 + R0!}
1 is put on the stack, then the next one is put on the stack, then they are added together, and the answer is put on the stack, then that is stored in register 0.

Do all languages work in a way similar to this?
 

Forth is classical example of a stack language, and most compilers use simillar approach when generatinc code (as well as pseudo-machines like Java and .NET/CLR).
 

Pinhedd

Distinguished
Moderator


It depends.

What you're describing is how most computational calculators work. Many programming languages use something conceptually similar called a call stack. Many microarchitectures have instructions designed for efficient call stack operation.

Each thread in a process has a call stack that controls the flow of data s the thread enters and returns from function calls. At a minimum, the call stack contains the following:

1. A return address pointer, which is the base address of the next instruction in the calling function to be executed once the called function completes

2. The value of the calling function's frame pointer, which is the base address of the stack frame of the for the calling function (this is used to roll back the stack to the proper position)

3. A region of space for the calling function to pass parameters into the called function

4. A region of space for the called function's local variables

Imagine the following

C++:
int foo()
{
int a = 0, b = 1, c = 2, d = 3, e, f;
e = bar(a,b,c,d);
f = baz();
}

In the example above, there are three functions, foo(), bar(), and baz(). Only foo() is described, and only bar() takes parameters.

In the example, the functions bar() and baz() are called by the function foo(). foo() is at some point called by another function.

When foo() is called, the frame pointer (also called the base pointer) points to a location in memory in which the current function's local variables start. The function's parameters and local variables can all be computed by adding or subtracting an offset to the frame pointer, so this makes addressing them easy. In the example above, foo()'s frame pointer may point to the address of the integer a. a can be computed by adding an offset of 0 to the frame pointer, b can be computed by subtracting an offset of sizeof(int) to the frame pointer (call stacks typically start at the top of the address space and increase downward), c can be computed by subtracting an offset of 2*sizeof(int) and so on.

When foo calls bar() it first pushes the return address onto the stack and adjusts the stack pointer accordingly. The return address will probably be an instruction to start calling baz(). Then, it pushes the caller's frame pointer onto the stack and adjusts the stack pointer accordingly. Then, it will push the values of a,b,c, and d onto the stack, and adjust the stack pointer accordingly. Then, it will set the frame pointer to the value of the stack pointer (such that they are coincident), and jump to the beginning of bar()'s code. One of the first things that bar() will do is adjust the stack pointer by the size of bar()'s frame. As a result, all local variables, no matter how many there are, are created at once.

When bar() is finished, the process is reversed. The stack pointer is rolled back to the frame pointer (destroying the callee's frame), the caller's frame pointer is restored from the stack to the frame pointer register (restoring the caller's frame), and program jumps to the instruction stored in the return address (exiting the function).

There are minor variations in calling conventions between ABIs and architectures, but the process is more or less the same.
 
Solution