News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Writing clean code tips?  (Read 10734 times)

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Writing clean code tips?
« on: April 09, 2013, 10:02:50 pm »
I'm not very good at writing clean code but I'm getting better. So this code seems to have some flaws: http://pastebin.com/zBT6KAHf

So people tell me not to use == when comparing strings. Whats so wrong with that? Is there some clean way of doing it? Also, someone told me to use enums. And I get kinda annoyed when writing those for loops. So is there a more efficient way of doing it? I also need to start organizing my code. Got any tips for that? I was thinking about making a shop function but I done that some other time and was getting error msgs saying not declared because my code was so messy. And its really easy to write messy code but its not so easy when it comes to expanding it. I only have one ability for everyone because that code is a remake.  And I would like to know some tips for the enemies targeting the players. Because I would have to make all the enemies have the same amount of abilities because of that etarget var.

You don't have to clean it for me unless you want to. Just give me some tips.

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Writing clean code tips?
« Reply #1 on: April 09, 2013, 10:24:23 pm »
So people tell me not to use == when comparing strings. Whats so wrong with that?
If you're using std::strings, nothing. If you're comparing char pointers or char arrays, though, it will compare the addresses of the strings, which is not what you want. Your code is using std::strings, though, so no problem there.

BTW, why does your code include "stdlib.h" and not "cstdlib"?

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Writing clean code tips?
« Reply #2 on: April 10, 2013, 12:41:25 am »
Use more functions. And seriously, move on to some graphical UI system. You can't ignore the problem of realtime simulation forever.

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Writing clean code tips?
« Reply #3 on: April 10, 2013, 09:01:07 am »
Use more functions. And seriously, move on to some graphical UI system. You can't ignore the problem of realtime simulation forever.

I was thinking of doing that but then I'm not an artist. Could I get images of videogamesprites.net? Like is it illegal? I would hate to publish a game that has content from other games.

BRPXQZME

  • Hero Member
  • *****
  • Posts: 4572
  • じー
    • View Profile
    • The BRPXQZME Network
Re: Writing clean code tips?
« Reply #4 on: April 11, 2013, 01:11:23 am »
So people tell me not to use == when comparing strings. Whats so wrong with that? Is there some clean way of doing it?
Depends on what you’re doing. The main issue with comparing strings is that more often than not, that’s not actually what you really want to do. Traditionally, people were against it because it would take a long time to do, but nowadays that doesn’t matter quite as much. Traditionally, people were also against it because you would introduce code security problems very easily, but that isn’t so much of a problem if you are using something that manages strings for you.

Nowadays, the main problem is that you might compare strings when what you really want to do is tell that, for example, you are checking a game unit’s identity, and it’s better (as well as faster) to use an ID number for that sort of thing, because what if you change the string that identifies the unit? Or what if the unit has the same identifier as another? Or what if you typo something, which means the error can’t be caught at compile time? It’s way easier to design things so that the ID number will never change, and will always distinguish things you want to be different.

(This isn’t necessarily something you learn in a typical programming course, but it’s an important part of database design, something I think not enough programmers study. A class in database design can be pretty expensive, though.)

So just think about what you are really doing before you ever compare strings. On modern computers, as a coding shortcut, it doesn’t actually hurt as much as experienced people might lead you to believe. What you don’t want to do, however, is put too much load on the system, especially in a game where you could be doing the same calculations many times a second. Strings can kill you there.

Also, someone told me to use enums.
Probably should. Use enums any time you use a hard-coded number to mean one of a limited number of distinct choices (red, green, blue parts of a color; X, Y, Z in a 3D coordinate; archer, mage, knight as a unit type...). You never want to go hunting around for hardcoded numbers.

And I get kinda annoyed when writing those for loops. So is there a more efficient way of doing it?
In C++? In terms of time it takes you to write them out?

Noooot exactly. It’s one of the first things I’d love to see changed in C or C++. Some ways to do this are in C++ already, such as std::for_each or “range-based for loops” (a C++11 feature). Unfortunately, these on their own will not be sufficient (without writing a class) for one of the single most unnecessarily repetitive tasks, which is to count through an integer range. The Boost library does provide boost::irange, which can be used in conjunction with C++11 range-based for loops to function this way (example). This isn’t a thing people normally use, but I think this will change eventually.

You have perhaps accidentally stumbled onto why “functional programming” has been all the rage lately, and why these features have been added to C++. Again, it’s all about meaning. In many for loops, you don’t necessarily care that i is first 0, then 1, then 2 and so forth. Perhaps you just want the same thing done to each part of a set of data. Perhaps, if you are working with a very large set of data, you want it to be split up into multiple sets and sent off to different processors so that the work gets done faster. C++ is not quite there when it comes to supporting this at the language level, but it is possible this might change eventually.

I also need to start organizing my code. Got any tips for that?
It’s hard, but worth it. When you learn a language, you also learn about its design. Thus, you learn how to split things apart when they get too big. Unfortunately, a sufficiently thorough study of this is could take multiple college-level courses to learn. Designing programs on a higher level is a pretty hard thing; that’s what they pay software engineers for.

So for now, continue to focus on things like maintaining consistent spacing and making sure all the brackets and semicolons are in the right place, and making sure there is no = where you mean ==. If you never forget these things, that’s one step to “cleanliness” (a bit of a subjective term, that).

Splitting things off into functions if they need to be found in more than one place, or if reading the name is all someone who reads a section of code really needs to know about that section.

You do not use classes here despite using C++, which is not necessarily a bad thing in my opinion (I’m a little bit of an OOP skeptic, actually), but consider learning how to use them. If you have a structure to data and you have functions that only really concern that structure, that’s the kind of thing you should make a class.

Also, learn how to split code into multiple files. As a rule of thumb, you should do this as a way of hiding code that doesn’t matter a whole lot to what you’re coding right now.

And I would like to know some tips for the enemies targeting the players. Because I would have to make all the enemies have the same amount of abilities because of that etarget var.
This, too, is a matter of designing your data, and it is a difficult thing to understand the overall principles of that.

You could, for instance, maintain (or generate) two lists of allies and enemies instead of (or as well as) treating that as a status someone has. Then you can go through those lists if you need a target. You might even sort those lists by some attribute if you only want to target players with certain statuses. It’s all up to how you want to organize it.
we are in a horrible and deadly danger

Bregalad

  • Hero Member
  • *****
  • Posts: 2644
    • View Profile
Re: Writing clean code tips?
« Reply #5 on: April 11, 2013, 05:41:21 am »
I'm not a C++ expert, but C++ is one of the few languages that allows opperators, such as ==, +, << and so on, to be overloaded. This means they can be a hidden function call to something that does very complex that is completely transparent in your code.

This can be both a good thing and a bad thing. For example if you're dealing with vectors, it's very convenient to overload the + and * operators to do vectorial opperations. However, when you write code you don't always remember the symbol actually calls a function, which can leads to problems.

I think the << surcharge for cout is dumb as this has no relationship with the original intent of << (left shifting). But I can't blame people that use this sine it appears to be a standard convention in C++ to call the overloaded << operator on strings instead to call a printf function.

Back on topic, you should either use enmus or macros to define constants, but don't ever use hardcoded numbers (unless you are 100% sure you'll never forget them and that you will never change them again, but I can be pretty sure this will NEVER be the case !)

Quote
And I get kinda annoyed when writing those for loops. So is there a more efficient way of doing it?
If you hate loops so much you should LOVE functional programming... but myself I prefer procedural programming. To each their own I guess.
You could simulate functional programming in C++ by forbid yourself to use variables and loops, but it would be extremely inefficient, instead you should use a language which is meant to be functional, so that the compiler automatically changes recursive calls into loops. Or at least I belive so, I might be wrong, I'm no expert.

syntax error

  • Full Member
  • ***
  • Posts: 227
    • View Profile
Re: Writing clean code tips?
« Reply #6 on: April 11, 2013, 07:50:24 am »
cppcheck or the analyzer from LLVM could help you finding problems in your source code.

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Writing clean code tips?
« Reply #7 on: April 11, 2013, 01:44:28 pm »
Use enums when it is a set of known values. Use constants/macros/whatever when it really is numerical data where the actual number matters.

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Writing clean code tips?
« Reply #8 on: April 11, 2013, 03:24:02 pm »
Functional programming such as haskell? Or lisp? Or those impractical languages? I would like to move on to another language. I'm not a fan of OOP. Yeah, c++, I hate saying c++ to my friends because it sounds weird in my opinion. I also might learn C as its not an OOP language. The problem is text linking. Notice in my code I'm text linking a lot.

KC

  • Full Member
  • ***
  • Posts: 209
    • View Profile
Re: Writing clean code tips?
« Reply #9 on: April 11, 2013, 05:10:00 pm »
What you write could hardly be less object oriented. Streams and variable declaration aside it's pretty much simple C code. You should change that, OOP is very powerful and even that extremely basic game of yours would benefit from it.
Also, do NOT move to anything graphical. Learn how to structure your code before you move on to something bigger. Split it up into different units and abstract everything.

You shouldn't use strings for checking types. Use enums for that. Put comments in your code. Avoid redundancy. Add whitelines to improve readability. Don't put everything in huge nested switch statements. Think of using a statemachine. Don't hard code all amounts, use a container and iterate through its content.
Just generally read some beginner literature that will teach these things.

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Writing clean code tips?
« Reply #10 on: April 11, 2013, 07:15:57 pm »
Delete this. It was somewhat vulgar before.
« Last Edit: April 11, 2013, 07:56:01 pm by InfamousKnight »

ETG

  • Jr. Member
  • **
  • Posts: 49
    • View Profile
« Last Edit: April 12, 2013, 10:53:22 pm by ETG »

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Writing clean code tips?
« Reply #12 on: April 12, 2013, 03:16:03 pm »
>.> I regret saying that and thats why I requested it to be deleted. Somebody should delete all three of these msgs.

Pikachumanson

  • Hero Member
  • *****
  • Posts: 607
    • View Profile
Re: Writing clean code tips?
« Reply #13 on: April 12, 2013, 03:53:40 pm »
Just modify your quotes and delete them yourself.

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Writing clean code tips?
« Reply #14 on: April 12, 2013, 05:34:30 pm »
Just modify your quotes and delete them yourself.

I can't delete it. The forum doesn't have that feature.

Rhys

  • Hero Member
  • *****
  • Posts: 706
    • View Profile
    • CN
Re: Writing clean code tips?
« Reply #15 on: April 12, 2013, 05:55:28 pm »
There's still room in the world for programmers who don't know OO, mainly in the areas of very low level programming - embedded devices, etc. But OO's an extremely important thing to learn if you want to expand outside of these areas.

Most applications out there these days use OO in at least some form, so I think it would be worth your while to learn about it properly regardless of if you like it or not. I hated C++ a year ago, now it's one of my favourite languages exactly because I stopped hating it long enough to give it a shot.

RedComet

  • Hero Member
  • *****
  • Posts: 3166
    • View Profile
    • Twilight Translations
Re: Writing clean code tips?
« Reply #16 on: April 12, 2013, 08:36:09 pm »
Whether you're pro-OOP or not, it's still valuable to learn how to use it properly, since you never know when a problem could be solved through much less effort by using it. A carpenter who knows how to use a hammer and a wrench is better than a carpenter who only knows how to use a hammer. ;)
Twilight Translations - More than just Dragonball Z. :P

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Writing clean code tips?
« Reply #17 on: April 12, 2013, 10:24:35 pm »
And seriously, move on to some graphical UI system. You can't ignore the problem of realtime simulation forever.
IMO this is not good advice. It's much better to hammer out the kinks in one's programming techniques using a console window when possible. The same can also be applied to experimenting with a game design even if you're already a good programmer. GUIs are just a distraction.

I'm currently coding a board game and right now it all uses a console window. It's not because I don't know how to code a UI, either (hell, I have a commercial Nintendo DS game under my belt); it's just often quicker to get up and running that way. A lot of game designs, or pieces of them, can be implemented easily in a console window.

Of course, sometimes you really do need a GUI to present a decent interface for something. You might also be using a game engine that doesn't make GUIs significantly less convenient than a console window, especially if you already have temporary art assets you can use. But sometimes it's best to KISS and work it all out in the console first.

Quote from: Rhys
I hated C++ a year ago, now it's one of my favourite languages exactly because I stopped hating it long enough to give it a shot.
That's funny, the more I learn/work with C++, the more I utterly hate it. And I've been using it for just about a decade and a half now. (OOP is certainly not the reason why I hate it, though; I use OOP in Python all the time.)
« Last Edit: April 12, 2013, 10:30:55 pm by furrykef »

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Writing clean code tips?
« Reply #18 on: April 12, 2013, 10:38:05 pm »
I find that graphical stuff is rather easy, if you have an appropriate library backing you. That is the reason why I like Flash, a strong graphics system suited for non trivial designs, yet easy to work with.

Yes, it can be a bit of code to hook the graphics up to the internal game state, but you need the same hooks if you stick to the cli.

There is also the big step into realtime simulations. Few games are turn based, and even fewer can get away with blocking on the player input.

Klarth

  • Sr. Member
  • ****
  • Posts: 484
    • View Profile
Re: Writing clean code tips?
« Reply #19 on: April 13, 2013, 01:01:24 am »
I suggest for Infamous to start reading books and source code from others so he can gain a better sense of how to organize his code.  Also, start using multiple source files.