By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Parameter name can be same as function name???

P: n/a
I just ran across this code at work:
BOOL
main_AddDefaultBenchHeaterPeriods // ???
(
DWORD main_AddDefaultBenchHeaterPeriods // ???
)
{
// ... two dozen lines of code ...

BHSP.obj_id = main_AddDefaultBenchHeaterPeriods; // ???

// ... a dozen lines of code ...

return TRUE;
}
What the devil??? Surely this isn't legal? Can a parameter have
the same name as its function? I'd always thought not. But my
compiler (Visual C++ 6.0) doesn't even give a warning.
The bloke who wrote this file must have had a more bizarre sense of
humor than I'd thought, because he keeps doing really weird things,
such as: a parameter variable which shadows a static variable of
the same name, which shadows a global variable of the same name...
or the case I outline above where the parameter name is the same
as the function name... that sort of thing. Sort of like laying
bear traps in his code for the unwary maintainer. (Ie, me.)
--
Very curious,
Robbie Hatley
Tustin, CA, USA
lone wolf intj atsign pac bell period net
Jun 22 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Robbie Hatley wrote:
I just ran across this code at work:
BOOL
main_AddDefaultBenchHeaterPeriods // ???
(
DWORD main_AddDefaultBenchHeaterPeriods // ???
)
{
// ... two dozen lines of code ...

BHSP.obj_id = main_AddDefaultBenchHeaterPeriods; // ???

// ... a dozen lines of code ...

return TRUE;
}
What the devil??? Surely this isn't legal? Can a parameter have
the same name as its function? I'd always thought not. But my
compiler (Visual C++ 6.0) doesn't even give a warning.
It is legal. The argument name is contained in a tighter scope
and as such *hides* the function name.
The bloke who wrote this file must have had a more bizarre sense of
humor than I'd thought, because he keeps doing really weird things,
such as: a parameter variable which shadows a static variable of
the same name, which shadows a global variable of the same name...
or the case I outline above where the parameter name is the same
as the function name... that sort of thing. Sort of like laying
bear traps in his code for the unwary maintainer. (Ie, me.)


Yes, unfortunately some see "job security" in it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '06 #2

P: n/a
Robbie Hatley wrote:
The bloke who wrote this file must have had a more bizarre sense of humor
than I'd thought, because he keeps doing really weird things, such as: a
parameter variable which shadows a static variable of the same name, which
shadows a global variable of the same name... or the case I outline above
where the parameter name is the same as the function name... that sort of
thing. Sort of like laying bear traps in his code for the unwary
maintainer. (Ie, me.)


Yes, a function's scope starts after its name, and, yes, this guy was an
idiot who has impaired your productivity with stupid pranks. Your company
may have unwittingly rewarded him by passing him on to new green-field
projects to afflict.

If you have not made up your complaint quota yet, complain to your boss
that your velocity is slower due to nothing more than petty and malicious
vandalism.

Also, start fixing these things each time you notice them. Read /Working
Effectively with Legacy Code/ by Mike Feathers to learn how.

--
Phlip
Jun 22 '06 #3

P: n/a
"Phlip" <ph*******@gEEEmail.com> wrote in message
news:pa***************************@gEEEmail.com...
Robbie Hatley wrote:
The bloke who wrote this file must have had a more bizarre sense of humor than I'd thought, because he keeps doing really weird things, such as: a parameter variable which shadows a static variable of the same name, which shadows a global variable of the same name... or the case I outline above where the parameter name is the same as the function name... that sort of thing. Sort of like laying bear traps in his code for the unwary
maintainer. (Ie, me.)
Yes, a function's scope starts after its name


Ok, that sort of makes sense.
and, yes, this guy was an idiot
A word two ex-co-workers and I used to use was "BONEHEAD". :-)
(All caps, because we'd #define BONEHEAD and use it in conditional
compilation directives to slice out pieces of his work.)
who has impaired your productivity with stupid pranks.
Yes.
Your company may have unwittingly rewarded him by passing him on to
new green-field projects to afflict.
Actually, he was asked to seek greener pastures elsewhere.
I'm his replacement.
If you have not made up your complaint quota yet, complain to your boss
that your velocity is slower due to nothing more than petty and malicious
vandalism.
I did, but I'm the whole software dept. now. (Downsizing.)
I've been trying to get the company to hire me some fellow programmers,
but the money's too tight right now.
Also, start fixing these things each time you notice them. Read /Working
Effectively with Legacy Code/ by Mike Feathers to learn how.


Thanks for the reference. I think I need that book.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lone wolf intj atsign pac bell period net
Jun 22 '06 #4

P: n/a
> What the devil??? Surely this isn't legal? Can a parameter have
the same name as its function? I'd always thought not. But my
compiler (Visual C++ 6.0) doesn't even give a warning.


That is pretty bizarre stuff. I'd give him the benefit of a doubt
though and say he's just a lousy programmer rather than saying he's
malicious. Does this guy have any background in Visual Basic? It
reminds me somewhat of how functions work there. Instead of using a
return statement like in C++, you assign the return value to a
quasi-variable with the same name as the function itself.
Jun 23 '06 #5

P: n/a
Victor Bazarov <v.********@comAcast.net> wrote:
Robbie Hatley wrote:
What the devil??? Surely this isn't legal? Can a parameter have
the same name as its function? I'd always thought not. But my
compiler (Visual C++ 6.0) doesn't even give a warning.

It is legal. The argument name is contained in a tighter scope
and as such *hides* the function name.


This is true but it seems to me unnecessary. The function
could be resolved based on the number and types of its arguments;
it would not be hidden by a second function with a different
argument list, so why let a plain variable hide it?

Steve
Jun 23 '06 #6

P: n/a
In message <e7**********@blue.rahul.net>, Steve Pope
<sp*****@speedymail.org> writes
Victor Bazarov <v.********@comAcast.net> wrote:
Robbie Hatley wrote:

What the devil??? Surely this isn't legal? Can a parameter have
the same name as its function? I'd always thought not. But my
compiler (Visual C++ 6.0) doesn't even give a warning.

It is legal. The argument name is contained in a tighter scope
and as such *hides* the function name.


This is true but it seems to me unnecessary. The function
could be resolved based on the number and types of its arguments;
it would not be hidden by a second function with a different
argument list, so why let a plain variable hide it?

What would happen if the "plain variable" _is_ a (pointer to a)
function?

--
Richard Herring
Jun 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.