Optimize code with StringBuilders



  • Some C# code I have to maintain contains this interesting use of StringBuilders:

    foreach (var exception in exceptions)
    {
        message.Append("\r\n" + exception.Message);
    }
    

    It's beautiful.



  • I don't think that a single string concat per loop would have a huge impact. Sure, they could have used message.Append(Environment.NewLine).Append(exception.Message), but I don't think there's much of a WTF here.



  • I would have used:

    message.Append(String.Format("{1}{0}",exception.Message,"\r\n"));

    Also, I don't know why you're not using ToString() on the exception...

     



  • Wouldn't calling .ToSTring() on the exception give you something like the type of the exception, rather than the exception message?



  • The real WTF here is code that generates so many exceptions that you have to worry about the efficiency of the loop that processes them.



  • @toth said:

    I don't think that a single string concat per loop would have a huge impact. Sure, they could have used message.Append(Environment.NewLine).Append(exception.Message), but I don't think there's much of a WTF here.
     

    For a string concatenation that cannot be optimised (for example because it's not predictable), it creates a StringBuilder, calls append, and returns the string. And that for each iteration. So each iteration, you have object creation, appending a string to a buffer or array, and converting the internal buffer back into a string. For the Java StringBuilder, string concatenation is several orders of magnitude slower than using a StringBuilder with only an append inside the loop body. Not sure how it is in .NET, but I suspect something similar.



  • Correct me if I'm wrong, but don't modern Java compilers turn string concatenation INTO StringBuilders without the programmer's knowing?



  • @Joel B said:

    Wouldn't calling .ToSTring() on the exception give you something like the type of the exception, rather than the exception message?
    Very good, but that's only if Exception doesn't override ToString, which I believe it does.



  • @zelmak said:

    Correct me if I'm wrong, but don't modern Java compilers turn string concatenation INTO StringBuilders without the programmer's knowing?

    TRWTF.

     


  • Trolleybus Mechanic

    @pbean said:

    For a string concatenation that cannot be optimised (for example because it's not predictable), it creates a StringBuilder, calls append, and returns the string. And that for each iteration. So each iteration, you have object creation, appending a string to a buffer or array, and converting the internal buffer back into a string. For the Java StringBuilder, string concatenation is several orders of magnitude slower than using a StringBuilder with only an append inside the loop body. Not sure how it is in .NET, but I suspect something similar.
     

    I just did a quick test in .Net.  I concat'd 10000 Guid.NewGuid().ToString() together, then did the same with a StringBuilder.append(), ending with a .ToString().

    Concat took 560ms

    String Builder took 31ms.

    [b]BUT[/b]... how many times are you going to be concat'ing 10000 strings?  As it stands, I had to do more than 600 strings just to get the concat loop to take longer than a millisecond.


  • Garbage Person

     ... Funny you mention that, I think I just saw that particular idiotic flaw in my own code. Hang on, let me look.

     

     

    ... Yup.

     

    if (rangeMin == rangeMax) builder.Append(rangeMin + ": ");
    else builder.Append(rangeMin + ".." + rangeMax + ": ");

     

    Of course, those lines are in a property that only gets called a few tens of thousands of times during an export - so in the grand scheme of things it isn't worth my bracket keys to fix it.



  • C# is completely different than Java in this regard.  First, I'd probably use StringBuilder.AppendFormat (as well as using Environment.NewLine as previously stated).  Second, building strings in C# does NOT automatically use a StringBuilder.  The + in this instance will translate to a call to String.Concat, which is faster than using a StringBuilder for small additions (although not necessarily if you already have the StringBuilder and are just appending it anyway).  ToString will NOT give you the type of the exception.

    As for the "metrics"... this is not the same thing as simply concatenating in a loop, which I suspect is what you did. 

    The following program outputs (in ms, was 1, 0 - which means with 10,000 concats, the worst performance was a ms):
    3824
    2442

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>StringBuilder</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> message = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>StringBuilder</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>();

    </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>List</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas><</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Exception</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>> exceptions = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>List</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas><</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Exception</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>>();</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>for</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>(</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>int</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> x = 0; x < 10000; ++x)</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    exceptions.Add(</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Exception</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>(</FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"message"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>));</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    }

    </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Stopwatch</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> concatSw = </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Stopwatch</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>.StartNew();

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>foreach</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>var</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> exception </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>in</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> exceptions)</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    message.Append(</FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"\r\n"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> + exception.Message);</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    }

    concatSw.Stop();

    System.</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Console</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>.WriteLine(concatSw.ElapsedTicks);</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    message.Clear();

    concatSw = </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Stopwatch</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>.StartNew();</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>foreach</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>(</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>var</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> exception </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>in</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> exceptions)</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    message.Append(</FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"\r\n"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>).Append(exception.Message);</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    }

    concatSw.Stop();

    </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Console</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>.WriteLine(concatSw.ElapsedTicks);</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>Console</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>.Read();

    </FONT></FONT>


  • (TRWTF is people claiming they know how C# works)

    (Also, creating new GUIDs takes about twice as long as either of the outputs there)



  • What is wrong with you?



  • I'm unsure of how to answer that question because I'm not sure what you're referring to, specifically.  Are you referring to the fact that I took 30s to create a test run, or that I actually know what I'm talking about?



  • @Sutherlands said:

    I'm unsure of how to answer that question

    Then tell him about some completely unrelated thing that is wrong with you. For example a deadly or very unfortunate illness. You could also make one up, though you might want to choose something very uncommon in case someone else here actually has it and might feel offended.



  • When I see this:

    (int x = 0; x < 10000; ++x)
    

    I can't help but ask myself: if you need to increase the size of x before even using it, why don't you declare it with a bigger value? I suspect that you use ++x because you favor style over convenience and it makes your code suspect.


  • Discourse touched me in a no-no place

    @thistooshallpass said:

    When I see this:

    (int x = 0; x < 10000; ++x)
    

    I can't help but ask myself: if you need to increase the size of x before even using it,

    In C (and C++), x isn't incremented until after the first pass through the loop. Are there languages that do it before the first pass?



    (And in this instance, there's absolutely no substantive difference between ++x and x++, since the value of the expression isn't being evaluated. The only case where it might matter is in the speed of the increment.)



  • @blakeyrat said:

    What is wrong with you?

     @derula said:

    Then tell him about some completely unrelated thing that is wrong with you.

    Very well.  I have very poor eyesight.

    @thistooshallpass said:

    When I see this:
    (int x = 0; x < 10000; ++x)
    

    I can't help but ask myself: if you need to increase the size of x before even using it, why don't you declare it with a bigger value? I suspect that you use ++x because you favor style over convenience and it makes your code suspect.

    >.>

    How long have you been a programmer?

    Also, WELCOME TO THE FORUMS!



  • @thistooshallpass said:

    When I see this:

    (int x = 0; x < 10000; ++x)
    

    I can't help but ask myself: if you need to increase the size of x before even using it, why don't you declare it with a bigger value? I suspect that you use ++x because you favor style over convenience and it makes your code suspect.

    Next time, make sure what you're saying is correct before so smugly claiming someone's (superior) style makes their code "suspect".

    Hint: Pre-increment doesn't magically rearrange the components of a for-loop.



  • @PJH said:

    The only case where it might matter is in the speed of the increment.)
    At the risk of blakey calling me weird, this should no longer matter under any modern compiler.  Indeed, C# compiles for(int x = 0; x < 10; ++x) and for(int x = 0; x < 10; x++) to the same thing.  Basically, for x++ you have to store the previous value, but if you're not using that value, the compiler should optimize it out.



  • @Sutherlands said:

    Very well.  I have very poor eyesight.

    Oh, well, thanks for that.

    I was more referring to this optimization-masturbation-fest going on in this thread. Which is firstly annoying because it's premature optimization*, and secondly annoying because it's so fucking useless.

    Unless you've written the shittiest code in history, the performance of StringBuilder compare to string concatenation does not fucking matter. Why would you waste milliseconds of your precious life, and kill neurons, thinking about this? What is wrong with you?

    Fuck I hate programmers.




    *) There's an extremely obvious joke there I'm not going to make



  • @blakeyrat said:

    Unless you've written the shittiest code in history, the performance of StringBuilder compare to string concatenation does not fucking matter.
    Granted I didn't explicitly say this, but all my post is is pointing out how far off the mark other posts were.  I had to switch the output to ticks instead of ms because it was so insignificant.

    Of course... your post was easily worth more of everyone's precious time than actually learning something.


  • Garbage Person

     Like I said - not worth the wear and tear on my bracket keys to fix it. Of course, when I commit this code to the public repos, someone will probably try to crucify me for it but such is the way of opensource contribution.

    @blakeyrat said:

    Fuck I hate programmers.
    I have never agreed with you more. Between occasional bouts of getting horribly confused by maths because I wasn't a computer science major*, I thank the gods that I wasn't a computer science major and therefore have a sane perspective about the role of programmers and programs in delivering value.

     *) I once wrote code to simulate the orbit of a planet. Simple implementation of some basic equations, right? Somehow the frakking planets would randomly fly around in no discernable pattern whatsoever



  • The point is:

    (int x = 0; x < 10000; ++x)

    versus:

    (int x = 1; x < 10000; x++)

    in the context provided is exactly the same, only the first one is less typical, and has no added value to justify it standing out in a codebase. I don't know about one being "superior", but anyone who maintains a lot of other people's code knows that personal style is expensive over the years because it slows down the code review (this is why God created code convention). This ++ thing by itself is not a WTF, but finding it in a small piece of code is a tell.



  • @thistooshallpass said:

    The point is:
    (int x = 0; x < 10000; ++x)
    versus:
    (int x = 1; x < 10000; x++)

    in the context provided is exactly the same, only the first one is less typical, and has no added value to justify it standing out in a codebase. I don't know about one being "superior", but anyone who maintains a lot of other people's code knows that personal style is expensive over the years because it slows down the code review (this is why God created code convention). This ++ thing by itself is not a WTF, but finding it in a small piece of code is a tell.

    Actually, the tell is that you think those two pieces of code are equivalent.  Especially considering people have told you that they aren't and pointed out why.

    Edit (since you don't seem to be learning): http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops



  • @Lorne Kates said:

    I just did a quick test in .Net.  I concat'd 10000 Guid.NewGuid().ToString() together, then did the same with a StringBuilder.append(), ending with a .ToString().

    Concat took 560ms

    String Builder took 31ms.

    BUT... how many times are you going to be concat'ing 10000 strings?  As it stands, I had to do more than 600 strings just to get the concat loop to take longer than a millisecond.

     

    The problem is that concatenating strings hides its complexity as it goes, at first is is very close to O(n), but as the resulting string builds up its size becomes much bigger than the new string based on the number of concats:

    • small: r ~= n, 
    • large: r > n
    • huge: r >> n.

    Consider loading a text file in chunks or in one line at a time, a 1M file may take seconds, and a 10M file can take minuets, the time doesn't scale linearly with the problem anymore, even though it should be O(n), it turns out that N is really (R + N). By using a string builder or simply reserving space == to the size of the file, the runtime can be reduced to seconds again. I had a bug like this at a previous job and the fix was 1 line:

    string.reserve(filesize);

    and loading large files went from minutes to seconds.

    Joel on software has an excelent indepth explainaiton of this: http://www.joelonsoftware.com/articles/fog0000000319.html

     



  • @Weng said:

    @blakeyrat said:
    Fuck I hate programmers.
    I have never agreed with you more. Between occasional bouts of getting horribly confused by maths because I wasn't a computer science major*, I thank the gods that I wasn't a computer science major and therefore have a sane perspective about the role of programmers and programs in delivering value.

    I was a computer science major; I dropped out when:

    1) They became completely obsessed with me learning useless maths (like calculus)

    2) It became apparent that they'd never actually teach the parts of computer science I actually cared about: how to make software easier for human beings to use. That is to say, they didn't bother saying a single word about the single most important aspect of software.

    But mostly I dropped out because I couldn't pass calculus.



  • @Sutherlands said:

    Edit (since you don't seem to be learning): http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops
     

    Or alternatively try something like this

     

    int x=0;
    int y=0;
    for(int i=0; i<100; i++) 
    {
         x++;
    }
    for(int i=0; i<100; ++i)
    {
         y++;
    }
    if(x==y)
    {
         printf("we are right and you are wrong");
    }
    else
    {
         printf("there must be a typo somewheres");
    }


  • @blakeyrat said:

    @Weng said:
    @blakeyrat said:
    Fuck I hate programmers.
    I have never agreed with you more. Between occasional bouts of getting horribly confused by maths because I wasn't a computer science major*, I thank the gods that I wasn't a computer science major and therefore have a sane perspective about the role of programmers and programs in delivering value.

    I was a computer science major; I dropped out when:

    1) They became completely obsessed with me learning useless maths (like calculus)

    2) It became apparent that they'd never actually teach the parts of computer science I actually cared about: how to make software easier for human beings to use. That is to say, they didn't bother saying a single word about the single most important aspect of software.

    But mostly I dropped out because I couldn't pass calculus.

     

    The math required to understand Theory of Computing, is only set theory and some discreete math, neither of which really require an understanding of calculus; unless you are doing scientific programming, or working for a scientist at national lab or similar, calculus isn't required. The only reason calculus was required for my BSCS degree was the calculus based statistics for engineering majors class that, ironically, engineering students were not required to take!

    As for #2, there was probably an elective you could take to cover that, but chances are it would be hard to take since most school will cull classes with less that some number of students enrolled (usually like 9-15 min.) I know there were electives like that when i was in college, they only got taught once every 2 years, because it took that long to get enough students enrolled for it. I knew post-grad students that couldn't graduate on time because of culled classes.

     



  • @Sutherlands said:

    @thistooshallpass said:

    The point is:

    (int x = 0; x < 10000; ++x)
    versus:
    (int x = 1; x < 10000; x++)

    in the context provided is exactly the same, only the first one is less typical, and has no added value to justify it standing out in a codebase. I don't know about one being "superior", but anyone who maintains a lot of other people's code knows that personal style is expensive over the years because it slows down the code review (this is why God created code convention). This ++ thing by itself is not a WTF, but finding it in a small piece of code is a tell.

    Actually, the tell is that you think those two pieces of code are equivalent.  Especially considering people have told you that they aren't and pointed out why.

    Edit (since you don't seem to be learning): http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops

    I understand. But there is a confusion. When you scroll quickly in a piece of code and you see this, you have to stop and ask yourself: what is going on here. And you have to think about what the guy who wrote the code wanted to do. For an experienced programmer (like yourself) it does not take long to discard it as a mere stylistic figure, but those seconds add up. Now you can focus on a mistake and make it an issue by itself, but I would say that the whole discussion shows that avoiding personal style can save a lot of time.



  • @locallunatic said:

    @Sutherlands said:

    Edit (since you don't seem to be learning): http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops
     

    Or alternatively try something like this

     

    int x=0;
    int y=0;
    for(int i=0; i<100; i++) 
    {
         x++;
    }
    for(int i=0; i<100; ++i)
    {
         y++;
    }
    if(x==y)
    {
         printf("we are right and you are wrong");
    }
    else
    {
         printf("there must be a typo somewheres");
    }

    If they are the same, why use ++i and create confusion? Because that is the actual point.



  • @thistooshallpass said:

    @locallunatic said:

    @Sutherlands said:

    Edit (since you don't seem to be learning): http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops
     

    Or alternatively try something like this

     

    int x=0;
    int y=0;
    for(int i=0; i<100; i++) 
    {
         x++;
    }
    for(int i=0; i<100; ++i)
    {
         y++;
    }
    if(x==y)
    {
         printf("we are right and you are wrong");
    }
    else
    {
         printf("there must be a typo somewheres");
    }

    If they are the same, why use ++i and create confusion? Because that is the actual point.

     

    But what you said was:

    @thistooshallpass said:

    The point is:

    (int x = 0; x < 10000; ++x)

    versus:

    (int x = 1; x < 10000; x++)

    in the context provided is exactly the same

    Note the differences in starting values between the two examples.


  • Garbage Person

    @blakeyrat said:

    I was a computer science major; I dropped out when:
    1) They became completely obsessed with me learning useless maths (like calculus)
    2) It became apparent that they'd never actually teach the parts of computer science I actually cared about: how to make software easier for human beings to use. That is to say, they didn't bother saying a single word about the single most important aspect of software.
    I had to do Calculus, but I skated by with the barest minimum percentage-to-pass. I actually kind of regret that, some of my other pursuits could use the knowledge. A very significant part of my degree program was making software easier to use. The rest was about being a useful software developer - design practices, databases, APIs, source control, testing, project management, time estimation, analysis (in the context of business and projects, not numbers), team organization, agile methodology. The actual programming was merely a vehicle through which to experience these things and by senior year a lot of my cohorts couldn't code their way out of a paper bag (until I taught them all the fuck how for our capstone project).

    It's made my immediate post-college career HELL because everyone out of the immediate vicinity of my University doesn't think such a program exists and assumes I'm a clueless Comp Sci graduate with bunch of buzzwords copypasta'd onto my resume, and therefore will need constant nannying. Of course, now that I have a headhunter who knows how to sell the fact that I know my head from my arse, things are looking quite nice.



  • @thistooshallpass said:

    I understand. But there is a confusion. When you scroll quickly in a piece of code and you see this, you have to stop and ask yourself: what is going on here. And you have to think about what the guy who wrote the code wanted to do. For an experienced programmer (like yourself) it does not take long to discard it as a mere stylistic figure, but those seconds add up. Now you can focus on a mistake and make it an issue by itself, but I would say that the whole discussion shows that avoiding personal style can save a lot of time.
    I would certainly not hire any developer, even a junior, who didn't understand that ++x and x++ in the last part of a for loop provide for the exact same thing.  If you by now actually understand that they're exactly the same code, I'll go back and change your post and reply to it as I was originally going to reply to it until I noticed that you put different starting values in it.

    @thistooshallpass said:

    The point is:
    (int x = 0; x < 10000; ++x)

    versus:

    (int x = 0; x < 10000; x++)

    in the context provided is exactly the same, only the first one is less typical, and has no added value to justify it standing out in a codebase. I don't know about one being "superior", but anyone who maintains a lot of other people's code knows that personal style is expensive over the years because it slows down the code review (this is why God created code convention). This ++ thing by itself is not a WTF, but finding it in a small piece of code is a tell.

    You're right, it is absolutely a tell.  I'm a bad programmer because when I created some quick dummy code in my own personal workspace, I didn't adhere to either your personal style guidelines, or the guidelines of whatever team you're on.

    I can't possibly see how your response could be any more douchey, and I've read a lot of blakeyrants.

    (Also, I could understand the difference and not take seconds to figure out what was going on when I was in college.  I guess bad programmers are just bad programmers.)



  • @Weng said:

    It's made my immediate post-college career HELL because everyone out of the immediate vicinity of my University doesn't think such a program exists

    When did you attend? Back when I was scouting schools, such a program literally did not exist, at least in my state.



  • @blakeyrat said:

    I was a computer science major; I dropped out when:
    1) They became completely obsessed with me learning useless maths (like calculus)
    2) It became apparent that they'd never actually teach the parts of computer science I actually cared about: how to make software easier for human beings to use. That is to say, they didn't bother saying a single word about the single most important aspect of software.

    But mostly I dropped out because I couldn't pass calculus.

     

    I'd disagree on the calc being useless, it's useful for things in my life (just not programming).  And if they weren't teaching about things like usability then you were in a terrible CS program (though in many that stuff is a little later and optional instead of required like it should be).


  • Garbage Person

    @thistooshallpass said:

    When you scroll quickly in a piece of code and you see this, you have to stop and ask yourself: what is going on here.
    I look at it and go "Oh, look. The simplest possible For loop. And the guy who wrote it thinks compilers don't know how to optimize it. How adorable!" and move on. Or I would if I bothered to stop and look. Because I don't.

     

    Shit like for(int x=0; x++<1000;) really fucking annoys me, though.Yes, I've seen people do that.



  • @Sutherlands said:

    (Also, I could understand the difference and not take seconds to figure out what was going on when I was in college. I guess bad programmers are just bad programmers.)

    If your goal is to hire "good programmers" so you can sit back and feel smug and write abstractions of abstractions of abstractions all day long, then you're taking the right approach.

    If, however, your goal is to write good software, then thistooshallpass had the right idea:

    @thistooshallpass said:

    If they are the same, why use ++i and create confusion? Because that is the actual point.



  • @locallunatic said:

    @thistooshallpass said:

    @locallunatic said:

    @Sutherlands said:

    Edit (since you don't seem to be learning): http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops
     

    Or alternatively try something like this

     

    int x=0;
    int y=0;
    for(int i=0; i<100; i++) 
    {
         x++;
    }
    for(int i=0; i<100; ++i)
    {
         y++;
    }
    if(x==y)
    {
         printf("we are right and you are wrong");
    }
    else
    {
         printf("there must be a typo somewheres");
    }

    If they are the same, why use ++i and create confusion? Because that is the actual point.

     

    But what you said was:

    @thistooshallpass said:

    The point is:

    (int x = 0; x < 10000; ++x)

    versus:

    (int x = 1; x < 10000; x++)

    in the context provided is exactly the same

    Note the differences in starting values between the two examples.

    I did a mistake and I did not notice the error until I posted. But then this is actually a pretty good example of confusion caused by non-typical syntax so it does not really matter if this is wrong.



  • @locallunatic said:

    I'd disagree on the calc being useless, it's useful for things in my life (just not programming).

    Yes, well, the problem was it was a requirement for the computer science program. Saying "it's useful, just not for programming" doesn't contribute much to the thread.

    @locallunatic said:

    And if they weren't teaching about things like usability then you were in a terrible CS program (though in many that stuff is a little later and optional instead of required like it should be).

    I concur, but let me ask: in 1997, what universities taught CS with a focus on usability? I honestly do not think any CS programs at that time did, and I certainly didn't find one at the time. Although I fully admit I could be wrong.



  • @blakeyrat said:

    @Sutherlands said:
    (Also, I could understand the difference and not take seconds to figure out what was going on when I was in college. I guess bad programmers are just bad programmers.)
    If your goal is to hire "good programmers" so you can sit back and feel smug and write abstractions of abstractions of abstractions all day long, then you're taking the right approach.

    If, however, your goal is to write good software, then thistooshallpass had the right idea: @thistooshallpass said:

    If they are the same, why use ++i and create confusion? Because *that* is the actual point.

    You think that my non-adherance to his unknown coding standards means that i'm not trying to write good software?  Or perhaps you think that people who understand the difference between ++i and i++ sit around and write abstractions of abstractions all day long?  Or perhaps you think that someone who can't understand the simplest of distinctions in the simplest of for loops can understand broader concepts like UI design or Web Service development?


  • @thistooshallpass said:

    I did a mistake and I did not notice the error until I posted. But then this is actually a pretty good example of confusion caused by non-typical syntax so it does not really matter if this is wrong.
    You are the absolute only person who has been confused by ++i vs i++ in this thread.


  • Garbage Person

    @blakeyrat said:

    @Weng said:
    It's made my immediate post-college career HELL because everyone out of the immediate vicinity of my University doesn't think such a program exists

    When did you attend? Back when I was scouting schools, such a program literally did not exist, at least in my state.

    Penn State. Program is called Information Sciences and Technology. There are a half dozen tracks in it, but the one called something about "Design and Development" is the good one. Best campus is not main campus, but actually the podunk shitville one in York - they have a unique variation where the upperclass projects are jobbed out onto real-world projects for real-world clients. I'm led to believe that student quality has gone down substantially since I left, though, because of a drive on that campus to recruit IST majors from India.

    Rhetoric when I was a freshman was that the program is absolutely unique and no other school does anything even remotely similar anywhere on the planet. No clue if that's still true or not.

    I seem to recall the first associate graduates came through in 1999, so that puts the program as opening in 97, so if you went to school before then, it simply didn't exist ANYWHERE.

    The developer track also has a bit of a branding problem - I was one of SIX students in my graduating class and one of a dozen PERIOD. Everyone else was enrolled in the bullshit MIS-like track because it gets promoted more heavily by recruiters (hell, even I was on that track to begin with, until I realized I liked making things more than I liked making presentations. Not that I didn't become an Ace I Don't Need No Stinkin Powerpoint presentation god by the end.).



  • @Sutherlands said:

    @blakeyrat said:

    @Sutherlands said:
    (Also, I could understand the difference and not take seconds to figure out what was going on when I was in college. I guess bad programmers are just bad programmers.)
    If your goal is to hire "good programmers" so you can sit back and feel smug and write abstractions of abstractions of abstractions all day long, then you're taking the right approach.

    If, however, your goal is to write good software, then thistooshallpass had the right idea: @thistooshallpass said:

    If they are the same, why use ++i and create confusion? Because *that* is the actual point.

    You think that my non-adherance to his unknown coding standards means that i'm not trying to write good software?  Or perhaps you think that people who understand the difference between ++i and i++ sit around and write abstractions of abstractions all day long?  Or perhaps you think that someone who can't understand the simplest of distinctions in the simplest of for loops can understand broader concepts like UI design or Web Service development?
    Or perhaps blakey's ideal candidate is someone who can't understand ++i vs i++, and creates a thread about how bad code is for not optimizing a .Append of a string concat into 2 .Appends?

    Hm, no i don't think that's it.



  • @Weng said:

    Rhetoric when I was a freshman was that the program is absolutely unique and no other school does anything even remotely similar anywhere on the planet. No clue if that's still true or not.

    That sounds pretty nice.  I know my school didn't have anything to do with UX at all.  Of course, right now my boss's boss assumes you don't know certain things unless you have a master's, but I definitely got all that stuff during school.  Overall, I'm happy with my education.


  • @Sutherlands said:

    You think that my non-adherance to his unknown coding standards means that i'm not trying to write good software?

    I think it leads to potentially worse software regardless of your intentions. I don't see how it could possibly improve software quality compared to adhering to generally accepted coding standards.

    This is the "using the tertiary 'if' operator" argument all over again.



  • @Weng said:

    I seem to recall the first associate graduates came through in 1999, so that puts the program as opening in 97, so if you went to school before then, it simply didn't exist ANYWHERE.

    I'm inclined to agree. Not that I'd be able to afford Penn State anyway. But like I'm said, I'm 99% confident that no Washington universities offered anything even close to that at the time I was entering college. I chose CWU because they have an awesome teacher education program, and I was still undecided in becoming a teacher-- glad I didn't. Also glad I dropped-out, honestly I should have dropped-out a year earlier.



  • @blakeyrat said:

    @Sutherlands said:
    You think that my non-adherance to his unknown coding standards means that i'm not trying to write good software?
    I think it leads to potentially worse software regardless of your intentions. I don't see how it could possibly improve software quality compared to adhering to generally accepted coding standards.

    This is the "using the tertiary 'if' operator" argument all over again.

    You don't see the advantage of adhering to local (team or department) guidelines over the guidelines of internet dude?

    I started writing ++x back when I was in college, when I learned about the efficiency differences.  I don't know if compilers all took care of it by then, but I did it anyway.  Now, I know that there are no differences, but I do it anyway out of habit.  This is the first time anyone has EVER not understood it, and this is the first time it has EVER come up.  If you want to think that my software is worse because I write it that way, fine, go ahead.  I'll be happy knowing that my software is better because I understand how things work and write things accordingly.  (And because I almost never use a for(<>;<>;<>) loop... who does that these days?)



  • @Sutherlands said:

    @Sutherlands said:

    @blakeyrat said:

    @Sutherlands said:
    (Also, I could understand the difference and not take seconds to figure out what was going on when I was in college. I guess bad programmers are just bad programmers.)
    If your goal is to hire "good programmers" so you can sit back and feel smug and write abstractions of abstractions of abstractions all day long, then you're taking the right approach.

    If, however, your goal is to write good software, then thistooshallpass had the right idea: @thistooshallpass said:

    If they are the same, why use ++i and create confusion? Because *that* is the actual point.

    You think that my non-adherance to his unknown coding standards means that i'm not trying to write good software?  Or perhaps you think that people who understand the difference between ++i and i++ sit around and write abstractions of abstractions all day long?  Or perhaps you think that someone who can't understand the simplest of distinctions in the simplest of for loops can understand broader concepts like UI design or Web Service development?
    Or perhaps blakey's ideal candidate is someone who can't understand ++i vs i++, and creates a thread about how bad code is for not optimizing a .Append of a string concat into 2 .Appends?

    Hm, no i don't think that's it.

    You make a lot of assumptions, and most of them are wrong. And I would kindly point out that the only person who said that you are a bad programmer is yourself.


    For the Append thing, it was not a matter of optimization, but simply a quirk - someone took the time to create a StringBuilder but used it with string concatenation, and it is funny. It's like that <center align="right"> that I see from time to time (but in some browsers it does not work anymore).


Log in to reply