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. How to Use Member Variables

How to Use Member Variables

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorial
23 Posts 12 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.
  • N Nagy Vilmos

    I've been looking at some code today written by a former slave. Now instead of using member variables in methods, dufus decided to pass them all around the shop as arguments to STATIC methods so we get crapola like this:

    class DoofusCode {
    private int first;
    private String second;

    // more stuff [tm]

    public void Method()
    {
    if (IsFirst(first))
    UpdateSecond(out second);
    }

    private static bool IsFirst(int first)
    {
    // more stuff [tm]
    return answer;
    }

    private static void IsFirst(out String second)
    {
    second = "Second";
    }
    }

    Go Optimisers! Go!


    Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    if the class members aren't static and the methods are, what do you want your slaves to do? anyway, if you don't educate and whip them enough, it is your problem, you're the master after all. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

    N 1 Reply Last reply
    0
    • L Luc Pattyn

      if the class members aren't static and the methods are, what do you want your slaves to do? anyway, if you don't educate and whip them enough, it is your problem, you're the master after all. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

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

      There is no need for the methods to be static. Numpty made them static to get rid of the build warnings that the methods didn't reference any member variables.


      Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

      L 1 Reply Last reply
      0
      • N Nagy Vilmos

        There is no need for the methods to be static. Numpty made them static to get rid of the build warnings that the methods didn't reference any member variables.


        Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        Nagy Vilmos wrote:

        the build warnings that the methods didn't reference any member variables

        Interesting. While I always build my C# apps at warning level 4, I've never seen such a warning, and I seem unable to get one on purpose either. :confused:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        R 1 Reply Last reply
        0
        • N Nagy Vilmos

          I've been looking at some code today written by a former slave. Now instead of using member variables in methods, dufus decided to pass them all around the shop as arguments to STATIC methods so we get crapola like this:

          class DoofusCode {
          private int first;
          private String second;

          // more stuff [tm]

          public void Method()
          {
          if (IsFirst(first))
          UpdateSecond(out second);
          }

          private static bool IsFirst(int first)
          {
          // more stuff [tm]
          return answer;
          }

          private static void IsFirst(out String second)
          {
          second = "Second";
          }
          }

          Go Optimisers! Go!


          Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

          R Offline
          R Offline
          Rob Grainger
          wrote on last edited by
          #5

          Makes you realise the utility of code reviews. At least then someone gets to ask why its been done that way.

          1 Reply Last reply
          0
          • L Luc Pattyn

            Nagy Vilmos wrote:

            the build warnings that the methods didn't reference any member variables

            Interesting. While I always build my C# apps at warning level 4, I've never seen such a warning, and I seem unable to get one on purpose either. :confused:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            R Offline
            R Offline
            Robert Rohde
            wrote on last edited by
            #6

            I would guess the warning comes from some 3rd party tool like Resharper.

            L 1 Reply Last reply
            0
            • R Robert Rohde

              I would guess the warning comes from some 3rd party tool like Resharper.

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

              It does.. Recommending to make a method static if it doesn't refer any member variables. A horror-hint :) The fact that a method "could be" static, doesn't mean it "should be". If things were that simple, the compiler could simple upgrade every method to be static once it doesn't find any references to member-variables. Besides that, quite a neat tool. If your PC is fast enough :)

              I are Troll :suss:

              L R 2 Replies Last reply
              0
              • L Lost User

                It does.. Recommending to make a method static if it doesn't refer any member variables. A horror-hint :) The fact that a method "could be" static, doesn't mean it "should be". If things were that simple, the compiler could simple upgrade every method to be static once it doesn't find any references to member-variables. Besides that, quite a neat tool. If your PC is fast enough :)

                I are Troll :suss:

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                explicitly making a method static can be useful as it turns every access to an instance member into an error. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                E 1 Reply Last reply
                0
                • N Nagy Vilmos

                  I've been looking at some code today written by a former slave. Now instead of using member variables in methods, dufus decided to pass them all around the shop as arguments to STATIC methods so we get crapola like this:

                  class DoofusCode {
                  private int first;
                  private String second;

                  // more stuff [tm]

                  public void Method()
                  {
                  if (IsFirst(first))
                  UpdateSecond(out second);
                  }

                  private static bool IsFirst(int first)
                  {
                  // more stuff [tm]
                  return answer;
                  }

                  private static void IsFirst(out String second)
                  {
                  second = "Second";
                  }
                  }

                  Go Optimisers! Go!


                  Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                  R Offline
                  R Offline
                  RugbyLeague
                  wrote on last edited by
                  #9

                  "More stuff" is trademarked? :(

                  1 Reply Last reply
                  0
                  • L Lost User

                    It does.. Recommending to make a method static if it doesn't refer any member variables. A horror-hint :) The fact that a method "could be" static, doesn't mean it "should be". If things were that simple, the compiler could simple upgrade every method to be static once it doesn't find any references to member-variables. Besides that, quite a neat tool. If your PC is fast enough :)

                    I are Troll :suss:

                    R Offline
                    R Offline
                    Robert Rohde
                    wrote on last edited by
                    #10

                    Well those tools are normally intelligent enough to generate such warnings only for private methods. I can't think of a situation where a private method which doesn't access any member variables shouldn't be static (except temporarily while developing). And even if there are, you can still ignore it :).

                    R 1 Reply Last reply
                    0
                    • N Nagy Vilmos

                      I've been looking at some code today written by a former slave. Now instead of using member variables in methods, dufus decided to pass them all around the shop as arguments to STATIC methods so we get crapola like this:

                      class DoofusCode {
                      private int first;
                      private String second;

                      // more stuff [tm]

                      public void Method()
                      {
                      if (IsFirst(first))
                      UpdateSecond(out second);
                      }

                      private static bool IsFirst(int first)
                      {
                      // more stuff [tm]
                      return answer;
                      }

                      private static void IsFirst(out String second)
                      {
                      second = "Second";
                      }
                      }

                      Go Optimisers! Go!


                      Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #11

                      You messed up the code. Shouldnt the second IsFirst method be called UpdateSecond ?

                      xacc.ide
                      IronScheme - 1.0 RC 1 - out now!
                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition

                      1 Reply Last reply
                      0
                      • R Robert Rohde

                        Well those tools are normally intelligent enough to generate such warnings only for private methods. I can't think of a situation where a private method which doesn't access any member variables shouldn't be static (except temporarily while developing). And even if there are, you can still ignore it :).

                        R Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #12

                        Funny, I have the opposite viewpoint. I regard static members of any type - including such methods - as a code smell. If a method has requires no access to an instance's state, is it really a method at all, are we really doing object-oriented programming here? Gilad Bracha's Room 101[^] has some excellent posts on the problems of static. Particular, look for "Constructors considered harmful" (they're a form of static method), and "Cutting out static".

                        G 1 Reply Last reply
                        0
                        • R Rob Grainger

                          Funny, I have the opposite viewpoint. I regard static members of any type - including such methods - as a code smell. If a method has requires no access to an instance's state, is it really a method at all, are we really doing object-oriented programming here? Gilad Bracha's Room 101[^] has some excellent posts on the problems of static. Particular, look for "Constructors considered harmful" (they're a form of static method), and "Cutting out static".

                          G Offline
                          G Offline
                          Gary Wheeler
                          wrote on last edited by
                          #13

                          Rob Grainger wrote:

                          If a method has requires no access to an instance's state, is it really a method at all, are we really doing object-oriented programming here?

                          Ah, ladies and gentlemen, we have a purist in our midst! One of the things OOP enthusiasts forget is that one of the purposes of classes (as opposed to objects) is to organize behavior. A method that does not reference an instance state might still be perfectly appropriate if it implements a behavior relevant to the class. The method might implement an operation or perform a calculation for objects of the class type that depends solely on arguments to the operation.

                          Software Zen: delete this;

                          A 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            explicitly making a method static can be useful as it turns every access to an instance member into an error. :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                            E Offline
                            E Offline
                            ely_bob
                            wrote on last edited by
                            #14

                            And reentrant code is happy code. :laugh:

                            I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

                            1 Reply Last reply
                            0
                            • G Gary Wheeler

                              Rob Grainger wrote:

                              If a method has requires no access to an instance's state, is it really a method at all, are we really doing object-oriented programming here?

                              Ah, ladies and gentlemen, we have a purist in our midst! One of the things OOP enthusiasts forget is that one of the purposes of classes (as opposed to objects) is to organize behavior. A method that does not reference an instance state might still be perfectly appropriate if it implements a behavior relevant to the class. The method might implement an operation or perform a calculation for objects of the class type that depends solely on arguments to the operation.

                              Software Zen: delete this;

                              A Offline
                              A Offline
                              Anna Jayne Metcalfe
                              wrote on last edited by
                              #15

                              He's right though. Although statics have their place, they are so frequently abused (a "lesser goto" seems a reasonable analogy) that instinctively associating their prescence in a codebase with a "smell" (particularly when said code was written by someone lacking experience) is perfectly natural. Beginner beware, as ever.

                              Anna :rose: Tech Blog | Visual Lint "Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

                              G N 2 Replies Last reply
                              0
                              • A Anna Jayne Metcalfe

                                He's right though. Although statics have their place, they are so frequently abused (a "lesser goto" seems a reasonable analogy) that instinctively associating their prescence in a codebase with a "smell" (particularly when said code was written by someone lacking experience) is perfectly natural. Beginner beware, as ever.

                                Anna :rose: Tech Blog | Visual Lint "Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

                                G Offline
                                G Offline
                                Gary Wheeler
                                wrote on last edited by
                                #16

                                Anna-Jayne Metcalfe wrote:

                                particularly when said code was written by someone lacking experience

                                That explains why my point of view is biased. We don't have anyone 'lacking experience' in my group. The original comment struck me as a generalization that I don't usually see. If a member or an entire class is static, there's usually a good reason for it.

                                Software Zen: delete this;

                                A U 2 Replies Last reply
                                0
                                • G Gary Wheeler

                                  Anna-Jayne Metcalfe wrote:

                                  particularly when said code was written by someone lacking experience

                                  That explains why my point of view is biased. We don't have anyone 'lacking experience' in my group. The original comment struck me as a generalization that I don't usually see. If a member or an entire class is static, there's usually a good reason for it.

                                  Software Zen: delete this;

                                  A Offline
                                  A Offline
                                  Anna Jayne Metcalfe
                                  wrote on last edited by
                                  #17

                                  Same here; however we do still find we've used it where we probably shouldn't from time to time. Fortunately, we're not shy about aggressive refactoring so it's usually no big deal to change it when required.

                                  Anna :rose: Tech Blog | Visual Lint "Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

                                  G 1 Reply Last reply
                                  0
                                  • A Anna Jayne Metcalfe

                                    Same here; however we do still find we've used it where we probably shouldn't from time to time. Fortunately, we're not shy about aggressive refactoring so it's usually no big deal to change it when required.

                                    Anna :rose: Tech Blog | Visual Lint "Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

                                    G Offline
                                    G Offline
                                    Gary Wheeler
                                    wrote on last edited by
                                    #18

                                    Anna-Jayne Metcalfe wrote:

                                    we're not shy about aggressive refactoring

                                    Anna? Shy? Pshaw :-D. Seriously, though, that's one of the things I'm appreciating more and more using C# and .NET. The metadata available to the IDE enables some seriously cool features that lower the barriers to frequent refactoring.

                                    Software Zen: delete this;

                                    A 1 Reply Last reply
                                    0
                                    • G Gary Wheeler

                                      Anna-Jayne Metcalfe wrote:

                                      we're not shy about aggressive refactoring

                                      Anna? Shy? Pshaw :-D. Seriously, though, that's one of the things I'm appreciating more and more using C# and .NET. The metadata available to the IDE enables some seriously cool features that lower the barriers to frequent refactoring.

                                      Software Zen: delete this;

                                      A Offline
                                      A Offline
                                      Anna Jayne Metcalfe
                                      wrote on last edited by
                                      #19

                                      Gary Wheeler wrote:

                                      Anna? Shy? Pshaw

                                      Believe it or not it still happens occasionally. Put me in an unfamiliar environment where I don't know anyone and I still clam up a bit too much for my liking.

                                      Gary Wheeler wrote:

                                      Seriously, though, that's one of the things I'm appreciating more and more using C# and .NET. The metadata available to the IDE enables some seriously cool features that lower the barriers to frequent refactoring.

                                      I can imagine. I still prefer C++ though (particularly with the 0x bits added in - the old girl just got a turbocharge!).

                                      Anna :rose: Tech Blog | Visual Lint "Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

                                      1 Reply Last reply
                                      0
                                      • G Gary Wheeler

                                        Anna-Jayne Metcalfe wrote:

                                        particularly when said code was written by someone lacking experience

                                        That explains why my point of view is biased. We don't have anyone 'lacking experience' in my group. The original comment struck me as a generalization that I don't usually see. If a member or an entire class is static, there's usually a good reason for it.

                                        Software Zen: delete this;

                                        U Offline
                                        U Offline
                                        User 4223959
                                        wrote on last edited by
                                        #20

                                        Gary Wheeler wrote:

                                        If a member or an entire class is static,

                                        All inner classes are static in C# - no complaints about them? (Java inner classes may instance-based or static). So not point in considering static methods evil. While the hint purpose primary is to point into discrepancy in the design ("did you mean that? Maybe you had something else in mind?"), the static methods would be slightly better optimised in runtime too.

                                        1 Reply Last reply
                                        0
                                        • A Anna Jayne Metcalfe

                                          He's right though. Although statics have their place, they are so frequently abused (a "lesser goto" seems a reasonable analogy) that instinctively associating their prescence in a codebase with a "smell" (particularly when said code was written by someone lacking experience) is perfectly natural. Beginner beware, as ever.

                                          Anna :rose: Tech Blog | Visual Lint "Why would anyone prefer to wield a weapon that takes both hands at once, when they could use a lighter (and obviously superior) weapon that allows you to wield multiple ones at a time, and thus supports multi-paradigm carnage?"

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

                                          Thank-you AJ! I take the view that static should be avoided, but there are times were expedience allows them.


                                          Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                                          A 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