Friday, March 2, 2018

Cute Puppies Against Evil Food

As a dog, you’re on the hunt for food, practically all day every day. So naturally, when an opportunity presents itself where food is just lying on the floor, unguarded, life couldn’t be better, right? […]

The post Cute Puppies Against Evil Food appeared first on Viral Viral Videos.



To read the full article, please visit Viral Viral Videos

Error'd: I Don't Always Test my Code, but When I do...

"Does this mean my package is here or is it also in development?" writes Nariim.

 

Stuart L. wrote, "Who needs a development environment when you can just test in production on the 'Just In' feed?"

 

"It was so nice of Three to unexpectedly include me - a real user - in their User Acceptance Testing. Yeah, it's still not fixed," wrote Paul P.

 

"I found this great nearby hotel option that transcended into the complex plane," Rosenfield writes.

 

Stuart L. also wrote in, "I can't think of a better place for BoM to test out cyclone warnings than in production."

 

"The Ball Don't Lie blog at Yahoo! Sports seems to have run out of content during the NBA Finals so they started testing instead," writes Carlos S.

 

[Advertisement] Otter allows you to easily create and configure 1,000's of servers, all while maintaining ease-of-use, and granular visibility down to a single server. Find out more and download today!


To read the full article, please visit The Daily WTF

Thursday, March 1, 2018

Skimbles, The Epic Cat Fail

Jumping cats, you’ve probably seen them before. But have you seen Skimbles? This cat has a history of failing his jumps, yet somehow he convinces himself next time will be better. Well… bad luck.

The post Skimbles, The Epic Cat Fail appeared first on Viral Viral Videos.



To read the full article, please visit Viral Viral Videos

Skydiving In A Car

Skydiving is by definition only for the brave. But what happens when you add a car into the mix? Would you feel safer or would it add to the anxiety??

The post Skydiving In A Car appeared first on Viral Viral Videos.



To read the full article, please visit Viral Viral Videos

CodeSOD: What a Stream

In Java 8, they added the Streams API. Coupled with lambdas, this means that developers can write the concise and expressive code traditionally oriented with functional programming. It’s the best bits of Java blended with the best bits of Clojure! The good news, is that it allows you to write less code! The better news is that you can abuse it to write more code, if you’re so inclined.

Antonio inherited some code written by “Frenk”, who was thus inclined. Frenk wasn’t particularly happy with their job, but were one of the “rockstar programmers” in the eyes of management, so Frenk was given the impossible-to-complete tasks and given complete freedom in the solution.

Frenk had a problem, though. Nothing Frenk was doing was actually all that impossible. If they solved everything with code that anyone else could understand, they wouldn’t look like an amazing genius. So Frenk purposefully obfuscated every line of code, ignoring indentation, favoring one-character variable names, and generally trying to solve each problem in the most obtuse way possible.

Which yielded this.

    Resource[] r; //@Autowired ndr
    Map<File, InputStream> m = null;
    if (file != null)
{
    m.put(file, new FileInputStream(file));}else
{

    m = Arrays.stream(r).collect(Collectors.toMap(x -> { try { return x.getFile(); }
catch (Exception e) { throw new IllegalStateException(e);}},
    x -> {try{return x.getInputStream();}catch (Exception e){throw new IllegalStateException(e)
;}}));
}

As purposefully unreadable code, I’d say that Frenk fails. That’s not to say that it isn’t bad, but Frenk’s attempts to make it unreadable… just make it annoying. I understand what the code does, but I’m just left wondering at why.

I can definitely say that this has never been tested in a case where the file variable is non-null, because that wouldn’t work. Antonio confirms that their IDE was throwing up plenty of warnings about calling a method on a variable that was probably null, with the m.put(…) line. It’s nice that they half-way protect against nulls- one variable is checked, but the other isn’t.

Frenk’s real artistry is in employing streams to convert an array to a map. On it’s surface, it’s not an objectively wrong approach- this is the kind of things streams are theoretically good at. Examine each element in the array, and apply a lambda that extracts the key and another lambda that extracts the value and put it into a map.

There are many real-world cases where I might use this exact technique. But in this case, Antonio refactored it to something a bit cleaner:

        Resource[] resources; //@Autowired again
        Map<File, InputStream> resourceMap = new HashMap<>();
        if (file != null)
            resourceMap.put(file, new FileInputStream(file));
        else
            for (Resource res : resources)
                resourceMap.put(res.getFile(), res.getInputStream());

Here, the iterative approach is much simpler, and the intent of the code is more clear. Just because you have a tool doesn’t make it the right tool for the job. And before you wonder about the lack of exception handling- both the original block and the refactored version were already wrapped up in an exception handling block that can handle the IOException that failed access to the files would throw.

[Advertisement] Onsite, remote, bare-metal or cloud – create, configure and orchestrate 1,000s of servers, all from the same dashboard while continually monitoring for drift and allowing for instantaneous remediation. Download Otter today!


To read the full article, please visit The Daily WTF