Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Looking for tips to improve my code  (Read 1291 times)

0 Members and 1 Guest are viewing this topic.

sensus12

  • Newbie
  • *
  • Posts: 6
    • View Profile
Looking for tips to improve my code
« on: September 22, 2014, 04:55:25 pm »
Hello. I am programming in SFML 2.0 for some time and i'd like you to give me some tips on improving my code overall. I made an Arkanoid clone.

Menu_State.h - main menu: http://pastebin.com/YVCrkiYg
Menu_State.cpp -  http://pastebin.com/x5rUZJU8

PlayGame_State.h - while playing: http://pastebin.com/ka7FGQ5y
PlayGame_State.cpp -  http://pastebin.com/vvRJ7Jpx

GameState.h - enum with game states: http://pastebin.com/bFEe8JXf

Forceofaqua

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Looking for tips to improve my code
« Reply #1 on: September 23, 2014, 01:20:59 am »
What I personally like to do is make comments above each block of code explaining what it does. This not only helps you and/or others later understand what is going on but forces you to fully understand what and how you are coding. You will catch many errors you put into code if you go back and explain the process in english. Of course this takes time and space, so you have to be willing to live with the small amount of clutter.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Looking for tips to improve my code
« Reply #2 on: September 23, 2014, 09:34:10 am »
Use consistent naming convention. And This_Style is very unusual.
Have a clear rule when to use reference and pointer parameters.
Use const for references that provide no access to modify the object (e.g. font parameter).
Avoid magic numbers (checkIfClicked()), use loops to avoid code duplication.
Indent consistently (if/else on same or different line).
Use spaces consistently, especially between operators.
You could use the sf::Drawable base class.

As you see, there are a lot of formatting-related issues, which makes the code quickly unreadable and harder to understand. Make sure these things are fixed before asking about semantics...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything