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's the f()?!

What's the f()?!

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharprubyquestion
6 Posts 6 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.
  • D Offline
    D Offline
    David MacDermot
    wrote on last edited by
    #1

    Here's a little gem I came across a while back.

    private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }

    It's employed all over the code module. Here's one place:

    byte[][] outBuf = new byte[2][];
    outBuf[0] = new byte[256];
    outBuf[1] = new byte[256];

    //
    // Skip some stuff
    //

    for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
    outBuf[i][j] = 0x20; //outBuf initialization

    It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list! Here's another place where it's used.

    public bool Visible
    {
    get { return f(_RawData[1] & VISIBLE); }
    set
    {
    if(f(_RawData[1] & VISIBLE) != value )
    _RawData[1] ^= VISIBLE;
    }
    }

    Well, that explains the name. Is this clever or a horror?

    OriginalGriffO C B B P 5 Replies Last reply
    0
    • D David MacDermot

      Here's a little gem I came across a while back.

      private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }

      It's employed all over the code module. Here's one place:

      byte[][] outBuf = new byte[2][];
      outBuf[0] = new byte[256];
      outBuf[1] = new byte[256];

      //
      // Skip some stuff
      //

      for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
      outBuf[i][j] = 0x20; //outBuf initialization

      It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list! Here's another place where it's used.

      public bool Visible
      {
      get { return f(_RawData[1] & VISIBLE); }
      set
      {
      if(f(_RawData[1] & VISIBLE) != value )
      _RawData[1] ^= VISIBLE;
      }
      }

      Well, that explains the name. Is this clever or a horror?

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

      From that, it look like a horror. Did he comment it at all? Or just throw it together and walk away? 'Cause if he didn't, it's probably time he met Mr Ugly Stick...

      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

      1 Reply Last reply
      0
      • D David MacDermot

        Here's a little gem I came across a while back.

        private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }

        It's employed all over the code module. Here's one place:

        byte[][] outBuf = new byte[2][];
        outBuf[0] = new byte[256];
        outBuf[1] = new byte[256];

        //
        // Skip some stuff
        //

        for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
        outBuf[i][j] = 0x20; //outBuf initialization

        It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list! Here's another place where it's used.

        public bool Visible
        {
        get { return f(_RawData[1] & VISIBLE); }
        set
        {
        if(f(_RawData[1] & VISIBLE) != value )
        _RawData[1] ^= VISIBLE;
        }
        }

        Well, that explains the name. Is this clever or a horror?

        C Offline
        C Offline
        Christian Bruggemann
        wrote on last edited by
        #3

        That is definately an absolute horror. I hate it when people think they are smart because they write such code. It is totally unmaintainable and unreadable to anyone (including the author of it a couple of weeks after he wrote it!)

        1 Reply Last reply
        0
        • D David MacDermot

          Here's a little gem I came across a while back.

          private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }

          It's employed all over the code module. Here's one place:

          byte[][] outBuf = new byte[2][];
          outBuf[0] = new byte[256];
          outBuf[1] = new byte[256];

          //
          // Skip some stuff
          //

          for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
          outBuf[i][j] = 0x20; //outBuf initialization

          It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list! Here's another place where it's used.

          public bool Visible
          {
          get { return f(_RawData[1] & VISIBLE); }
          set
          {
          if(f(_RawData[1] & VISIBLE) != value )
          _RawData[1] ^= VISIBLE;
          }
          }

          Well, that explains the name. Is this clever or a horror?

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          if (C++)
          delete pBrain;

          1 Reply Last reply
          0
          • D David MacDermot

            Here's a little gem I came across a while back.

            private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }

            It's employed all over the code module. Here's one place:

            byte[][] outBuf = new byte[2][];
            outBuf[0] = new byte[256];
            outBuf[1] = new byte[256];

            //
            // Skip some stuff
            //

            for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
            outBuf[i][j] = 0x20; //outBuf initialization

            It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list! Here's another place where it's used.

            public bool Visible
            {
            get { return f(_RawData[1] & VISIBLE); }
            set
            {
            if(f(_RawData[1] & VISIBLE) != value )
            _RawData[1] ^= VISIBLE;
            }
            }

            Well, that explains the name. Is this clever or a horror?

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

            It's definitely a horror. It's unclear, poorly named and results in ugly calling code. In addition, the cast and call to a Convert function will make it slow.

            1 Reply Last reply
            0
            • D David MacDermot

              Here's a little gem I came across a while back.

              private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }

              It's employed all over the code module. Here's one place:

              byte[][] outBuf = new byte[2][];
              outBuf[0] = new byte[256];
              outBuf[1] = new byte[256];

              //
              // Skip some stuff
              //

              for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
              outBuf[i][j] = 0x20; //outBuf initialization

              It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list! Here's another place where it's used.

              public bool Visible
              {
              get { return f(_RawData[1] & VISIBLE); }
              set
              {
              if(f(_RawData[1] & VISIBLE) != value )
              _RawData[1] ^= VISIBLE;
              }
              }

              Well, that explains the name. Is this clever or a horror?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              What do you mean by "C# wouldn't allow abuse of the comma operator"? C# doesn't have a comma operator. A quick search pulled up this little gem (I wonder how it got copied from the MSDN foum :sigh: ): http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-csharp/93126/C-for-loop-and-comma-operator[^] Yes, that's a horror -- the method should have a better name and it could even be made generic!

              public T
              LastOf
              (
              params object[] list
              )
              {
              return ( (T) list [ list.Count - 1 ] ) ;
              }

              It's still silly, but this just points up how easy it is to implement a way to get around the limitation. Come to think of it, couldn't you do it with Linq? I don't know Linq, but something like: (new int[] { a++ , b++ }).Last ? P.S. Here's a little test of the concept:

                  int i = 0 ; 
                  int j = 0 ;
              
                  while ( (new int\[\] { i++ , j += 2 }).Last() < 10 )
                  {                   
                    System.Console.WriteLine ( "{0} {1}" , i , j ) ;
                  }                   
                                      
                  for ( i = 0 , j = 0 ; (new int\[\] { i++ , j += 2 }).Last() < 10 ; )
                  {
                    System.Console.WriteLine ( "{0} {1}" , i , j ) ;
                  }
              
              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