Romhacking.net

Romhacking => Programming => Topic started by: InfamousKnight on April 09, 2013, 10:02:50 pm

Title: Writing clean code tips?
Post by: InfamousKnight 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.
Title: Re: Writing clean code tips?
Post by: furrykef 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"?
Title: Re: Writing clean code tips?
Post by: henke37 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.
Title: Re: Writing clean code tips?
Post by: InfamousKnight 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.
Title: Re: Writing clean code tips?
Post by: BRPXQZME 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 (http://stackoverflow.com/a/7256008)). 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.
Title: Re: Writing clean code tips?
Post by: Bregalad 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.
Title: Re: Writing clean code tips?
Post by: syntax error on April 11, 2013, 07:50:24 am
cppcheck or the analyzer from LLVM could help you finding problems in your source code.
Title: Re: Writing clean code tips?
Post by: henke37 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.
Title: Re: Writing clean code tips?
Post by: InfamousKnight 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.
Title: Re: Writing clean code tips?
Post by: KC 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.
Title: Re: Writing clean code tips?
Post by: InfamousKnight on April 11, 2013, 07:15:57 pm
Delete this. It was somewhat vulgar before.
Title: Re: Writing clean code tips?
Post by: ETG on April 11, 2013, 10:07:39 pm
Here's a bunch of links you may be interested in reading:
Id coding conventions (http://www.geeks3d.com/20081111/id-software-c-coding-conventions/).
Source code to a bunch of Id games (https://github.com/id-Software).
The Flash Game Dojo (http://flashgamedojo.com/).
An excellent C++ book (http://www.amazon.com/Beginning-C-Through-Game-Programming/dp/1435457420).
A game development wiki (http://www.gpwiki.org/).
List of engines, so you don't have to make them yourself (http://www.pixelprospector.com/the-big-list-of-game-making-tools/).
A can of worms (http://en.wikipedia.org/wiki/Software_design_pattern).
Title: Re: Writing clean code tips?
Post by: InfamousKnight 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.
Title: Re: Writing clean code tips?
Post by: Pikachumanson on April 12, 2013, 03:53:40 pm
Just modify your quotes and delete them yourself.
Title: Re: Writing clean code tips?
Post by: InfamousKnight 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.
Title: Re: Writing clean code tips?
Post by: Rhys 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.
Title: Re: Writing clean code tips?
Post by: RedComet 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. ;)
Title: Re: Writing clean code tips?
Post by: furrykef 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.)
Title: Re: Writing clean code tips?
Post by: henke37 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.
Title: Re: Writing clean code tips?
Post by: Klarth 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.
Title: Re: Writing clean code tips?
Post by: BRPXQZME on April 13, 2013, 02:24:23 am
...
GUIs aren’t graphics, though. Graphics is not so bad. Interactive graphics, still not so bad. GUI code, on the other hand, is the work of madmen.

The point is more about game state and thinking in terms of frames per second. The sooner you get out of console turn-based mode, the sooner you understand the relationships between game state and time, which is a good thing for learning to program games better, which I understand to be IK’s goal here? It doesn’t matter so much if the game is turn based.

(inb4 curses and aalib... yes, I know curses, that’s how I pulled off my “pong on a Cray” stunt in high school. It is sufficiently close to graphics/GUI to not be part of what I mean by “console based” in the above paragraph)

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.
For years I would prefer printf to cout, even in C++, because it would add a nasty number of kilobytes to the executables, a good way to make sure beginner code takes longer to load off the disk than it takes to run ;)
Title: Re: Writing clean code tips?
Post by: InfamousKnight on April 13, 2013, 11:57:25 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.

I have done that in the past. My first little turn based RPG was really basic. You had a one on one battle and the fights were really boring so I moved on to something harder. And that was this turn based system that allowed as many people on the field you like. Now that card game I created some time ago I got the idea of using multiple struct arrays for teams which really made me feel good about programming. I love it how I have my storage for my enemies. I should make an allies struct too and just have that players struct as the field.

Just to let you know, I abandoned the triple triad project. I made it to the logic part and was having a hard time. I could have read that DS source code and found the logic but I had better things to do. And writing multiple source codes, I would have to read up on that. I've seen people do it. And they make some decent games. And about the GUIs, they're just tedious. Not really hard.

This should be a big discussion for those users who are in the same state as me.

I have a question about the rules, it says on it that you can't do necromancy on topics. Could the OP resurrect the topic?
Title: Re: Writing clean code tips?
Post by: KC on April 13, 2013, 02:58:14 pm
The error you keep making is that you overestimate your skill and the complexity of the code you wrote so far. And as long as you don't change that attitude, this discussion will get nowhere as you refuse to accept the feedback you get.
Any decent programmer could write the programs you showed here in less than an hour, and certainly better. That may sound harsh to you, but if you can't accept that, then you don't need to ask for feedback and are just wasting everyone's time. We already had this in each of your threads.
Title: Re: Writing clean code tips?
Post by: InfamousKnight on April 13, 2013, 04:03:30 pm
>.> I understand that.
Title: Re: Writing clean code tips?
Post by: KaioShin on April 13, 2013, 04:21:57 pm
>.> I understand that.

Doesn't seem that way when there is a new topic by you every week with the same issues and the same mistakes and the same poorly thought out opinions.
Title: Re: Writing clean code tips?
Post by: InfamousKnight on April 13, 2013, 04:56:09 pm
Doesn't seem that way when there is a new topic by you every week with the same issues and the same mistakes and the same poorly thought out opinions.

That game is just an example of poor code. And the codes somewhat dirty. I just felt really accomplished when I first made it.