Copy&Paste factory pattern

When You take project over, after it being very long time in development, You can find very funny code snippets. Then You understand why project was soooo long in development. It is related rather with developer’s (not so)high skills ;)

One example (and there is more pieces, even better qualified as entry for The Daily WTF). It seems that is perfect example of design pattern known as Copy&Paste factory:

<a href="#">
<% if session[:lang] && session[:lang] == "fr" %><img src="/images/LanguageToolFlags/flag-fr.gif" alt="fr_flag"/><% end -%>
<% if session[:lang] && session[:lang] == "en" %><img src="/images/LanguageToolFlags/flag-gb.gif" alt="gb_flag"/><% end -%>
<% if session[:lang] && session[:lang] == "es" %><img src="/images/LanguageToolFlags/flag-es.gif" alt="es_flag" /><% end -%>
<% if session[:lang] && session[:lang] == "nl" %><img src="/images/LanguageToolFlags/flag-nl.gif" alt="nl_flag" /><% end -%>
<% if session[:lang] && session[:lang] == "it" %><img src="/images/LanguageToolFlags/flag-it.gif" alt="it_flag" /><% end -%>
<% if session[:lang] && session[:lang] == "ru" %><img src="/images/LanguageToolFlags/flag-ru.gif" alt="ru_flag" /><% end -%>
<% if session[:lang] && session[:lang] == "de" %><img src="/images/LanguageToolFlags/flag-de.gif" alt="de_flag" /><% end -%>
 <%=_("Languages") %></a>

Do I have to say, that it was written as a single line :) ?

Now You know why customer had to find someone to finish application (read – rewrite)…

6 thoughts on “Copy&Paste factory pattern

  1. @Sebastian
    I would start with something like:

    <% if session[:lang] -%>
    <img src="/images/LanguageToolFlags/flag-<%= session[:lang]%>.gif" alt="<%= session[:lang]%>_flag"/>
    <% else -%>
    <%= _("Language") %>
    <% end -%>
  2. Pingback: 2mind - transcedentalne tworzenie oprogramowania» Archiwalia » Refaktoryzacja - wst?p oraz wzorzec copy & paste

  3. Pingback: Shoulda You abandon Test::Unit? : NetManiac

  4. Pingback: Refaktoryzacja - wst?p oraz wzorzec copy & paste - Software is fun

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.