Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. What one Should do if Program warns you about one's uninitialized variable?

What one Should do if Program warns you about one's uninitialized variable?

Scheduled Pinned Locked Moved The Weird and The Wonderful
helpquestion
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohibur Rashid
    wrote on last edited by
    #1

    My Priors Idea is hide it. So from the Servers Initialization file Error and Warning is completely disabled during development time.

    N OriginalGriffO 2 Replies Last reply
    0
    • M Mohibur Rashid

      My Priors Idea is hide it. So from the Servers Initialization file Error and Warning is completely disabled during development time.

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      Switch on warnings in production and test. Switch on errors - if possible - on the dev box and get them fixed. Simples. At my last job, we had autobuilds of all the products over-night and it was a mater of pride that the builds were clean. For the C# stuff we had FxCop running in Strict South American Police State mode.


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      J 1 Reply Last reply
      0
      • N Nagy Vilmos

        Switch on warnings in production and test. Switch on errors - if possible - on the dev box and get them fixed. Simples. At my last job, we had autobuilds of all the products over-night and it was a mater of pride that the builds were clean. For the C# stuff we had FxCop running in Strict South American Police State mode.


        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

        J Offline
        J Offline
        Julien Villers
        wrote on last edited by
        #3

        Nagy Vilmos wrote:

        For the C# stuff we had FxCop running in Strict South American Police State mode.

        I'm just curious, how many of your team did make it to release? :laugh:

        'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

        N 1 Reply Last reply
        0
        • J Julien Villers

          Nagy Vilmos wrote:

          For the C# stuff we had FxCop running in Strict South American Police State mode.

          I'm just curious, how many of your team did make it to release? :laugh:

          'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          Mostly all releases would go out the door with clean builds. There was a lot of rivalry, still is, between the teams about going the most days without a failed build. The dev builds would intentionally fail on any warnings. That meant nobody could get an up to date build once shitty code was checked in. The result was that people learnt to check for warnings before they checked in their code. You'd get, on average, one or two build fails a week out of a dozen products being built daily.


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          J J 2 Replies Last reply
          0
          • N Nagy Vilmos

            Mostly all releases would go out the door with clean builds. There was a lot of rivalry, still is, between the teams about going the most days without a failed build. The dev builds would intentionally fail on any warnings. That meant nobody could get an up to date build once shitty code was checked in. The result was that people learnt to check for warnings before they checked in their code. You'd get, on average, one or two build fails a week out of a dozen products being built daily.


            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

            J Offline
            J Offline
            Julien Villers
            wrote on last edited by
            #5

            I do not doubt the very positive effects on quality that such settings gain. I'm doubting your *slightly* exaggerated qualification for your build rules. Real SA police mode would have got your team members to disappear suddenly ;) I was thinking along the lines of this sketch (in French): http://www.youtube.com/watch?feature=player_detailpage&v=lPtUZASxCqk#t=306s[^] For the meteo part at the end, it translates to "Good evening, (...), maybe a little cloud here...".

            'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

            1 Reply Last reply
            0
            • M Mohibur Rashid

              My Priors Idea is hide it. So from the Servers Initialization file Error and Warning is completely disabled during development time.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              In VS there is a very handy little option: Project Properties, Build tab, "Treat warnings as errors" - switch it to "All". If you don't fix what it complains about, you can't run it. Modern compilers complain for good reason - it's not like the old days - and if they say "this is not initialized" then it isn't - and if it doesn't give you a problem now, it will later on when you have forgotten that you are ignoring the warning. That's when it gets expensive to fix a problem...because the code that causes it is is a different module, in a different DLL! Ignoring warnings is lazy stupidity, and you can tell him that from me!

              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              M 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                In VS there is a very handy little option: Project Properties, Build tab, "Treat warnings as errors" - switch it to "All". If you don't fix what it complains about, you can't run it. Modern compilers complain for good reason - it's not like the old days - and if they say "this is not initialized" then it isn't - and if it doesn't give you a problem now, it will later on when you have forgotten that you are ignoring the warning. That's when it gets expensive to fix a problem...because the code that causes it is is a different module, in a different DLL! Ignoring warnings is lazy stupidity, and you can tell him that from me!

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                M Offline
                M Offline
                Mohibur Rashid
                wrote on last edited by
                #7

                Actually Its PHP. It has the capabilities to overlook warning and errors but the worse case occurs in case of syntax errors. If there is a syntax error the entire page goes blank. Than I had read carefully to get the syntax error :)

                1 Reply Last reply
                0
                • N Nagy Vilmos

                  Mostly all releases would go out the door with clean builds. There was a lot of rivalry, still is, between the teams about going the most days without a failed build. The dev builds would intentionally fail on any warnings. That meant nobody could get an up to date build once shitty code was checked in. The result was that people learnt to check for warnings before they checked in their code. You'd get, on average, one or two build fails a week out of a dozen products being built daily.


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  J Offline
                  J Offline
                  jsc42
                  wrote on last edited by
                  #8

                  Whilst checking for warnings is important, it can (as you point out) become so overriding that checking for the code working becomes insignificant. The attitude becomes "Oh dear, variable 'x' is uninitialised, I'll just change it from int x; to int x = 0;. Phew! Job done." without even stopping to consider what x is being used for. Far better to have something like

                  int x; // Value defined in the inner 'for' loop, we cannot determine what it will be yet

                  and just accept that there will be a warning. I have seen code that looks like

                  int x = 0; // Value defined in the inner 'for' loop. The 0 is a dummy value only to stop warning messages and actually is never correct in the 'for' loop below

                  N B Y 3 Replies Last reply
                  0
                  • J jsc42

                    Whilst checking for warnings is important, it can (as you point out) become so overriding that checking for the code working becomes insignificant. The attitude becomes "Oh dear, variable 'x' is uninitialised, I'll just change it from int x; to int x = 0;. Phew! Job done." without even stopping to consider what x is being used for. Far better to have something like

                    int x; // Value defined in the inner 'for' loop, we cannot determine what it will be yet

                    and just accept that there will be a warning. I have seen code that looks like

                    int x = 0; // Value defined in the inner 'for' loop. The 0 is a dummy value only to stop warning messages and actually is never correct in the 'for' loop below

                    N Offline
                    N Offline
                    Nagy Vilmos
                    wrote on last edited by
                    #9

                    Firstly, your assertion is slightly wrong. Uninitialised variables generate a warning if the code path allows them to be accessed before being set. As an example the following will cause a build warning on the last line:

                    Integer x;
                    if (something()) {
                    x = 3;
                    }

                    int y = x * 4;

                    Secondly, without a good reason, variables used within a loop should be defined in the loop.


                    Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                    L 1 Reply Last reply
                    0
                    • J jsc42

                      Whilst checking for warnings is important, it can (as you point out) become so overriding that checking for the code working becomes insignificant. The attitude becomes "Oh dear, variable 'x' is uninitialised, I'll just change it from int x; to int x = 0;. Phew! Job done." without even stopping to consider what x is being used for. Far better to have something like

                      int x; // Value defined in the inner 'for' loop, we cannot determine what it will be yet

                      and just accept that there will be a warning. I have seen code that looks like

                      int x = 0; // Value defined in the inner 'for' loop. The 0 is a dummy value only to stop warning messages and actually is never correct in the 'for' loop below

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #10

                      I've not seen this with locals too often (the code walker is quite good at checking them), but it is possible to set up conditions such that the code really, honestly, has to assign to x before reading it, but the compiler can't work it out. I think you can do it with

                      int x;
                      if(a) x = 5;
                      // ... some other processing
                      if(a){
                      // something with x, e.g.
                      return x;
                      }

                      What seems to be more common is the 'member X is declared but never used', if you're implementing an interface but not actually using it. For example let's say we have a collection of objects that we're listening to notifications on, but some of the classes don't actually need to notify:

                      class DontBother : INotifyPropertyChanged {
                      public event PropertyChangedEventHandler PropertyChanged;
                      }

                      class MainClass {
                      List<INotifyPropertyChanged> sources;

                      void SomeMethod(){
                      sources.Add(new DontBother());
                      // Along with some meaningful classes, obviously
                      }
                      }

                      The declaration in DontBother will, I think, give you a compiler warning. Maybe it's not exactly this scenario, I forget, but it's similar. Sometimes the only way to get a clean compile is to use #pragma warning disable :^) .

                      1 Reply Last reply
                      0
                      • N Nagy Vilmos

                        Firstly, your assertion is slightly wrong. Uninitialised variables generate a warning if the code path allows them to be accessed before being set. As an example the following will cause a build warning on the last line:

                        Integer x;
                        if (something()) {
                        x = 3;
                        }

                        int y = x * 4;

                        Secondly, without a good reason, variables used within a loop should be defined in the loop.


                        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Nagy Vilmos wrote:

                        variables used within a loop should be defined in the loop

                        Surely you meant inside condition, not loop? (BTW, I know your name is not Shirley ;))

                        1 Reply Last reply
                        0
                        • J jsc42

                          Whilst checking for warnings is important, it can (as you point out) become so overriding that checking for the code working becomes insignificant. The attitude becomes "Oh dear, variable 'x' is uninitialised, I'll just change it from int x; to int x = 0;. Phew! Job done." without even stopping to consider what x is being used for. Far better to have something like

                          int x; // Value defined in the inner 'for' loop, we cannot determine what it will be yet

                          and just accept that there will be a warning. I have seen code that looks like

                          int x = 0; // Value defined in the inner 'for' loop. The 0 is a dummy value only to stop warning messages and actually is never correct in the 'for' loop below

                          Y Offline
                          Y Offline
                          YvesDaoust
                          wrote on last edited by
                          #12

                          There could be a cleaner, pedantic-like way:

                          #pragma warning(suppress: 4700)
                          int x; // Value defined in the inner 'for' loop, we cannot determine what it will be yet, hence the pragma

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups