Article 3Z5T7 CodeSOD: Flip to a Blank Page

CodeSOD: Flip to a Blank Page

by
Remy Porter
from The Daily WTF on (#3Z5T7)

You have a web application, written in Spring. Some pages live at endpoints where they're accessible to the world. Other pages require authentication, and yet others require users belong to specific roles. Fortunately for you, Spring has features and mechanisms to handle all of those details, down to making it extremely easy to return the appropriate HTTP error.

Unfortunately for you, one of the developers on your team is a Rockstara who is Officially Very Smart and absolutely refuses to use the tools your platform provides. When that Certified Super Genius leaves the organization, you inherit their code.

That's what happened to Emmer. And that's how they found this:

List<String> typeList = getTypeList (loginName);if(CollectionUtils.size(typeList) > 0){ return viewRepository.findBySubmsnTypeList(typeList, pr);}else{ return viewRepository.findEmptyPage(pr);}

This doesn't look too bad, does it? It's not great- why are roles called "types", why are we representing them with strings, why are we checking if a user is logged in by checking which roles they have, and not whether or not they're logged in" and why on Earth would you send the user an empty page if they're not authenticated?

The question is: how do you generate an empty page? If "just return an empty view object" is what you thought you'd do, you're obviously not a Rockstara.

@Query(value = "from viewTable where 1=0", nativeQuery = false)public Page<viewTable> findEmptyPage(Pageable pr);

If you want to return an empty page, you run a query against the database which is guaranteed to return absolutely no results. That guarantees that you'll send a blank page back, because there's no data to put on the page. Genius! This way, returning nothing requires a hop across the network and a call to the database, instead of just, y'know, returning nothing (or even better, returning an error page).

Suffice to say, when this master programmer gave their two weeks notice, Emmer and the rest of the team suggested that this programmer should spend their last two weeks on vacation.

raygun50.png [Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more. TheDailyWtf?d=yIl2AUoC8zAyD_Mn09ZHpM
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments