Like Tree2Likes
  • 2 Post By nvoigt

which code is better. why?

This is a discussion on which code is better. why? within the C# Programming forums, part of the General Programming Boards category; The following code prints a list of permissions, permission sets and security policies available on a given machine. Code: static ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    which code is better. why?

    The following code prints a list of permissions, permission sets and security policies available on a given machine.

    Code:
            static void Main(string[] args)
            {
                IEnumerator ie = SecurityManager.PolicyHierarchy();
    
                while(ie.MoveNext())
                {
                    Console.WriteLine(((PolicyLevel)ie.Current).Label);
                    foreach (NamedPermissionSet nps in ((PolicyLevel)ie).NamedPermissionSets)
                    {
                        Console.WriteLine("\t"+nps.Name);
                        foreach (IPermission ip in nps)
                        {
                            Console.WriteLine("\t\t" + ip.GetType().Name);
                        }
                    }
    
                }
                Console.Read();
            }
    The following code does the same thing, but instead of bothering with the IEnumerator's MoveNext method and Current property I put the code inside an iterator block that yield returns the elements of the IEnumerator object and allows us to use a foreach loop.

    Code:
            static IEnumerable GetIEnumerable(IEnumerator ie)
            {
                while (ie.MoveNext()) yield return ie.Current;
            }
    
            static void Main(string[] args)
            {
                foreach(var ie in GetIEnumerable(SecurityManager.PolicyHierarchy()))
                {
                    Console.WriteLine(((PolicyLevel)ie).Label);
                    foreach (NamedPermissionSet nps in ((PolicyLevel)ie).NamedPermissionSets)
                    {
                        Console.WriteLine("\t"+nps.Name);
                        foreach (IPermission ip in nps)
                        {
                            Console.WriteLine("\t\t" + ip.GetType().Name);
                        }
                    }
    
                }
                Console.Read();
            }
    The question is, which code is "better", and why?

    (I like the first code better because it keeps all the work inside the Main method. But the second code is easier to read because it does all the iterations using foreach loops.)

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Both versions seem bad because one uses the enumerator directly and the other is just a workaround. When looking up the function to check why you are doing this, the first thing I noticed is that it's obsolete and will throw exceptions. Maybe you should scrap both versions and try to find out how to solve your problem with the current version?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    71
    Quote Originally Posted by nvoigt View Post
    Both versions seem bad because one uses the enumerator directly and the other is just a workaround. When looking up the function to check why you are doing this, the first thing I noticed is that it's obsolete and will throw exceptions. Maybe you should scrap both versions and try to find out how to solve your problem with the current version?
    I still use .NET 3.5. I get no exceptions. Besides, if not using the enumerator then how else am I going to loop through the PolicyLevel objects? there is no Ienumerable to use a foreach loop with. (at least in the version of the framework I'm on)

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Microsoft uses the enumerator in their own examples. It's probably so old that their coders weren't up to it and forgot IEnumerable. I'll take a wild guess and say that the non-obsolete replacement will not behave like this. Anyway, your first code looks like the code used in the MSDN so I won't argue with that. Both are correct, both are horrible because of what Microsoft did with the API, it's a purely aestetic, personal choice. I'd pick the one that tastes like strawberry
    Elkvis and rags_to_riches like this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21