Are you a programmer? Care to do a quick review of a completely artificial example of some code? The programming language isn’t important – I am just making a point.
function max_squared(number: integer; no : integer)
/* returns square of the maximum of the two parameters */
{
integer num;
if (no > number)
{
num = no;
}
else
{
num = number;
}
return sqr(num);
}
So what’s wrong with that code? Well, I expect you can come up with lots of issues, but there is one that stands out.
Some of you will prefer a different brace-style and/or indenting.
Some of you will prefer that the num
variable was initialised the moment that it was defined.
Some of you would prefer the function was called maxSquared
or Max_Squared
.
Some of you might recommend that we check the programming language to see if it already has a max
function.
However, I hope all of you complained that having three different variables, num
, no
and number
was confusing.
It certainly confused me. While writing the first draft of this code, I slipped up and returned the wrong one!
It is a common coding standard/guideline to avoid similar sounding or appearing names for variables in the same scope.
(Why am I going on about silly errors in coding style? Bear with me. I’m building up a framework for an argument. I’ll come back to this later on.)
Comment by alastair on October 25, 2005
I think the most obvious problem with this code is that it’s using the square root function
sqr()
when it says that it will be squaring (one of) the inputs.Comment by Pete on October 25, 2005
Alastair is making an assumption about the implementation language. Where does it define that “sqr” means square-root?
It would be safe to assume this if we were talking about the C language – but I’ve never seen C do function declarations like
“function max_squared(number: integer; no : integer)”
To me a more obvious problem is that the code isn’t simply using a function like “max” (which this pseudo-language should surely have):
“return sqr(max(no, number));”
Comment by Sunny Kalsi on October 25, 2005
I’m actually easy on practically everything (brace, indenting, caps). num/no/number freaked me out a little, but so did the weird commenting. That’s just not right. The thing that gets me the most, however, is that the function doesn’t do anything “proper”. max_squared? there should be a max, a sqr (assuming that does a square) but max_squared? WTF?
I understand this is an example, however, so I’ll assume that it’s “some reasonable function” as opposed to “max_squared” specifically.
Comment by Julian on October 25, 2005
Alastair, you are right. I should have simply returned (num*num) and avoided the ambiguity. If anything, this silly oversight strengthens my point.
Q: Does
sqr
map tosquare
orsquare_root
?A: Don’t use similar sounding names, like
sqr
, and we won’t even need to have the discussion!Thanks Pete for coming to my defence. As I wrote it, I didn’t have a specific programming language in mind. I tried to choose language features that would be the most obvious to the various C++, Java and Python-loving factions of my readership. Before long, I realised the result was pretty much pure Ada. Ooops.
For the record, Ada has a built-in
sqrt
function but nosqr
function.(Oh dear. Now I will have missed mentioning Ruby or LISP or Rope or something, and get flamed.)
I covered the idea in the original article that it should use the languages built in
max
. If Ada has amax
function, I can’t recall, but it almost certainly involves generic instantiation, and I certainly don’t want to go into that.Sunny,
Yeah, you are right. I normally prefer function names to a bit more high-level than just a recipe of steps! This is just an example. Please assume it does something proper.
What’s in particular did you object to with the commenting? The C-style comment tokens? Appearing after the function declaration rather than before? Stating the bleeding obvious?
Comment by Andrew on October 26, 2005
Actually, what struck me was the inadequate requirements. What do you expect to get if you call max_squared(5, -7)?
There should be a comment explaining that one.
Comment by Sunny Kalsi on October 26, 2005
The fact that it appears after the function declaration. The fact that it’s C-style makes this even poorer, because one could type:
(note the semicolon)
Also, if the comment was long enough, by the time you get to the brace, you could’ve forgotten what the function was all about. It basically weakens the relationship between the declaration and the brace. I also don’t like comments like this:
for similar reasons, but sometimes they are unavoidable…
Comment by Shark8 on February 2, 2013
Ada’s Max function is an attribute: TYPE_NAME’Max( Var_1, Var_2 ).
It’s available for all Scalar types according to the LRM: http://www.ada-auth.org/standards/12rm/html/RM-K-2.html
PS ‘Min is in there as well.