Question about style

Started by
56 comments, last by JasonHise 19 years, 8 months ago
Quote:Original post by Bbalstrzmj90
thanks guys. I think I'll develop my own style as long as theres nothing wrong with it. And for now I'll still "use" using namespace std. And I'll also start working on the way to declare classes.


AFAICS, there is nothing wrong with a "using namespace std". I have read (in Scott Meyers' excellent "Effective C++" and "More Effective C++") that including using directives in your header files effectively defeat the purpose of the namespace, because the directive exposes the entire contents of the namespace anywhere the header is included. My guess is that this is what this meme refers to. Putting a "using" directive in appropriate .cpp files keeps the encapsulation of the namespace intact, but simplifies your work where needed.

=-=-=-=

I personally have developed software with and without hungarian notation. These days I don't recommend it, and I prefer to work without it.

Code style is important to me for these reasons:

-- Being able to understand what you wrote now
-- Being able to understand what somebody else wrote now
-- Being able to understand what you wrote a long time ago
-- Being able to understand what somebody else wrote a long time ago

Having worked on teams of various sizes, with various degrees of coding standards, I recommend that you find some styles that:

-- make your work easier
-- you can remember easily
-- you and whoever else you work with can agree on

Good luck!
Advertisement
As far as the using thing goes... for one, "using namespace std" isn't necessarily bad. Assuming you're not using it in a header file, and your classes aren't horrible monolithic beasts including 15 namespaces, you should be fine. And even if they are such beasts, you can always use "using std::cout".

I generally prefer the latter, it provides a nice little synopsis of a class's dependencies at the beginning of the source file.
krolco and nuvem just said it, but I'll emphasize. The original poster misunderstood advice about the "using" directive. The common advice is to never use it in a header.

Hungarian notation is a crutch. This doesn't necessarily mean it's bad to use it, but I've seen people use it excessively and it's just plain annoying. Why do you need to prefix classes with C and structs with S... they're the same thing anyway. What are you going to mistake them for... functions? unions? It's all pretty ridiculous.

Anyway the best advice you can get on style is to be flexible. You'll find that when you are a professional programmer, you need to adapt your coding to the particular style mandated by your employer. If you think that you are above using someone else's coding standard, you are a disposable employee. Period.
I do programming for 13 years as hobby and i found that everything exept some trivial code&forgeth programs should
written with :
-proper indentation
-comments
-describing names :)

Yes it would take longer to write each function like that but you would be gratefull for that even after 2 h and 100 lines of code later.
Few things about my naming convention :
Normally all my names are all lowercase (slices , bolts etc)
Names of constants are beginning with uppercase letter like
Arrow ;
CamelCase for complex names like mapThrowingCounter ; netfinityDriverLoader etc.
Pascal capitalization for const long names like ThingsGotBetter,
OneBigOcean etc.
And all uppercase for identifiers irr.planet.STR

Regards
Blade
How do you guys call your for loop variables? ( iFrames )

for( int iFrames = 0; iFrames <= frames; iFrames++ )
Just one comment, Konfusius:

Quote:
-private class variables have underscore _before them


The standard states IIRC, that underscore prefixed names are allowed to be reserved by the compiler, so using variables that start with underscores is not advisable. Ending with an underscore is the recommended replacement. (Pretty sure my source on this is CUJ)
Oh.. and how would you guys comment and write this code:

// [([(35 + BaseLevel*HPMultiplier + SigmaOfBaseLevel*HPFactor)*// (1 + VIT/100)]+HPAdditions)*ItemHPMultipliers]   max_life = static_cast<int>             (              (    (                      ( 35 + level*5 + sigmaOfBaseLevel*0.55 )                     * (1 + vit/100)                    ) + extra_hp              ) * itemHPMultipliers             );

Quote:Original post by cozman
Just one comment, Konfusius:

Quote:
-private class variables have underscore _before them


The standard states IIRC, that underscore prefixed names are allowed to be reserved by the compiler, so using variables that start with underscores is not advisable. Ending with an underscore is the recommended replacement. (Pretty sure my source on this is CUJ)
Herb Sutter writes about it in either exceptional c++ or more exceptional c++. I don't understand why people still do it :)
Quote:
Oh.. and how would you guys comment and write this code:

// [([(35 + BaseLevel*HPMultiplier + SigmaOfBaseLevel*HPFactor)*
// (1 + VIT/100)]+HPAdditions)*ItemHPMultipliers]
max_life = static_cast<int>
(
( (
( 35 + level*5 + sigmaOfBaseLevel*0.55 )
* (1 + vit/100)
) + extra_hp
) * itemHPMultipliers
);


I'm going to need the formula for that equation in order to refactor it properly--which means write it out so that there are no numbers and with longer, more explanatory names.
Quote:Original post by rakoon2
How do you guys call your for loop variables? ( iFrames )
for( int iFrames = 0; iFrames <= frames; iFrames++ )


for (int i = 0; i < frames; ++i)

Or, to make things slightly clearer:

for (int frame = 0; frame < numOfFrames; ++frame)

But, if the word "frame" is used a lot in that bit of code, then, to clear things up even further:

for (int frameIndex = 0; frameIndex < numOfFrames; ++frameIndex)

Then again, if it's not being used in an array, but more as a counter, then something like:

for (int frameCount = 0; frameCount < numOfFrames; ++frameCount)


Quote:Original post by rakoon2
Oh.. and how would you guys comment and write this code:


Without knowing what exactly it is you're trying to do, here goes:

// The base hitpoint value for all players.const int BASE_HITPOINTS = 35;// The increase in hitpoints based on the user's level.const int HIT_POINT_MULTIPLIER = 5;// Some weird sigma calculation factor to further increase the // player's hitpoints based on level.const float HIT_POINT_FACTOR = 0.55f;// The maximum hit points the player of this level may have // (without any additions/powerups).  This is simply the base// hitpoint value, plus any hitpoints gained through levelling // up.float maxHitpoints = BASE_HITPOINTS + (level*HIT_POINT_MULTIPLIER) + (sigmaOfBaseLevel*HIT_POINT_FACTOR);// The more vitality the player has, the more life he has.  No // vitality does not affect the player negatively.  100% vitality // will double the players life.float vitalityMultiplier = 1 + (vit/100);// Calculate the total hitpoints which is based on the max // hitpoints increased by vitality, plus any additional hit // points that may have been acquired.float totalHitPoints = (maxHitpoints * vitalityMuliplier) + extraHitpoints; // The maximum life power of the player is the number of // total hitpoints he has mutiplied by any "hitpoint affecting" // items.int maxLife = static_cast<int>(totalHitPoints * itemHPMultipliers);


I would also probably break a couple of those calculations up into functions because, chances are, you probably use them a lot.

Like, when trying to calculate the player's hitpoints (ie. BASE_HITPOINTS + (level*HIT_POINT_MULTIPLIER) + (sigmaOfBaseLevel*HIT_POINT_FACTOR))... Something like that is probably used in more than one location. So, making that a function would be nice.

Breaking it into functions would probably also make things a little cleaner. If they're that simple, you can even make them inline if you're really worried about speed. (Though, you should make sure it's necessary/helpful before you go around making everything inline.)

-HQ.

This topic is closed to new replies.

Advertisement