- Joined
- Mar 17, 2007
- Messages
- 9,305
It looks like we don't have a thread on coding standards yet. While it's probably natural that the evolve, and I've hesitated to bring it up (since what if the standard we decide on is the opposite of what I'd prefer?), overall it's probably better to have a few than to have none whatsoever.
I'd like to think of this is a "propose something, we discuss/vote on it" type of thing. Where a vote could be a vote for option A, option B, option C (if available), or not setting a standard. A standard would only be set if one of the options receive a majority of votes (or a plurality, if someone says, "I vote for B, but switch to A if that makes it a majority").
I'll start with a couple proposals:
-----------
Variable type specifications:
Option A: We specify variable types:
Option B: We do not specify variable types:
My vote: Option A.
Reason: The author always knows what type something will return (or should); specifying the type is easy up-front. Down the line, it improves readability. In the middle line above, it's easy enough, but in the first and third lines, you don't necessarily know that TheAnswerToLife always returns 42, or that the calculateMapDifficulty method returns an object instead of an number, when types aren't specified. Specifying the types saves a future reader who isn't familiar with those methods' return values from having to look them up, or having to try to figure it out from the code that comes next.
-----------
Multiple Return Values:
Option A: Allow Multiple Return Values
Option B: Don't Allow Multiple Return Values
My vote: Option B.
Reason: It can be difficult to reason about why there are two return values, at least if they aren't obviously and closely related. Returning multiple values also makes it much easier to not follow the single-responsibility principle.
If we do have multiple return values, the "why" should be preferably obvious, but if not, then clearly stated. Maybe there's a tremendous performance speedup due to using one method, for example, although I'd probably ask if memoization might be a better idea in that case.
An example where I would be okay with multiple return values is a method that returns an X and a Y coordinate, if there is an appropriate reason to need that to be returned versus, for example, a Tile object.
-------
We also probably ought to decide on PascalCasing versus camelCasing versus snake_casing, and when to use each; we've been tremendously inconsistent there, although I haven't felt a lot of pain from it. I'll let someone else write a proposal for that when they are feeling annoyed by it.[/CODE]
I'd like to think of this is a "propose something, we discuss/vote on it" type of thing. Where a vote could be a vote for option A, option B, option C (if available), or not setting a standard. A standard would only be set if one of the options receive a majority of votes (or a plurality, if someone says, "I vote for B, but switch to A if that makes it a majority").
I'll start with a couple proposals:
-----------
Variable type specifications:
Option A: We specify variable types:
Code:
int theAnswer = FindTheAnswerToLife(someParameter);
Civ3Map baseTerrainMap = new Civ3Map(map.numTilesWide, map.numTilesTall, map.RelativeModPath);
DifficultyObject relativeDifficulty = calculateMapDifficulty(baseTerrainMap);
Option B: We do not specify variable types:
Code:
var theAnswer = FindTheAnswerToLife(someParameter);
var baseTerrainMap = new Civ3Map(map.numTilesWide, map.numTilesTall, map.RelativeModPath);
var relativeDifficulty = calculateMapDifficulty(baseTerrainMap);
My vote: Option A.
Reason: The author always knows what type something will return (or should); specifying the type is easy up-front. Down the line, it improves readability. In the middle line above, it's easy enough, but in the first and third lines, you don't necessarily know that TheAnswerToLife always returns 42, or that the calculateMapDifficulty method returns an object instead of an number, when types aren't specified. Specifying the types saves a future reader who isn't familiar with those methods' return values from having to look them up, or having to try to figure it out from the code that comes next.
-----------
Multiple Return Values:
Option A: Allow Multiple Return Values
Code:
public (ReturnType1 firstReturnValue, ReturnType2 secondReturnValue) calculateSomeValues(int param1, string param2) {
//Do some calculations
return (firstReturnValue, secondReturnValue);
(returnValue1, returnValue2) = calculateSomeValues(14, "randomString");
Option B: Don't Allow Multiple Return Values
Code:
public ReturnType1 calculateOnlyTheFirstValue(int param1, string param2) {
//Do some calculations
return firstReturnValue;
}
public ReturnType2 calculateOnlyTheSecondValue(int param1, string param2) {
//Do some calculations
return secondReturnValue;
}
returnValue1 = calculateOnlyTheFirstValue(14, "randomString");
returnValue2 = calculateOnlyTheSecondValue(14, "randomString");
My vote: Option B.
Reason: It can be difficult to reason about why there are two return values, at least if they aren't obviously and closely related. Returning multiple values also makes it much easier to not follow the single-responsibility principle.
If we do have multiple return values, the "why" should be preferably obvious, but if not, then clearly stated. Maybe there's a tremendous performance speedup due to using one method, for example, although I'd probably ask if memoization might be a better idea in that case.
An example where I would be okay with multiple return values is a method that returns an X and a Y coordinate, if there is an appropriate reason to need that to be returned versus, for example, a Tile object.
-------
We also probably ought to decide on PascalCasing versus camelCasing versus snake_casing, and when to use each; we've been tremendously inconsistent there, although I haven't felt a lot of pain from it. I'll let someone else write a proposal for that when they are feeling annoyed by it.[/CODE]