Re: wanna be expert
From: Corey Murtagh (emonk_at_slingshot.no.uce)
Date: 05/04/04
- Next message: Arto Viitanen: "Re: size of object In C++"
- Previous message: hannu.poropudas_at_osakk.fi: "Re: If the universe is shaped like a cone why does the sky look round?"
- In reply to: David Ananas: "wanna be expert"
- Next in thread: Michael Jørgensen: "Re: wanna be expert"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 04 May 2004 20:09:21 +1200
David Ananas wrote:
> 1. When you write something like: if (myVar == SOME_OPTIONS), is it
> better to define SOME_OPTIONS as constant or to use macro?
Personally I prefer:
if (SOME_OPTIONS == myVar)
...where SOME_OPTIONS is a const or enum, depending on type requirements.
It's largely (but not entirely) a question of style. Named constants,
in whatever form (#define, const, enum) are more readable than 'magic'
numbers. Which do you prefer:
a = r * r * 3.14159265358979323846;
or:
area = radius * radius * M_PI;
The compiler doesn't much care or course.
> 2. Example like my question #1 above is typical of what I use in my
> programs (scientific, number-crunching type, I program just to get
> some numbers out mostly). Lately, I've been moving into (Visual) C++
> and looking at other people's programs, written by "real" programmers,
> it seems that they all do things like: if ((vSW&0x0ca1) == 0x0c00). My
> question is, is this the way I should be programming? My problem with
> this is that, reading programs by others, I'm really having a hard
> time figuring out what the options are, and what the program is doing.
> Just thought it'd be instructive to hear what the gurus say about
> this.
Problem is, a lot of self-proclaimed 'real' programmers started out as
code monkeys, and never really progressed from there. They got used to
using magic numbers and such in their code, since they understand what
it means. They've generally never had to deal with maintaining someone
else's code, or had to work in a team environment where obfuscated code
gets dirty looks from their workmates... or even had to come back to a
piece of code they wrote 6 months ago and try to figure out what it does
and how.
In reality a lot of programmers use literals instead of named constants
at some point. I try to avoid it in production code, but my own little
home projects are littered with them.
No one's perfect ;)
-- Corey Murtagh The Electric Monk "Quidquid latine dictum sit, altum viditur!"
- Next message: Arto Viitanen: "Re: size of object In C++"
- Previous message: hannu.poropudas_at_osakk.fi: "Re: If the universe is shaped like a cone why does the sky look round?"
- In reply to: David Ananas: "wanna be expert"
- Next in thread: Michael Jørgensen: "Re: wanna be expert"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|