• P (unregistered)

    Looks like they're directly translated from LINQ.

  • (nodebb)

    return "PR" + 1;

    Every time I see code like the above, I want to start stabbing people.

    Why?

    Er, because eventually they bring this habit to C or C++, and create jolly bugs (er, it returns "R"...).

  • Naomi (unregistered)

    Here's hoping the comment box will handle Markdown.

    return "PR" + prices.stream()
        .map(Price::getOfferPriceId)
        .map(price -> price.split("PR")[1])
        .mapToInt(Integer::parseInt)
        .max()
        .orElse(0)
    

    As for everything else wrong with this...

  • (nodebb)

    I love when people say a particular language is crap, when in fact it is the "programmers" who type crap and then blame the platform for being somehow inadequate.

  • Kleyguerth (github) in reply to Bananafish

    I tend to call a language crap when the vast majority of programmers and online tutorials are crap (e.g. PHP) because this means the language itself breeds more crap programmers, who will write more crap tutorials and then become crap managers on crap companies using that crap language.

  • (nodebb)

    TRWTF is the use of the word stream to describe an enumeration.

  • (nodebb)

    Obvious fix for the exception is

     return  "PR" -1 
    Everyone knows a negative value is an error, right?

  • Missy (unregistered)

    Too bad there isn't a way to create some sort of Globally Unique token that could be used as an IDentifier.

  • Bobby Coder (unregistered)

    For anyone looking for better code, some options are below. Which is more readable to you is a matter of taste, but I expect to get lots of angry feedback both ways ;)

    Of course, storing the PK as a string is a giant WTF, but for the sake of this exercise let's pretend that can't be changed.

    	public String createPriceId_A() {
    		long maxId = prices.stream()
    			.mapToLong(price -> Long.parseLong(price.getOfferPriceId().split("PR")[1]))
    			.max().orElse(0);
    		return String.format("PR%s", maxId + 1);
    	}
    
    	public String createPriceId_B() {
    		long maxId = prices.stream()
    			.map(price -> price.getOfferPriceId())
    			.map(idStr -> idStr.split("PR")[1])
    			.mapToLong(idStr -> Long.parseLong(idStr))
    			.max().orElse(0);
    		return String.format("PR%s", maxId + 1);
    	}
    
  • I dunno LOL ¯\(°_o)/¯ (unregistered) in reply to cellocgw

    It should be "PR#6" because that's how you reboot an Apple II.

  • sizer99 (google) in reply to Bananafish

    You can program perl in any language, and I've seen two perl codebases that were amazingly clean, readable, and maintainable (really the only thing that matters) - but they did that by not using most of perl, treating it like it was C.

    Languages just have a code smell - how baseline unmaintainable their code is if you just write code without discipline. And bad programmers are attracted to those with stronger code smells, like flies to crap, because it's easier to bang crap out in completely undisciplined languages - but you also find them in other rooms of the house, buzzing around confused when someone closes the door and traps them in C#. And then they will probably write perl in C#, you're right there. But there are crap-encouraging languages.

  • ID collision? (unregistered)

    Why are all the IDs for the entity available to the entity when creating a new ID? What happens during multithreaded operations when an entity gets an ID, then doesn't get saved to the DB immediately and another entity comes in on another thread using the same ID and gets saved first?

    Generated IDs are the only way to go.

  • Just for Laughes (unregistered)

    Hey!

    You finished early!

    Great!

    Cache it, ship it, make the next one better please.

    If anyone complains, send them to sales.

    Who's sales?

    YOU.

    and if you are going to be doing stuff like this, I gotta get a taste. Because next time, there won't be a next time.

    I DONT CARE.

  • owlstead (unregistered) in reply to Mr. TA

    It stopped being an enumeration when it returned "PR1" ("PR" + 1) in case of any error. Then it just became an indexed stream, or indeed list to be processed. I'm already envisioning a future event where two DB need to be reconciled after a split and they find out that there are multiple rows with the same ID (because at some point the row with the highest ID was removed and any new element inserted). That's a poor DBA who has to deal with that (or, if he's payed by the hour, a rich one of course).

Leave a comment on “Swimming Downstream”

Log In or post as a guest

Replying to comment #:

« Return to Article