Jump to content

  • Log In with Google      Sign In   
  • Create Account


Code Sample Review Request


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 09 October 2012 - 04:29 PM

I'm putting together a job application for a generalist programmer position, and I'm in the process of selecting a code sample for my review, and I would very much like to hear any feedback before sending it off. Too simple, could be written better... ect. Whatever comes to mind if you were handed it as a potential applicant.

As always, thanks for the help.



[source lang="cpp"]/* Core game class. Manages the elements, start and end of a game. Instantiated by main.cpp*/#include "Game.h"Game::Game(){ // Set default values gameInProgress = false; navigating = false; selectedNode = NULL; player = NULL; lastCoreTransform = NULL;}Game::~Game(){ if( gameInProgress ) endGame(); delete lastCoreTransform;}// Starts a new game, if one is not already runningvoid Game::startGame( int levelID ){ // Break if a game is already in progress if( gameInProgress ) return; gameInProgress = true; player = new Player(); // Create default cameras cameras.push_back( new Camera() ); //player camera cameras.push_back( new Camera() ); //fixed cam // Set camera offset, and set it to follow the player's ship currentCamera = cameras[0]; currentCamera->translate( 0, -30, -100.0f ); currentCamera->rotate ( 0.0f, 0.0f, 0.0f ); currentCamera->camTarget = player; // Offset the static camera cameras[1]->translate( 0, 0, -100.0f ); setCamera(0); // Generate the level, based on the passed in ID GameLevelData::createLevel( levelID );}// Ends the curent game, and deletes all non texture assetsvoid Game::endGame(){ gameInProgress = false; if( player != NULL ) delete player; player = NULL; // Delete all Nodes, Actors and Cameras while( Node::getNodes().size() > 0 ) delete (*Node::getNodes().begin()); while( Actor::getActors().size() > 0 ) delete (*Actor::getActors().begin()); for( int i = cameras.size()-1; i >= 0; i-- ) delete cameras[i];}// Function to be run, each time the frame updatesvoid Game::update( int timePassed ){ handleInput(); // Process user input updateActors( timePassed ); // Update all actors, AI, and physics updateCamera( timePassed ); // Update the current camera drawScene(); // Draw scene data drawHud(); // Draw 2d HUD elements}// Function to generate the view matrixvoid Game::updateCamera(){ // If a game is not running, or a camera is not set, view matrix is just an identity matrix if( !gameInProgress || currentCamera == NULL ) { iGLEngine->viewMatrix->setMatrix( mat4(1.0f) ); } else if( currentCamera->camTarget == NULL ) { // Camera has no target, so retun just the cameras transform matrix iGLEngine->viewMatrix->setMatrix( *currentCamera->getTransform()->getMatrix() ); } else { // We have a camera target. Interpolate the last camera position, a set percentage, towards // the transform of the camera target. lastCoreTransform->interpolate(currentCamera->camTarget->getCoreTransform(), 0.5f, //Rot multiplier 0.25f ); //Pos Multiplier // Apply the camera's offset, to the interpolated transformation, and set it as the current view Matrix iGLEngine->viewMatrix->setMatrix( (*currentCamera->getTransform()->getMatrix()) * glm::inverse( *lastCoreTransform->getMatrix() ) ); }}// Handles user input at the game levelvoid Game::handleInput(){ // Get a pointer to the current input state via a static accessor InputState *inputState = InputState::getInputState(); // Toggle between the two cameras with the number keys if( inputState->keyDown( SDL_SCANCODE_1 ) ) setCamera( 0 ); if( inputState->keyDown( SDL_SCANCODE_2 ) ) setCamera( 1 );}// Updates all actors, processes AI (if any), and physicsvoid Game::updateActors( int timePassed ){ for ( list<Actor *>::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); it++ ) { (*it)->update( timePassed ); }}// Draws the scene elementsvoid Game::drawScene(){ for ( list<Node *>::iterator it = Node::getNodes().begin(); it != Node::getNodes().end(); it++ ) { Mesh *mesh = static_cast<Mesh *>(*it); if( mesh && mesh->getParent() == NULL ) { mesh->drawMesh(); } }}// Recursive function, to delete a node, and all it's children.void Game::deleteNode( Node *node ){ list<Node *> children = node->getChildren(); for ( list<Node *>::iterator it = children.begin(); it != children.end(); it++ ) { deleteNode( (*it) ); } delete node;}// Sets the current cameravoid Game::setCamera( int index ){ // Verify that the passed in index is within boundaries index = ( index < 0 ) ? 0 : index; index = ( index > (int)cameras.size() ) ? (int)cameras.size() -1 : index; currentCamera = cameras[index]; // If the camera has a target, make sure to set the data for the 'lastCoreTransform' if( currentCamera->camTarget != NULL ) { // Create the lastCoreTransform if it does not already exist if( lastCoreTransform == NULL ) lastCoreTransform = new Transform(); lastCoreTransform->setMatrix( *currentCamera->camTarget->getCoreTransform()->getMatrix() ); }}[/source]

Sponsor:

#2 Kyall   Members   -  Reputation: 287

Like
1Likes
Like

Posted 09 October 2012 - 05:15 PM

for ( list::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); it++ )
  {
   (*it)->update( timePassed );
  }

++it

The postfix increment, it++, creates a copy of the object prior to the operation for comparison of the old value, and it isn't necessary here, the prefix increment doesn't do this. so:

for ( list::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); ++it )
  {
   (*it)->update( timePassed );
  }

Don't compare against a function call in a loop, where you have

it != Actor::getActors().end();

Change it so you create a local iterator value called end, so you end up with:
for ( list::iterator it = Actor::getActors().begin(), end = Actor::getActors().end(); it != end; ++it )
  {
   (*it)->update( timePassed );
  }

And lastly (*it)-> looks like a bug. Without the class to look at to see that the *it operator is a dereference for the iterator and the class for actor to look at to see that -> is a valid reference for that class I'm just having alarms shoot off here. If that is valid syntax you need to put a comment in there to explain why you're dereferencing twice.

for ( list::iterator it = Actor::getActors().begin(), end = Actor::getActors().end(); it != end; ++it )
  {
   // The Actor class overloads -> because it is stored as a shared pointer (example)
   (*it)->update( timePassed );
  }

// Delete all Nodes, Actors and Cameras
  while( Node::getNodes().size() > 0 )
   delete (*Node::getNodes().begin());
  
  while( Actor::getActors().size() > 0 )
   delete (*Actor::getActors().begin());

  for( int i = cameras.size()-1; i >= 0; i-- )
   delete cameras[i];

Are you sure you want to delete the result of .begin() ? Once again I don't know how you're code works but this looks like an infinite loop to me.


for( int i = cameras.size()-1; i >= 0; i-- )
   delete cameras[i];


while( cameras.size() > 0 ) {
   cam = cameras.pop_back();
   delete cam;
}

you might want to leave your camera delete in to demonstrate that you know how to do a counting downwards for loop though

Otherwise the general quality of the code is better than what I had for my code samples when I started.

Edited by Kyall, 09 October 2012 - 05:20 PM.

I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

#3 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 10 October 2012 - 03:30 AM

I appreciate the feedback. I've made some adjustments, per your advice. In particular, the loops have been updated with reduced function overhead, and comments have been added to unclear sections. Just to be sure, I've re-reviewed and tested the loops during memory cleanup as well, just to verify that they do work as intended.

#4 frob   Moderators   -  Reputation: 18917

Like
2Likes
Like

Posted 10 October 2012 - 09:54 AM

I hate to be a spoilsport on this sort of thing, but I feel like posting your code for review like that is a bit dishonest.

The whole point of a code sample is to see how you really code.

Instead you are presenting a code sample of how your peer-review writes code.


Asking for a code review so you can learn and benefit in your coding style is one thing. Asking for a code review for an employment opportunity is quite another.
Check out my personal indie blog at bryanwagstaff.com.

#5 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 10 October 2012 - 11:44 AM

That is a good point. I hadn't thought about it like that.

I'll send it out as is, and see about a code review some other time.

#6 Kyall   Members   -  Reputation: 287

Like
0Likes
Like

Posted 10 October 2012 - 07:08 PM

Isn't code peer-reviewed when you work:
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS