Re: wanna be expert

From: Corey Murtagh (emonk_at_slingshot.no.uce)
Date: 05/04/04


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!"


Relevant Pages

  • [PATCH 8/10] Re: [2.6-BK-URL] NTFS: 2.1.19 sparse annotation, cleanups and a bugfix
    ... -static int ntfs_attr_find(const ATTR_TYPES type, const ntfschar *name, ... -typedef enum { ... +typedef le32 NTFS_RECORD_TYPE; ... These are the so far known MFT_RECORD_* flags which contain ...
    (Linux-Kernel)
  • Re: String or Numeric Variable?
    ... it appears that using numeric constants will solve the 'readability' ... >>issue and Enum perhaps more efficient? ... > Const Afternoon As Long = 2 ... > Dim TimeOfDay as TOD ...
    (comp.lang.basic.visual.misc)
  • Re: String or Numeric Variable?
    ... it appears that using numeric constants will solve the 'readability' ... >issue and Enum perhaps more efficient? ... Const Afternoon As Long = 2 ... Dim TimeOfDay as TOD ...
    (comp.lang.basic.visual.misc)
  • Re: Cursor Constants
    ... means you have to define the named constants if you want to be able to use ... Doug Steele, Microsoft Access MVP ... const adLockOptimistic = 3 ... What rules or logic define what numerals are given to cursor constants? ...
    (microsoft.public.access.queries)
  • Re: enum as array index
    ... you really DON'T want to use an enum. ... Consider named constants instead. ... the same readable usage, but anchors the flag ... DOULOS - Developing Design Know-how ...
    (comp.lang.vhdl)