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

Derrick 'dman' Hudson dman@dman.ddts.net
Wed, 3 Jul 2002 16:41:25 -0500


--/Uq4LBwYP4y1W6pO
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Jul 03, 2002 at 06:07:02PM +0100, alan.gauld@bt.com wrote:
| > up writing a C++ equivalent for someone else a few minutes=20
| >=20
| > ...or if anyone cared to point out other ways
|=20
| Not so much other ways but I notice you define your variables=20
| in C++ just before using them.
|=20
| That's a recipe for disaster! If you write more than a few=20
| hundered lines of code finding your definitions will be murder.=20

A function that large (usually) needs to be refactored anyways.

| Imagine discovering that a certain int has to be made a long.=20
| Now find where within several hundred lines you declared it.=20
| Ok you can use search functions but it's much easier to just=20
| go to the top of the function!

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.

| Its much more maintainable to group declarations at the=20
| top of the containing scope(function/file etc).

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.  I think it is easier to follow a function if you only have to
remember the relevant variables for that section of it.  Limiting the
scope of a variable limits the length of time for which you must
remember what it means.

I really like python's ability to 'del' a no-longer-needed local if
your function is too long.

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.

Oh, also I think it is good to initialize the variable immediately.
Initializing it to a senseless value like NULL isn't really helpful,
but often that is the only value you have available at the top of the
function, whereas if you declare the variable later you can construct
it properly then.  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
to the existing one (if you use stack-automatic memory management) or
you'll have to mess with pointers and new/delete and ensure you never
end up with NULL when you shouldn't, etc.

| 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.
=20
| Others might disagree but I think most regular C++'ers=20
| will back this up.

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

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.

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.


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

-D

--=20

If we claim we have not sinned, we make Him out to be a liar and His
Word has no place in our lives.
        I John 1:10
=20
http://dman.ddts.net/~dman/


--/Uq4LBwYP4y1W6pO
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0jb4UACgkQO8l8XBKTpRTiNwCguvqN2OzNCeZ/2sc6jxKHY3Px
/NoAn1swmkmhx6yncWw0IwUfRDgQxZM0
=zgqe
-----END PGP SIGNATURE-----

--/Uq4LBwYP4y1W6pO--