Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 268 269 [270] 271 272 ... 796

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

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4035 on: March 01, 2013, 10:26:20 am »

I think someone already said this, but why don't you expand the variable names?

So r->resistance, rnot->initialResistance, vs->resistorVoltage, rs->resistanceOfResistor. vm->voltageOfMeter.

So it would look like


(note, I have a question or two inside)
Spoiler (click to show/hide)

The reason for expanding the variable names is because of increased readability. Before I changed them, I had to keep looking up to see what the heck "vm" is. But now, I can see what it is in one glance. It's easier to see if you're doing anything wrong, for example. Short, 1~6 letter variable names irk programmers a lot.
I put the original equation's variables in comments. This is actually how I did my models that were based off a scientific paper. I put the variable names in comments and expanded them so I can tell what I'm doing. The paper, for example, called for fT, fB, fR, fL. I expanded them to fluxToTop, fluxToBottom, fluxToRight, fluxToLeft. Much more readable. Same with Ks (sedimentCapacityConstant), Kd (sedimentDepositingConstant), or cc (runoffCurveConstant).

Also notice that I inserted a lot of spaces. Spaces also help your code look much more readable.
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

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4036 on: March 01, 2013, 10:33:22 am »

Short, 1~6 letter variable names irk programmers a lot.
No, they don't. Using a do while with a double and an exact check one bigger than your range is what irks me.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4037 on: March 01, 2013, 10:37:04 am »

Short, 1~6 letter variable names irk programmers a lot.
No, they don't. Using a do while with a double and an exact check one bigger than your range is what irks me.
Huh, he checks for != o_o
It should be <= 110, at least.

Also, you seem to not have seen old source code for a huge simulator. >.> Every file name is less than 10 characters, each variable is cryptic and terse, and the code itself is unknowable.
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

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4038 on: March 01, 2013, 10:44:26 am »

Scientist code is always like that. Especially older scientists, since they learned programming on computers with punchcard readers.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4039 on: March 01, 2013, 11:04:01 am »

Why are there std's in the code?
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4040 on: March 01, 2013, 11:28:14 am »

Why are there std's in the code?
Because nobody likes using namespace std;
:)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4041 on: March 01, 2013, 02:22:26 pm »

Why are there std's in the code?

Stack Overflow explains why importing a namespace (such as std) is considered bad practice in C++. I only do it if I'm outright bringing one namespace into another, which is pretty rare an event.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4042 on: March 01, 2013, 02:24:01 pm »

Are namespace low level language shenanigans? As I never heard of them :x
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4043 on: March 01, 2013, 02:28:05 pm »

Eh, I see no reason not to use namespaces. I mean, obviously you should never use them in your interfaces, the bits of code other people actually use and interact with and include in their own files. But for self-contained blocks of code, I don't see the potential issue.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4044 on: March 01, 2013, 04:04:58 pm »

Time for a Namespace Lesson!!!


Are namespace low level language shenanigans? As I never heard of them :x

Not really,
a namespace is kind of like a struct or a class,
Syntax is:

namespace namespace_name
{
//Namespace members would go here
void examplefunction()
     {
        //examplefunction stuff.
     }
}

to access a member of a namespace, you use the scope resolution operator "::"
so to call the example function it would be
namespace_name::examplefunction();

They are used for libraries and stuff because it lets them use function names, that other libraries might be using, so to use a function from it you have to include the namespace name.  In the standard library there is a namespace called "std"
members of that namespace include things like cout and cin, which are commonly used.
so, in order to use cout or cin, you'd normally have to write it like this.
std::cout  or std::cin

if your program uses a lot of functions from one namespace, it might be a better idea to access it using a "using namespace" line,
so if towards the beginning of your code, you write  "using namespace std;"
you can access cin and cout without using the namespace name, so in this case it would be
cout or cin

Quote from: random audience member dressed as a cowboy
"Hey! that looks both easy AND fun! why don't we do the using namespace thing for ALL the namespaces?!"

woah! calm down there cowboy, yes that may look like a good idea at first, but it includes ALL of the function names from that namespace, so what if you wanted to use 2 namespaces that both had a function with the same name? It wouldn't know what to do with itself, and probably wouldn't do what you want it to.

Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Soadreqm

  • Bay Watcher
  • I'm okay with this. I'm okay with a lot of things.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4045 on: March 01, 2013, 05:26:49 pm »

woah! calm down there cowboy, yes that may look like a good idea at first, but it includes ALL of the function names from that namespace, so what if you wanted to use 2 namespaces that both had a function with the same name? It wouldn't know what to do with itself, and probably wouldn't do what you want it to.

Doesn't the compiler warn you when you do that? Because it kind of seems to me like it should. "Hey dude you got like six different functions named 'bluh()' available in this context which one do you want me to use here?"
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4046 on: March 01, 2013, 05:28:29 pm »

Actually, if the signatures are different (the int, int or void things), it just overloads them no problem. O.o

Note: not sure what they are called...
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

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4047 on: March 01, 2013, 05:31:42 pm »

It essentially brings the definitions of all functions and classes into another namespace. You can actually do:

Code: [Select]

namespace foo
{
    void bar();
}

namespace piano
{
    using namespace foo;
}

int main()
{
    piano::bar();
    return 0;
}

In fact creating that kind of behaviour in a library is the only time I ever will use using statements in C++. Otherwise they just risk causing more trouble than they are worth.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4048 on: March 01, 2013, 05:35:06 pm »

Why not using statements in cpp files? O_o Those aren't shared.
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

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4049 on: March 01, 2013, 05:43:12 pm »

Why not using statements in cpp files? O_o Those aren't shared.

Because they include headers, and headers define functions. It's an accident waiting to happen, and I find tends to make it harder to follow what's going on in a piece of code. The prefix's add clarity, and readability is an important thing in coding. Quite frankly, there's just no reason to use the using namespace instead of std::, but plenty of reasons not to. I'd rather not do the wrong thing in the first place by habit, instead of waiting for the compiler to tell me I'm doing it wrong.

Heck, consider the following C++11 line:
auto pcount = &count;

...am I getting a pointer to a variable called count, or a function pointer to std::count?
« Last Edit: March 01, 2013, 05:47:54 pm by MorleyDev »
Logged
Pages: 1 ... 268 269 [270] 271 272 ... 796