hate these ads?, log in or register to hide them
Page 3 of 118 FirstFirst 1234561353103 ... LastLast
Results 41 to 60 of 2356

Thread: <xml><thread isTerrible="true" isUnderstandable="false" /></xml> - Coding help thread

  1. #41
    dpidcoe's Avatar
    Join Date
    June 29, 2011
    Location
    San diego
    Posts
    4,908
    Quote Originally Posted by Waagaa View Post
    Real life example, which IMO doesn't need more documentation than the test and the code + method names / javadocs.
    It's a servlet 2.5 hack for httpOnly cookies. Ugh, java...
    It might be because I don't know anything about web coding, but what's the reasoning behind replacing the single \ with 5 of them in this part?
    Code:
    String value = cookie.getValue().replaceAll("\"", "\\\\\"");
    edit: doh, missed the second quote. Looks like " is being replaced with \\" , which makes sense because the next thing I was going to ask was why was there a \ with no escape sequence.

  2. #42
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    11,971
    Yep it's " being replaced with \\" so something else will know to escape it I expect. Also not sufficient to make the resulting value safe. Why does that code exist/what is it meant to do?

  3. #43
    Donor
    Join Date
    April 11, 2011
    Location
    The Netherlands
    Posts
    1,027
    Quote Originally Posted by Daneel Trevize View Post
    Yep it's " being replaced with \\" so something else will know to escape it I expect. Also not sufficient to make the resulting value safe. Why does that code exist/what is it meant to do?
    Cookie values need to be quoted and thus quotes need to be escaped, that's why You don't want to html-escape it, because that's not how the cookie-spec works.

  4. #44
    Qwert's Avatar
    Join Date
    April 10, 2011
    Location
    Elsewhere
    Posts
    1,827
    Quote Originally Posted by dpidcoe View Post
    Quote Originally Posted by Waagaa View Post
    Real life example, which IMO doesn't need more documentation than the test and the code + method names / javadocs.
    It's a servlet 2.5 hack for httpOnly cookies. Ugh, java...
    It might be because I don't know anything about web coding, but what's the reasoning behind replacing the single \ with 5 of them in this part?
    Code:
    String value = cookie.getValue().replaceAll("\"", "\\\\\"");
    edit: doh, missed the second quote. Looks like " is being replaced with \\" , which makes sense because the next thing I was going to ask was why was there a \ with no escape sequence.
    This is a perfect example for what inline comments can be useful for. A simple

    Code:
    \\ Replaces " with \\"
    would have added clarity to otherwise confusing code.

  5. #45

    Join Date
    April 13, 2011
    Posts
    5,185
    I wouldn't have called it confusing, particularly if you're looking at it in a proper IDE that can tell the outer quotes are there to make it a literal and the left \s of each pair to escape - replaceAll() is a standard String method. I can definitely see it being confusing if you're not used to Java, but if you look at it every day of the week that kind of nonsense is second nature.
    Last edited by elmicker; April 18 2013 at 12:48:27 AM.

  6. #46
    Qwert's Avatar
    Join Date
    April 10, 2011
    Location
    Elsewhere
    Posts
    1,827
    Quote Originally Posted by elmicker View Post
    I wouldn't have called it confusing, particularly if you're looking at it in a proper IDE that can tell the outer quotes are there to make it a literal and the left \s of each pair to escape - replaceAll() is a standard String method. I can definitely see it being confusing if you're not used to Java, but if you look at it every day of the week that kind of nonsense is second nature.
    Backslashes escaping things is one thing, 5 of them in a row forces you to start counting.

  7. #47
    NoirAvlaa's Avatar
    Join Date
    April 12, 2011
    Location
    Liverpool, laaaa
    Posts
    4,157
    Quote Originally Posted by Qwert View Post
    Quote Originally Posted by elmicker View Post
    I wouldn't have called it confusing, particularly if you're looking at it in a proper IDE that can tell the outer quotes are there to make it a literal and the left \s of each pair to escape - replaceAll() is a standard String method. I can definitely see it being confusing if you're not used to Java, but if you look at it every day of the week that kind of nonsense is second nature.
    Backslashes escaping things is one thing, 5 of them in a row forces you to start counting.
    Personally, would you pair them off first to make it easier to count? ie.

    "\\" + "\\" + "\""

  8. #48
    Donor
    Join Date
    April 11, 2011
    Location
    The Netherlands
    Posts
    1,027
    Quote Originally Posted by NoirAvlaa View Post
    Quote Originally Posted by Qwert View Post
    Quote Originally Posted by elmicker View Post
    I wouldn't have called it confusing, particularly if you're looking at it in a proper IDE that can tell the outer quotes are there to make it a literal and the left \s of each pair to escape - replaceAll() is a standard String method. I can definitely see it being confusing if you're not used to Java, but if you look at it every day of the week that kind of nonsense is second nature.
    Backslashes escaping things is one thing, 5 of them in a row forces you to start counting.
    Personally, would you pair them off first to make it easier to count? ie.

    "\\" + "\\" + "\""
    For readability that's really nice, but it's also really expensive and might cause some code analysis tooling to start whining.
    It's much more readable in IntelliJ IDEA, but a comment might actually be useful here, hehe
    Reason why it's expensive: Strings in java are immutable. So every addition causes a new string to be created in memory.
    Now under the hood I think the compiler nowadays turns this into a String builder and "+" gets translated into a call to the add() method of the string builder, but I'm not entirely sure. That'd be less expensive than lots of string literals being allocated and garbage collected, but for something that's called a couple of thousand times a second on some websites, it's not bad to keep this performing well. Would be nice if the compiler would optimize it away, not sure if it does that.
    Could also turn it into a constant, hmm..
    Last edited by Waagaa; April 18 2013 at 06:10:41 PM.

  9. #49
    Donor Aea's Avatar
    Join Date
    April 13, 2011
    Location
    Colorado
    Posts
    13,500
    If one way of doing things is 5x smaller but the difference is imperceptible overall then it doesn't matter. I'd err towards the side of readability every time. Performance optimization typically comes from small critical sections, not doing micro-optimizations everywhere.

  10. #50
    Donor
    Join Date
    April 11, 2011
    Location
    The Netherlands
    Posts
    1,027
    True that. Premature optimization is a lot like premature ejaculation: bad.

    Quote Originally Posted by Aea View Post
    If one way of doing things is 5x smaller but the difference is imperceptible overall then it doesn't matter. I'd err towards the side of readability every time. Performance optimization typically comes from small critical sections, not doing micro-optimizations everywhere.

  11. #51
    Qwert's Avatar
    Join Date
    April 10, 2011
    Location
    Elsewhere
    Posts
    1,827
    Try changing the while loop to a do{}while(). Running the loop once regardless is exactly why it exists.

  12. #52
    dpidcoe's Avatar
    Join Date
    June 29, 2011
    Location
    San diego
    Posts
    4,908
    The second else if is unnecessary, it could just be an else. Though that's not a big deal, and in this case helps with readability (an else with an inline comment would be just as readable though imo).

    Also, people on stackoverflow seem to agree that using namespace std; is really bad. Though I think that for something as simple as this, lack of "std::" everywhere is preferable due to the pretty much non-existent chance of colliding something with the standard namespace.

  13. #53
    Movember 2012 Warmenhoven's Avatar
    Join Date
    June 29, 2011
    Location
    All over the shop
    Posts
    859
    Currently I'm writing a program that goes:

    Run compiled Fortran 95 program
    Fortran 95 -> creates datafile
    Fortran 95 -> calls Gnuplot
    Gnuplot -> turns datafile into jpegs + saves
    Exit
    Run Gimp to get jpeg -> gif


    Is there a way to get from the execution of the Fortran program to a final gif without exiting?

  14. #54

    Join Date
    April 9, 2011
    Posts
    337
    Quote Originally Posted by dpidcoe View Post
    Also, people on stackoverflow seem to agree that using namespace std; is really bad. Though I think that for something as simple as this, lack of "std::" everywhere is preferable due to the pretty much non-existent chance of colliding something with the standard namespace.
    The lack of "std::" is not really an advantage. Obviously for something as small as this it doesn't matter, but in large applications it is preferable because it makes it immediately clear where the method is coming from. It makes things more explicit and easier to identify.


    As far as style improvements: declare and initialize 'volume' and 'bouyant' on the same line, and you can even make them const to show your not going to change them. Also, pull the PI variable out of main and make it static.

    Don't worry about using float or double and just always use double. There are some instances when float might be preferable, but they are so rare and specific that by the time you run into them you'll know if you need it or not.
    Last edited by MortyM; April 20 2013 at 06:46:53 AM.

  15. #55
    balistic void's Avatar
    Join Date
    May 5, 2011
    Location
    Dublin/London
    Posts
    2,630
    It's fine to put "using namespace std;" in a cpp file. The big NONO is if you use it in a header coz then it can cause pollution.

  16. #56

    Join Date
    April 16, 2011
    Posts
    269
    Is there a good way of avoiding large if-then-else statements?

    I often end up using large
    if a:
    doA()
    elif b:
    doB()
    ...
    else:
    doSomethingElse()

    This is ok if it's one or two, but I always end up just adding to it as the program expands and suddenly I have a statement with 10 elif and a hassel to add or change.

    Is there a particular pattern/approach that can be used in general to avoid this or is it very specific for each program/problem?

  17. #57
    מלך יהודים Zeekar's Avatar
    Join Date
    April 10, 2011
    Posts
    14,513
    cases should be the answer for you renox.


    

  18. #58

    Join Date
    April 16, 2011
    Posts
    269
    Quote Originally Posted by Zeekar View Post
    cases should be the answer for you renox.
    Switch-cases? I'd only consider them a marginal improvement as they are almost the same in my optics, but I might be mistaken in how similar they are?

  19. #59
    מלך יהודים Zeekar's Avatar
    Join Date
    April 10, 2011
    Posts
    14,513
    Well they are similar ( else if is a version of switch-case iirc ) its just easier to add or remove changes with them and they are easier to manage in my experience ( which again is also very limited ). I would also guess it depends very much on the program/case itself.


    

  20. #60
    Tools's Avatar
    Join Date
    April 9, 2011
    Posts
    840
    Yeah use a switch statement if the argument is something you can switch on. If it's not then it's time for a ~hashtable~of~function~pointers~.

    The hashtable of function pointers is also my favourite way to make shitty cs students who are scared of C cry.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •