Dom’s Blog

Polysyllabic Pretension Pertaining Primarily to Programming

Coding Sample

When giving out advice on code, I believe in the school of thought that says “put up or shut up.”  It’s difficult for me to respect the code of others when they won’t show their chops, so to speak.  Here is me putting up so you can see for yourself if I need to shut up, or if you can learn something of value here.  This code is a sample class to, of course, tell the world ‘Hello!’ with a little style.

<?php

/**
 * class Hello
 *
 * This class helps us display our feelings to the world.
 *
 * LICENSE: You are free to do absolutely anything with this
 *          file.
 *
 * @copyright  2009 Domenic R. Merenda
 * @license    Unrestricted, no specific license
 * @version    1.00
 * @link       http://www.edgeprod.com
 * @since      File available since Release 1.00
 * @todo       Take over the world with this file alone
 */

class Hello {

   public $returnText = 'Hello World!';  // Stores the string to return

   /**
    * public function getHelloText($moodId,$smileyId)
    *
    * @param   integer  $moodId     0 = happy, 1 = sad, 2 = silly
    * @param   integer  $smileyId   0 = :), 1 = :(, 2 = 8-)
    * @return  string   returns the switch()-determined value of $moodId
    *                   and $smileyId
    */

    public function getHelloText($moodId,$smileyId) {

        $returnText .= ' '; // Add a space for human readability

        // Add the next sentence based on our mood ($moodId parameter)

        switch($moodId) {

            case 0 : // Happy
               $returnText .= 'I am extremely happy!';
            break;

            case 1 : // Sad
               $returnText .= 'I am so sad!';
            break;

            case 2 : // Silly
               $returnText .= 'I stick cereal in my nose!';
            break;

            // If the parameter is incorrect, return an error

            default :
               return 'Parameter $moodId must be 0, 1, or 2!';
            break;
        }

        $returnText .= ' '; // Add another space for human readability

        // Add a smiley based on the $smileyId parameter

        switch($smileyId) {

            case 0 : // Smiley Face
               $returnText .= ':)';
            break;

            case 1 : // Frownie Face
               $returnText .= ':(';
            break;

            case 2 : // Silly Face
               $returnText .= '8-)';
            break;

            // If the parameter is incorrect, return an error

            default :
               return 'Parameter $smileyId must be 0, 1, or 2!';
            break;
        }

        return $returnText;
    }
}

?>
 

4 Responses to “Coding Sample” (post new)

  1.  

    Public variable? Where’s the encapsulation? You are using a class and making objects of that class right? If so you forgot one of the most important parts of object oriented programming. Unless you are using this class statically – in which case it’s still coded wrong.
    No offense but it looks like you learned to program using PHP instead of a real programming language.

  2.  

    You never actually define $returnText before appending data to it.

    You are also relying on Type Juggling in PHP for comparison in your switch. You specify in your comments that it’s expecting an integer, but your switching is testing for strings.

  3.  

    Semantics, on the integer/strings because it’s in the comments for readability by another (future) coder. PHP doesn’t have strong typing — if this were C++ or a C derivative, I would have certainly made sure the variable typing was specific to the language. I’m not a fan of needless (and in PHP, unnecessary) complexity. I guess that’s part of what separates me from the pack.

    I don’t see where you’re saying $returnText isn’t defined. If you’re new at reading code, look toward the top of the class, where it’s defined as a public variable for the methods to use.

  4.  

    @HarryMuff: I tend to code with an eye toward the actual language itself. If you’re coding in PHP, don’t try to shoehorn in “real” programming languages, as you term them. When you only have a hammer, every problem looks like a nail.

    Fortunately, my tool box isn’t as bare.

Leave a Reply