How to eliminate this global variable, silent?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • istillshine@gmail.com

    How to eliminate this global variable, silent?

    When I control if I print messages, I usually use a global variable
    "int silent". When I set "-silent" flag in my command line
    parameters, I set silent = 1 in my main.c.


    I have many functions that may print some messages.

    foo(...)
    {
    if (!silent)
    printf("Msg1\n" );
    }


    foo2(...)
    {
    if (!silent)
    printf("Msg2\n" );
    }


    and so on...


    Is the above bad coding practice? How to eliminate the variable
    "silent" but achieve the same effect?
  • Barry Schwarz

    #2
    Re: How to eliminate this global variable, silent?

    On Fri, 11 Apr 2008 21:31:41 -0700 (PDT), istillshine@gma il.com wrote:
    >When I control if I print messages, I usually use a global variable
    >"int silent". When I set "-silent" flag in my command line
    >parameters, I set silent = 1 in my main.c.
    >
    >
    >I have many functions that may print some messages.
    >
    >foo(...)
    >{
    if (!silent)
    printf("Msg1\n" );
    >}
    >
    >
    >foo2(...)
    >{
    if (!silent)
    printf("Msg2\n" );
    >}
    >
    >
    >and so on...
    >
    >
    >Is the above bad coding practice? How to eliminate the variable
    >"silent" but achieve the same effect?
    If you are using for debugging, it is fine.


    Remove del for email

    Comment

    • Ian Collins

      #3
      Re: How to eliminate this global variable, silent?

      istillshine@gma il.com wrote:
      When I control if I print messages, I usually use a global variable
      "int silent". When I set "-silent" flag in my command line
      parameters, I set silent = 1 in my main.c.
      >
      >
      I have many functions that may print some messages.
      >
      foo(...)
      {
      if (!silent)
      printf("Msg1\n" );
      }
      >
      >
      foo2(...)
      {
      if (!silent)
      printf("Msg2\n" );
      }
      >
      >
      and so on...
      >
      >
      Is the above bad coding practice?
      No, it's quite common for debugging.
      How to eliminate the variable "silent" but achieve the same effect?
      Create a wrapper for printf and link with either a debug or production
      version. If you simply want to remove the global, make silent static
      and expose it through a getSilent() function.

      --
      Ian Collins.

      Comment

      • Pradeep

        #4
        Re: How to eliminate this global variable, silent?

        Hi,
        Here is modification for your program.
        --------------
        foo()
        {
        #if !SILENT
        printf("Msg1\n" );
        #endif

        }

        foo2()
        {
        #if !SILENT
        printf("Msg2\n" );
        #endif
        }
        main()
        {
        foo();
        foo2();
        }
        --------------
        command: gcc -D SILENT foo.c
        $add '-D SILENT' to compiler option
        Your program should work.
        i have tested successfully in gcc-4.0 compiler.

        -- Pradeep
        On Apr 12, 10:16 am, Ian Collins <ian-n...@hotmail.co mwrote:
        istillsh...@gma il.com wrote:
        When I control if I print messages, I usually use a global variable
        "int silent". When I set "-silent" flag in my command line
        parameters, I set silent = 1 in my main.c.
        >
        I have many functions that may print some messages.
        >
        foo(...)
        {
        if (!silent)
        printf("Msg1\n" );
        }
        >
        foo2(...)
        {
        if (!silent)
        printf("Msg2\n" );
        }
        >
        and so on...
        >
        Is the above bad coding practice?
        >
        No, it's quite common for debugging.
        >
        How to eliminate the variable "silent" but achieve the same effect?
        >
        Create a wrapper for printf and link with either a debug or production
        version. If you simply want to remove the global, make silent static
        and expose it through a getSilent() function.
        >
        --
        Ian Collins.

        Comment

        • istillshine@gmail.com

          #5
          Re: How to eliminate this global variable, silent?

          On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.co mwrote:
          Create a wrapper for printf and link with either a debug or production
          version.
          Suppose the wrapper name is called info. How to implement it? I
          tried to write it,as shown below. But failed to do it.


          #ifdef DEBUG
          #define info() /* how ? */
          #else
          #define info() /* how? */
          #endif /* DEBUG */

          Comment

          • Eric Sosman

            #6
            Re: How to eliminate this global variable, silent?

            istillshine@gma il.com wrote:
            On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.co mwrote:
            >
            >Create a wrapper for printf and link with either a debug or production
            >version.
            >
            Suppose the wrapper name is called info. How to implement it? I
            tried to write it,as shown below. But failed to do it.
            >
            >
            #ifdef DEBUG
            #define info() /* how ? */
            #else
            #define info() /* how? */
            #endif /* DEBUG */
            Are you the same "istillshin e" who in another thread
            is lecturing us all about the use of `const'? If so, I
            hope you'll pardon me for declining to accept advice from
            someone who can't even read the FAQ. Question 10.26, to
            be specific.

            --
            Eric Sosman
            esosman@ieee-dot-org.invalid

            Comment

            • istillshine@gmail.com

              #7
              Re: How to eliminate this global variable, silent?

              On Apr 12, 10:46 am, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
              Are you the same "istillshin e" who in another thread
              is lecturing us all about the use of `const'?
              I think "lecturing" was not my attitude when I posted that thread. If
              it appeared to be so, I am sorry. I posted it because I felt confused
              on that point. I wanted to see what other people think.

              If so, I
              hope you'll pardon me for declining to accept advice from
              someone who can't even read the FAQ. Question 10.26, to
              be specific.
              Oh it's there. I did not notice it. Thank you for pointing it out to
              me.

              Comment

              • Ben Bacarisse

                #8
                Re: How to eliminate this global variable, silent?

                Pradeep <pradeep.sancha na@gmail.comwri tes:
                Here is modification for your program.
                --------------
                foo()
                {
                #if !SILENT
                printf("Msg1\n" );
                #endif
                }
                <snip more>

                That is a modification that reduces the usability of the program. The
                original had a run-time settable "silent" option.

                For many simple programs I would not mind a few "global"[1] variables
                that control the overall behaviour of the system. However, since
                flags tend to breed, I'd consider:

                struct program_options {
                unsigned silent: 1;
                /* there will be more, I can feel it */
                };
                struct program_options _s program_options ;

                Now, having done that[2], why put the other "global" data in there as
                well (and maybe change its name). Then, when your program becomes a
                component in a bigger system, you are already set. After all,
                "global" variables should have nice long names, and

                program_options .silent

                is not much harder to use than

                program_options _silent

                [1] Can we not permit the phrase "global variable" into c.l.c to mean
                an object defined at file scope with external linkage? It would save
                a lot of typing.

                [2] The down side, is that one can't use the 'silent' member in many
                option parsing systems since you can't take its address. If that
                matters, don't make it a bit field.

                --
                Ben.

                Comment

                • Keith Thompson

                  #9
                  Re: How to eliminate this global variable, silent?

                  Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                  [...]
                  [1] Can we not permit the phrase "global variable" into c.l.c to mean
                  an object defined at file scope with external linkage? It would save
                  a lot of typing.
                  [...]

                  That works for me *if* we expend just a little extra typing to make
                  clear how the term is being used, since some people do use the term
                  with subtly different meanings.

                  Hypothetical example:

                  QI've heard that global variables are a bad idea, but I think
                  Qthey're really cool.

                  AAssuming that "global variables" refers to "objects defined at file
                  Ascope with external linkage", here's why they're usually a bad idea
                  A...

                  (I think of "global variables" as a general programming concept, with
                  "objects defined at file scope with external linkage" being the way to
                  implement that concept in C. It's analogous to the use of pointer
                  parameters (a C construct) used to implement pass-by-reference (a more
                  general programming concept).)

                  Note that the above still assumes that a const-qualified object is a
                  "variable", even though it's not able to vary. I'm willing to ignore
                  that quibble as long as it's clearly not relevant to the particular
                  case being discussed.

                  Note also that the use of the term "global" can tend to obscure the
                  important distinction between scope and storage duration.

                  Even though C doesn't define either "global" or "variable", I don't
                  mind using either or both as a convenient shorthand *as long as* the
                  usage is unambiguous (or at least as unambiguous as it needs to be).

                  --
                  Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Richard

                    #10
                    Re: How to eliminate this global variable, silent?


                    Keith Thompson <kst-u@mib.orgwrites :
                    Even though C doesn't define either "global" or "variable", I don't
                    mind using either or both as a convenient shorthand *as long as* the
                    usage is unambiguous (or at least as unambiguous as it needs to be).
                    What a load of hot air.

                    It should be apparent to an sentient being what a "variable" is in a
                    programming newsgroup. Even this one. Ditto for "global".


                    Comment

                    • lawrence.jones@siemens.com

                      #11
                      Re: How to eliminate this global variable, silent?

                      Richard <devr_@gmail.co mwrote:
                      >
                      It should be apparent to an sentient being what a "variable" is in a
                      programming newsgroup. Even this one. Ditto for "global".
                      "Variable" shouldn't be controversial, even the C Standard uses the
                      term. But "global variable" is a slipperier concept in C. For example,
                      does a file scope static variable qualify or not? It's not exactly a
                      local variable, but it's not exactly a global variable, either.

                      -Larry Jones

                      OK, there IS a middle ground, but it's for sissy weasels. -- Calvin

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: How to eliminate this global variable, silent?

                        lawrence.jones@ siemens.com writes:
                        Richard <devr_@gmail.co mwrote:
                        >>
                        >It should be apparent to an sentient being what a "variable" is in a
                        >programming newsgroup. Even this one. Ditto for "global".
                        >
                        "Variable" shouldn't be controversial, even the C Standard uses the
                        term. But "global variable" is a slipperier concept in C. For example,
                        does a file scope static variable qualify or not? It's not exactly a
                        local variable, but it's not exactly a global variable, either.
                        It is a grey area, but I did explicitly exclude it.

                        --
                        Ben.

                        Comment

                        • Ben Bacarisse

                          #13
                          Re: How to eliminate this global variable, silent?

                          Richard <devr_@gmail.co mwrites:
                          lawrence.jones@ siemens.com writes:
                          >
                          >Richard <devr_@gmail.co mwrote:
                          >>>
                          >>It should be apparent to an sentient being what a "variable" is in a
                          >>programming newsgroup. Even this one. Ditto for "global".
                          >>
                          >"Variable" shouldn't be controversial, even the C Standard uses the
                          >term. But "global variable" is a slipperier concept in C. For example,
                          >does a file scope static variable qualify or not? It's not exactly a
                          >
                          No. Because its file scope. Not global scope. I don think I'm being
                          overly simplistic here.
                          You (correctly in my opinion) exclude it, but you should exclude it
                          because it does not have external linkage. This variable:

                          int x;

                          outside of any function is also a file scape object, and you probably
                          *do* call that a global.

                          --
                          Ben.

                          Comment

                          • Richard Heathfield

                            #14
                            Re: How to eliminate this global variable, silent?

                            Ben Bacarisse said:
                            Richard <devr_@gmail.co mwrites:
                            >
                            >lawrence.jones@ siemens.com writes:
                            >>
                            >>Richard <devr_@gmail.co mwrote:
                            >>>>
                            >>>It should be apparent to an sentient being what a "variable" is in a
                            >>>programmin g newsgroup. Even this one. Ditto for "global".
                            >>>
                            >>"Variable" shouldn't be controversial, even the C Standard uses the
                            >>term. But "global variable" is a slipperier concept in C. For
                            >>example,
                            >>does a file scope static variable qualify or not? It's not exactly a
                            >>
                            >No. Because its file scope. Not global scope. I don think I'm being
                            >overly simplistic here.
                            >
                            You (correctly in my opinion) exclude it, but you should exclude it
                            because it does not have external linkage.
                            Excluding it because it doesn't have global scope is perfectly correct.
                            "There are four kinds of scopes: function, file, block, and function
                            prototype", quoth the Standard. Since no object can be considered global,
                            even by a troll, unless it has global scope, and since there is no such
                            thing as global scope in C, *therefore* C doesn't have global objects (or
                            "variables" , as some people like to call them). And even the trolls agree
                            with this.

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • Richard Harter

                              #15
                              Re: How to eliminate this global variable, silent?

                              On Tue, 15 Apr 2008 04:54:21 +0000, Richard Heathfield
                              <rjh@see.sig.in validwrote:
                              >Ben Bacarisse said:
                              >
                              >Richard <devr_@gmail.co mwrites:
                              >>
                              >>lawrence.jones@ siemens.com writes:
                              >>>
                              >>>Richard <devr_@gmail.co mwrote:
                              >>>>>
                              >>>>It should be apparent to an sentient being what a "variable" is in a
                              >>>>programmi ng newsgroup. Even this one. Ditto for "global".
                              >>>>
                              >>>"Variable" shouldn't be controversial, even the C Standard uses the
                              >>>term. But "global variable" is a slipperier concept in C. For
                              >>>example,
                              >>>does a file scope static variable qualify or not? It's not exactly a
                              >>>
                              >>No. Because its file scope. Not global scope. I don think I'm being
                              >>overly simplistic here.
                              >>
                              >You (correctly in my opinion) exclude it, but you should exclude it
                              >because it does not have external linkage.
                              >
                              >Excluding it because it doesn't have global scope is perfectly correct.
                              >"There are four kinds of scopes: function, file, block, and function
                              >prototype", quoth the Standard. Since no object can be considered global,
                              >even by a troll, unless it has global scope, and since there is no such
                              >thing as global scope in C, *therefore* C doesn't have global objects (or
                              >"variables" , as some people like to call them). And even the trolls agree
                              >with this.
                              For some particular breed of troll, of course. Some of us trolls
                              wouldn't use that definition for global.


                              Richard Harter, cri@tiac.net
                              http://home.tiac.net/~cri, http://www.varinoma.com
                              Save the Earth now!!
                              It's the only planet with chocolate.

                              Comment

                              Working...