Monday, January 31, 2011

Coding

FLG was thinking this morning that he has no idea how people make it through their lives without knowing how to read and write computer code. Well, that's hyperbole. He knows plenty of people leading perfectly fine lives without knowing how to code, but he finds knowing that skill so useful.

Still, FLG found it funny that Lifehacker recently had a learn to code weekend.

Speaking of which, FLG has a pet peeve when it comes to coding. Surprise, right? In computer programs, there are things called functions. They are very much like mathematical functions. The basic gist is that you feed the function input and it will return some output. The area of a circle is an easy example.

Here's some C++ code, which FLG actually hasn't written in a long while, for a function that calculates the area of a circle:

float circle_area(float radius)
{
const float PI = 3.14159;
area = radius * radius * PI;
return area;
}

You'd call it with something like this:
answer = circle_area(3);


Answer would then equal 3 * 3 * 3.14159, or 28.27 something.

Now, you could actually simplify the code by raising the radius to a power with something like this:
float circle_area(float radius)
{
const float PI = 3.14159;
area = pow(radius, 2) * PI;
return area;
}

FLG knows what you're thinking. What's your pet peeve in all this?

Well, see how those curly braces are on separate lines and clearly demarcate what's contained within the function? That's how people traditionally do all sorts of things in C++. So, for another example, an if statement.

if (x > 0)
{
Do something here.
}
else
{
Do something else.
}

Nice, clear, easily understood. What's the problem then? Jackasses is what. For some reason, in other languages that borrowed syntax from C/C++, and FLG is thinking of PHP here, the convention is this:
circle_area($radius) {
$PI = 3.14159;
$area = $radius * $radius * $PI;
return $area;
}

if (x > 0) {
do something here.
}
else {
do something else.
}

That's all kinds of fucked up and confusing. Plus, these jackasses have decided to bring their silliness to other languages.

Wait, FLG. You went through all that just to complain about the formatting of a curly brace? Yes, yes, FLG did.

3 comments:

Andrew Stevens said...

I find both conventions equally easy to read, so long as I'm aware which is being used. I think you're just being curmudgeonly here, simply because you're not used to the convention.

I note that you did not indent the "do something here" bit past the function definition line as anybody who actually used that convention would (thus matching up the closing brace with the original function definition line and all within indented).

FLG said...

Curmudgeonly, surmudgeonly.

Doh! My software actually indents for me.

Zog Karndon said...

You obviously don't code for a living, or else you would have seen the various flame wars that erupt over the One True Indentation Style. It makes the war against Blefescu seem, well, Lilliputian.

 
Creative Commons License
This work is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States License.