[Tutor] Re: small program in Python and in C++

alan.gauld@bt.com alan.gauld@bt.com
Thu, 4 Jul 2002 18:06:21 +0100


> | That's a recipe for disaster! If you write more than a few 
> | hundered lines of code finding your definitions will be murder. 
> 
> A function that large (usually) needs to be refactored anyways.

Not so. If a function consists of say 50-60 lines of executable 
code then most industrial C++ code will typically contain at least 
as many lines of comment as there are code so you'll have 120 lines of 
text to wade thru'

In the real world many functions contain 200 plus lines of 
executable code(*) which means maybe 400-500 lines of text.

(*)If you have an object with over 100 attributes to serialize
(not an unusual occurence in real world scenarios!) refactoring 
the function isn't really practical or sensible.

> With a good editor (eg vim, where your hands stay on the keyboard) it
> is quite trivial to search for variable names.  I do it all the time,
> even though I've mostly been working with python lately.

Yes but you have to search for the definition/declaration. You have 
to keep stepping past all the usage items to get there! That is 
time consuming and error prone. Jumping to the top of the function 
is a single keystroke!

> I find it confusing, actually, to see a whole mess of types/names at
> the top of a function when I have no idea what each one really means
> yet.  

Well written variable declarations should all be commented! :-)

> I think it is easier to follow a function if you only have to
> remember the relevant variables for that section of it.  

If they are only relevant for that section then I agree they 
can be done locally (like loop counters) or that bit of the 
function could be refactored. But I'm talking abouty variables 
that are used throughout a function.

> Limiting the scope of a variable limits the length of time 
> for which you must remember what it means.

Only if reading the code sequentially, if you are jumping in 
at random - as is usually the case for maintenance programmers 
- the position of declaration is usually not an issue until 
they come to change it!!

> Of course, if you are writing a 3-5 line function (or so, something
> short) it is clearer to put the declarations at the top so they don't
> clutter the flow of the rest of the logic.

Hmm, not sure I follow the logic there. I'd have said declaring 
variables in a short function was less important since you can 
see whats happening whichever way you do it...

> Oh, also I think it is good to initialize the variable immediately.

I agree there.

> Initializing it to a senseless value like NULL isn't really helpful,

Not true. Many a NULL has caused a crash when an unitinialised 
variable would have kept running - with completely wrong results. 
A crash in those circumstances is a good thing! And NULLS help 
achieve it!

> function, whereas if you declare the variable later you can construct
> it properly then.  

OTOH If you want to use aiabvle declared further down a function near 
the top you have to go hunt for the declaration and cut n paste it 
back up to where you need it. If its declared at the top its always 
available.

> This is also even more significant in C++ than in
> Java or Python when working with classes.  Either you'll end up
> wasting some resources by first constructing a default instance of the
> class, and then later constructing the one you wanted and copying it

I see the point of wasted resource but not sure how Java/Python 
helps here. If you initialize the variable to a dummy object you 
waste resource and in all 3 languages that resource stays wasted 
till you get rid of it.

> | The only reasonable exception to this are single letter variablers
> | used as loop counters etc.
> 
> Loop counters should be declared in the opening of the loop, IMO.

Agreed, thats what I meant.

> It probably depends on whether the "regular" C++'ers started with C or
> not.

Nope, It probably depends more on how many months or years they've 
spent debugging other peoples code :-)

> C required all locals to be declared at the start of a block.  I think
> that's due to the early implementations, but I'm just hypothesizing
> here.

Its good software engineering practice as taught at the time 
- Pascal for example requires variables to be declared in a 
var section of the code, as does ADA and Modula (and even COBOL!)

> Java has the same rules as C++, and all the code I've written and read
> seems to follow the "don't declare it until you actually need it"
> style.

Yes, and maintenance programmers the world over are learning 
to hate it! Our Java style guides at work have recently been 
ammended to insist that all variables be declared at the top 
of scope - previously it was just a recommendation...)

> Of course, much of this is just a matter of style with little
> technical merit either way, so do as you please :-).

If it hits the time to fix faults its more than just style.
When an operational fault has to be fixed in lkess than 4 hours 
say, you really don't have time to cast about looking for variables.
It becomes a matter of real bottom line accounting.

Alan g.