Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

 

Throw me a drone, here: How do I win this code competition?

Last week, we announced the launch of our code competition.

Read post

Last week, we announced the launch of our code competition. It's a huge opportunity for any SilverStripe code cobbler with a penchant for amateur aviation. The way it works is dead simple: you submit some of your beautiful code to us, and we evaluate it against all the other submissions to see if it's the best one. If it is, you win a quadcopter drone. It's seriously the most awesome transaction since Nicolas Cage bought a dinosaur skull.

The "best" code? That's a bit subjective.

We have a set of criteria we're using to evaluate the code to make it a more objective process. We wrote about this in a previous blog, but just to recap, the code must be less than a certain number of lines, adhere to established SilverStripe coding conventions, and do something that really showcases the power and appeal of SilverStripe Framework. That last one is the tricky bit, and in this post, we hope to provide some clarity on what we're looking for to show off SilverStripe Framework.

First, keep in mind what the end game is for us. We have some prime real estate on our software page set aside for showing code examples of SilverStripe Framework. For newcomers to the community, this is one of the primary experiences that will define their first impression of the open-source product, so we need it to be appetising, exciting, and, above all, differentiating. We find that developers choose SilverStripe for the elegance and flexibility of its framework, and we want to broadcast that message. A picture is worth <?php echo pow(10, 3); ?> words, so what better medium than code examples to enthrall a crowd of tire-kicking CMS hunters?

Sounds great, but I could use some examples.

One important quality we're looking for is that the code is cohesive and evinces clear functionality.

Bad:


class ProductController extends Controller {

  public function makeItRain() {
    $this->Status = 'PURPLE';

    if(date('w') == 3) {
        $this->Price -= 4 * pi();
    }

    $result = $paymentApi->purchase($config->Price);

    if($result === true) {
        return new RSSFeed();
    }

    return parse_url('http://friendster.com');
  }

}

Better:


class ProductController extends Controller {

  public function doPurchase(Product $product, $data) {
    $result = PaymentGateway::inst()->purchase(
        $product->Price,
        $data['CreditCard']
    );

    if($result->Status === 'OK') {
        return $this->redirect($this->Link('success'));
    }

    SS_Log::log($result->Error, SS_Log::ERR);

    return $this->redirect($this->Link('failure'));
  }

}

We're also looking for submissions that exemplify the SilverStripe way of doing things. Whenever possible, use the methods that are afforded to you by the framework to accomplish a task that can be done with core PHP functions.

Bad:


class MenuItem extends DataObject {

  public function saveToDatabase() {
    if(isset($_GET['sendEmail'])) {
        mail(
            'admin@example.com',
            'New menu item',
            'Someone added an item to the menu. Log into the website to see it.'
        );
    }

    DB::query('UPDATE MenuItem SET Title = "'.$this->Title.'"');

  }

}

Better:


class MenuItem extends DataObject {

  public function onAfterWrite() {
    parent::onAfterWrite();

    if(Controller::curr()->getRequest()->getVar('sendEmail')) {
        $email = Email::create(
            Config::inst()->get('Email', 'admin_email'),
            'New menu item'
        );
        $email->setTemplate('NewMenuItemEmail');
        $email->populateTemplate(array(
            'Item' => $this
        ));
        $email->send();
    }
  }

}

Lastly, try to show off the features that differentiate SilverStripe from other frameworks. Common, dry utility methods may be useful, but they're not a huge selling point of a framework.

Bad:


class ProductPageController extends PageController {

  public function getProductJSON(Product $product) {
    $variations = $product->getVariations();
    if(ArrayLib::is_associative($variations)) {
        return Convert::array2json($variations);
    }
  }

}

Better:


class ProductPageController extends PageController {

  public function productdetail(SS_HTTPRequest $request) {
    $product = Product::get()->byID($request->param('ID'));

    if(!$product) {
        return $this->httpError(404,'That product could not be found!');
    }

    if($request->isAjax()) {
        $json = Convert::array2json($product->getVariations());
        $response = new SS_HTTPResponse($json, 200);
        $response->addHeader('Content-Type', 'application/json');

        return $response;
    }

    return $this->renderWith(array(
        'ProductDetailPage',
        'Page'
    ));
  }

}

Alright, I'm droning on and on, here...

We really want to see some great submissions to our code competition! Seize the moment and stake your claim to a slice of SilverStripe glory. If you have any queries or feedback, leave a comment below, and we'll be sure to respond to you. Good luck!

About the author
Aaron Carlino

Aaron Carlino, better known by his whimsical pseudonym Uncle Cheese has been an active member of the SilverStripe community since 2007, and has never looked back. In that time, he has established himself as a support resource, mentor, and contributor of some of the framework's most popular open source modules.

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments