• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Beginners C#query

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    #31
    Originally posted by Jaws View Post
    Likewise, in my mind it is far easier to read and the better approach for loop of that nature.

    I think "while( (data = sr.ReadLine()) != null )" is fine too, it's a common pattern and therefore easy enough to read.
    I don't know if C# supports truthy and falsey values but in Javascript you wouldn't even need the != null. You could just use:

    while(data = sr.ReadLine()) {
    // do stuff with data
    }

    Comment


      #32
      Originally posted by VectraMan View Post
      Don't forget to use the comma operator:

      Console.WriteLine(iRows++ + " - " + (data = sr.ReadLine(), somethingElse = thing.SomethingElse(), iRows<<=2, someExtremelyLargeAndSlowOperation() ));

      Chuck in some lambda functions (can C# do that?) and you can write an entire program in one statement.
      In .NET 4.0 File.ReadLines probably (not checked) works with a StreamReader behind the scenes (definitely does not load entire file into memory).

      Code:
      foreach(string s in File.ReadLines("path.txt").Select((line, i) => string.Format("{0} - {1}", i+1, line))) Console.WriteLine(s);
      Dont know what all the fuss is about

      Comment


        #33
        Originally posted by Scrag Meister View Post
        Thanks so much for the replies.

        Minestrone, sorry hasty on the neg, I think the some total of your contributions was worth a +ve, even if bits of your code didn't do what mine did. . Will correct in due course.

        P.S. There is a LOT more data manipulation code in the loop I just cleared it out to make clear the bit I was asking about. Performance is important but not THAT important in this situation.

        Thank you all again.
        Don't worry about it, I was fookin hammered the previous night with football related activity, I gave it a "aw naw, made a tit out of myself there" face palm gesture about 60 seconds after posting.

        Comment


          #34
          Originally posted by doodab View Post
          Of course one reason you have to put return values into a variable rather than use them directly is that people writing the methods often return null when they should have thrown an exception so you need to check the value before you use it. This gets on my tits.
          I think always return null.

          I believe exceptions are there for when something goes wrong, I don't think user.getFirstName() should chuck an exception if the user has no first name.

          What does piss in my soup is collections being returned as null, pass me back an empty collection if there is nowt in it.

          Comment


            #35
            Originally posted by minestrone View Post
            I believe exceptions are there for when something goes wrong
            Yes, which is why if you return null when you should have thrown an exception you are doing a bad thing.

            Thingy thingy = ThingyFactory.createThingy() should not require me to check I have a valid reference to a thingy if a thingy couldn't be created.
            While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

            Comment


              #36
              Originally posted by doodab View Post
              Yes, which is why if you return null when you should have thrown an exception you are doing a bad thing.

              Thingy thingy = ThingyFactory.createThingy() should not require me to check I have a valid reference to a thingy if a thingy couldn't be created.
              You are mixing this, first off it was methods, now you are using a creational factory methods in you example.

              Grey Goose has been consumed tonight, shall come back to this again.

              Comment


                #37
                Originally posted by minestrone View Post
                You are mixing this, first off it was methods, now you are using a creational factory methods in you example.

                Grey Goose has been consumed tonight, shall come back to this again.
                It was methods that return null when they should throw an exception. Getters that return null because the underlying value is null are perfectly sensible. A List sort() or add() method or a Document parse() method or a factory create method that returns null because it didn't work for some reason is very very not fine.
                While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

                Comment


                  #38
                  Originally posted by doodab View Post
                  It was methods that return null when they should throw an exception. Getters that return null because the underlying value is null are perfectly sensible. A List sort() or add() method or a Document parse() method or a factory create method that returns null because it didn't work for some reason is very very not fine.
                  Agree.

                  Comment


                    #39
                    Originally posted by minestrone View Post
                    Not in that context.

                    iRows is going to be the same with ++ appended or prepended when it comes to the next line

                    I would have written it...

                    using (StreamReader sr = new StreamReader(args[0]) {
                    while ( sr.ReadLine() !=null) {
                    Console.WriteLine(iRows++ + " - " + sr.ReadLine() );
                    }
                    That would give a different result to what the original code produced however

                    I see this kind of tulip all the time
                    Last edited by adubya; 10 November 2012, 08:55.

                    Comment


                      #40
                      What i'm really fecked off about just now is the uncle bob clean code brigade that seem to have swept through the code bases i'm working on just now.

                      Sometimes, despite trying to push functionality into classes where that should belong in you just have to write a long winded method to do a process, maybe a 150 liner but I think that if you name the method correctly and comment it out correctly I'm good with that.

                      What I keep finding is classes with one public method and a dozen private methods called by the public method. The class becomes an API splodge.

                      Uncle bob says that is right and Uncle Bob needs a kick in the nads.

                      I'm going to get started on anemic domain models next.

                      Comment

                      Working...
                      X