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

Author Topic: Writing Good Code  (Read 9841 times)

0 Members and 1 Guest are viewing this topic.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Writing Good Code
« on: July 16, 2013, 08:07:51 pm »
Hello everybody!

I'm relatively new with C++ (I have a basic understanding and so far I really like it) and I'm pretty proficient with Java.  Learning Java really helped learning C++ but the structures within cpp files and java classes is TOTALLY different.  I've just been following CodingMadeEasy's tutorials and have my code has just gotten bigger and bigger.  However, it's really ugly and sometimes hard to follow, so I split everything into different functions and I call those functions in the constructor.  Then I call the constructor in the main method.  Here's my code.

https://github.com/ChaoticCactus/SFMLProject

You can ignore the first 2 files. 

I'm just curious how I can improve my coding.

Thanks!


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Writing Good Code
« Reply #1 on: July 16, 2013, 09:30:40 pm »
Youtube tutorials are famous for teaching very bad code style, so that is not suprising ;)

The only way to learn C++ and good coding pratices is reading books. You should start with a beginner book (e.g. C++ Primer) that explains all the language and standard library feature. Even if you don't consider yourself a beginner, there are a lot of subtle differences between Java and C++ that you should know, and only good books cover those in-depth. Tutorials omit many important details, and of video tutorials I don't even want to begin to speak.

After that, you should read an advanced book that teaches modern idioms like RAII and techniques like exception safety or efficient use of STL. The classical book for this was Effective C++, but some of the rules taught by it are not up-to-date anymore, but in general you can probably still learn a lot.

It may sound demotivating, but reading such books still takes far less time than the maintenance and debug sessions arising from bad code. Furthermore, it is not nearly as frustrating. You should really invest some time, C++ is a very complex language.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Writing Good Code
« Reply #2 on: July 16, 2013, 09:53:35 pm »
Ok, thanks!   I'll definitely check that book out.  Anything for making programming less frustrating.  It's an addicting puzzle but sometimes it can be so frustrating. :)

Just as a side note, how does the code in the github link look?

EDIT: Forgot to mention.  I've heard of a lot of people that learned C++ through TheNewBoston.  Would those tutorials be a decent substitute?  Or should I just stick with the book?
« Last Edit: July 16, 2013, 09:55:31 pm by Clockwork »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Writing Good Code
« Reply #3 on: July 16, 2013, 10:05:56 pm »
EDIT: Forgot to mention.  I've heard of a lot of people that learned C++ through TheNewBoston.  Would those tutorials be a decent substitute?  Or should I just stick with the book?
Although I like TheNewBoston as Nexus said, you can't really learn C++ from a few Youtube videos. Go grab yourself a book (I mean many libraries should have them if you don't want to buy it) and start reading! :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Writing Good Code
« Reply #4 on: July 16, 2013, 10:15:16 pm »
Ok, I'll definitely get the book. 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Writing Good Code
« Reply #5 on: July 16, 2013, 10:20:00 pm »
Which one? There is a whole list of possibilities :)

The C++ Primer is often recommended (as opposed to C++ Primer Plus).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Writing Good Code
« Reply #6 on: July 16, 2013, 11:17:52 pm »
Woops, I meant I'll definitely get C++ Primer, I read through the "Look Inside!" section on amazon, and it seems pretty informative.

cloudncali

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://jakesmakes.tumblr.com/
Re: Writing Good Code
« Reply #7 on: July 17, 2013, 12:57:26 am »
Well I remember when I first started learning C++, I was very messy with my code. But these are some things I've learned that help me write better code.

Choose a coding standard and naming convention and stick to it. I know this sounds like a minor thing, but it really does lead to more readable code. When I work on GQE(https://code.google.com/p/gqe/) I tend to stick with Pascal Case Notation(http://en.wikipedia.org/wiki/CamelCase). It really doesn't matter what naming convention you use. As long as its readable and constant. This is why I cant even use some of my older projects, I can even figure out my old code. Take a look at at the coding standards for GQE: https://code.google.com/p/gqe/wiki/CodingStandards. Some things you probably wont have to worry about, like documentation. But its nice to have consistency in your code.

Look around other open source projects. Its good to see how other people do things. Sometimes you might even get the chance to help with a project. That's how I became a contributor to GQE.

Finally one thing i would always do is. If you program any thing Late at night, While Tired, out of it, Etc. Look it over the next day. You might not even remember what it does :P. Always good to review your own code (Especially sleepy-coding)

Just my two cents.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: Writing Good Code
« Reply #8 on: July 17, 2013, 01:09:43 am »
You could as well try to grab the source code of the SFML book, the code style there is really clean, modern.
Don't even need to buy the book in order to grab the source :)

That would give you a full example of a project using SFML to look at.
Surprised Nexus didn't bring it up.
« Last Edit: July 17, 2013, 01:12:10 am by Groogy »
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: Writing Good Code
« Reply #9 on: July 17, 2013, 01:41:00 am »
Here's my code.

https://github.com/ChaoticCactus/SFMLProject

Overall, IMHO, it doesn't look too bad. Of course, it's also really simple. As the complexity of your code increases, you may want to encapsulate stuff: For instance, maybe put your player in 1 class. Then you'll probably discover that's not enough, and want to separate the visual aspects of that class from the logic and from the input, and put each of those in separate classes. And so forth...
I think it's ok to work iteratively and just do what works first, slowly abstracting stuff as you deem it necessary.

Here is an example of what not to do. Mind you, that (commercial) game still shipped.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Writing Good Code
« Reply #10 on: July 17, 2013, 02:20:12 am »
Here is an example of what not to do. Mind you, that (commercial) game still shipped.

How did you find that...  ??? But seriously that is insane  :o  10047 lines of code all in one single file  ::)
Reminds me of some of the ancient code I have gone through at work (old VB6 stuff) :P
« Last Edit: July 17, 2013, 02:24:36 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Writing Good Code
« Reply #11 on: July 17, 2013, 02:25:12 am »
Old software tends to be messy on the lots-of-code-that-does-ritchie-knows-what-but-works-super-wonderfully side. Duke Nukem 3D has raw calls to os, globals, assuming many things about platform, mad code full of tricks, arcane assembly and was meant for pcs without fpu so it doesn't ever use floats. ;D
http://fabiensanglard.net/duke3d/code_legacy.php
I've seen people on the internet say that if everything is in one file then compilers long ago were (supposedly) optimizing it much better and back then performance was everything.
« Last Edit: July 17, 2013, 02:26:52 am by FRex »
Back to C++ gamedev with SFML in May 2023

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Writing Good Code
« Reply #12 on: July 17, 2013, 02:31:45 am »
How did you find that...  ??? But seriously that is insane  :o  10047 lines of code all in one single file  ::)
Reminds me of some of the ancient code I have gone through at work (old VB6 stuff) :P

That code may be old, but this kind of practice happens all the time, even in modern day AAA titles.
This kind of thing tends to happen when you have a company try and milk a franchise as much as possible, so they just tack on new code to an old code base and replace the textures. It's fun times sifting through the mess afterwards.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Writing Good Code
« Reply #13 on: July 17, 2013, 08:25:04 pm »

Here is an example of what not to do. Mind you, that (commercial) game still shipped.

Oh god.  I can't even look at that. I opened up the link and cringed. :-\  It's hard to believe that that actually shipped...

And I'm working on putting everything into different classes right now actually, it's much easier to look at. :P

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Writing Good Code
« Reply #14 on: July 17, 2013, 08:33:32 pm »
Holy crap, even I don't write code that badly and I'm still a noob, as others have said Primer C++ is definitely a good option.

Quote
That code may be old, but this kind of practice happens all the time, even in modern day AAA titles.
This kind of thing tends to happen when you have a company try and milk a franchise as much as possible, so they just tack on new code to an old code base and replace the textures. It's fun times sifting through the mess afterwards.

I was wondering about this but is this why in the code that the book provides they've made seperate headers/files for all the main functions? I was wondering why that was, it does look a lot neater but I still think it's a bit disorganised somehow but I guess that's because I don't know the full thing yet.
« Last Edit: July 17, 2013, 08:46:52 pm by Lethn »

 

anything