Bad code as art form



  • (I submitted this as a Code SOD but I post it here in case it isn't published)

    An ex-coworker of ours took the simplest of tasks and gave them a whole new level of abstraction. Some would call him a terrible programmer, I call him an artistic genius:

            private Decimal GetNumStatsPerPage()
    {
    int factor = 0;
    if (this.demoGroupLimit % this.stats.Length == 0) //if stats per page is a multiple of num demos
    return (Decimal)this.demoGroupLimit;
    else //if not a multiple, return smaller value until it is a multiple of num demos to fit stats per demo per page
    {
    for( int idx = 1 ; idx < this.demoGroupLimit; idx++ )
    {
    if ((this.demoGroupLimit - idx) % this.stats.Length == 0)
    {
    factor = idx;
    break;
    }
    else
    continue;
    }
    return (Decimal)this.demoGroupLimit - factor;
    }
    }

    Note the use of else-continue, a common paradigm among his masterpieces, and the blatant use of Decimal instead of int is so post-modern. Here is the "correct", but totally unaesthetically pleasing version version of the code:

    	private int GetNumStatsPerPage()
    {
    return this.demoGroupLimit - this.demoGroupLimit % this.stats.Length;
    }


  • Does "continue" even get through the compiler outside of loops? I thought it was the Java equivalent for Fortran's "cycle", but I might be confusing it with PHP.



  • @Arancaytar said:

    Does "continue" even get through the compiler outside of loops? I thought it was the Java equivalent for Fortran's "cycle", but I might be confusing it with PHP.


    But it's in a for loop...



  • @Bob said:

    @Arancaytar said:
    Does "continue" even get through the compiler outside of loops? I thought it was the Java equivalent for Fortran's "cycle", but I might be confusing it with PHP.


    But it's in a for loop...

     

    Nice ! The else statement is utterly useless. The continue does function properly, but totally unnecessary... its the end of the loop.



  • @Arancaytar said:

    Does "continue" even get through the compiler outside of loops? I thought it was the Java equivalent for Fortran's "cycle", but I might be confusing it with PHP.

    It is in a loop.  But in this situation, a good compiler will chuck it out.


Log in to reply