Normally when I see code posted here like yours, I edit it, because we hate horizontal scroll. But since that's part of your question, I'll show you the edit here:
int extractMessage(char keyWord[25], char cipherText[17424],
int rowSize, char message[388])
{
int keyColumn = 0;
int cipherColumn = 0;
int offset = 1;
int nextWord = 1;
int lengthOfWord = 0;
int lengthOfCipher = 0;
lengthOfWord = length(keyWord);
lengthOfCipher = length(cipherText);
while (keyWord[keyColumn] != cipherText[cipherColumn]) {
cipherColumn++;
if (keyWord[keyColumn + offset]
!= cipherText[cipherColumn + (rowSize*nextWord) + nextWord]) {
cipherColumn++;
continue;
}
}
}
That break may be surprising, but it's more readable than the version with horizontal scroll, and it's better than shortening the names to i
, j
, and k
.
It's not that you should never use i
, j
, and k
. Those are fine names when indexing 3 nested for
loops. But here the names are really my only clue about what you expected to be happening. Especially since this code doesn't actually do anything.
Best rule to follow on variable name length is scope. The longer a variable lives, the more fellow variables its name has to compete with. The name CandiedOrange is unique on stack exchange. If we were in a chat, you might just call me "Candy". But right now, you're in a scope where that name could be confused with Candide, Candy Chiu, or Candyfloss. So the longer the scope, the longer the name should be. The shorter the scope, the shorter the name can be.
Line length should never dictate name length. If you feel like it is then find a different way to lay out your code. We have many tools to help you do that.
One of the first things I look for is needless noise to get rid of. Unfortunately this example doesn't do anything, so it's all needless noise. I need something to work with so first let's make it do something.
int calcCipherColumn(char keyWord[25], char cipherText[17424],
int rowSize, char message[388])
{
int keyColumn = 0;
int cipherColumn = 0;
int offset = 1;
int nextWord = 1;
int lengthOfWord = 0;
int lengthOfCipher = 0;
lengthOfWord = length(keyWord);
lengthOfCipher = length(cipherText);
while (keyWord[keyColumn] != cipherText[cipherColumn]) {
cipherColumn++;
if (keyWord[keyColumn + offset]
!= cipherText[cipherColumn + (rowSize*nextWord) + nextWord]) {
cipherColumn++;
continue;
}
}
return cipherColumn;
}
There, now it does something.
Now that it does something, I can see what I can get rid of. This length stuff isn't even used. This continue
doesn't do anything either.
int calcCipherColumn(char keyWord[25], char cipherText[17424],
int rowSize, char message[388])
{
int keyColumn = 0;
int cipherColumn = 0;
int offset = 1;
int nextWord = 1;
while (keyWord[keyColumn] != cipherText[cipherColumn]) {
cipherColumn++;
if (keyWord[keyColumn + offset]
!= cipherText[cipherColumn + (rowSize*nextWord) + nextWord]) {
cipherColumn++;
}
}
return cipherColumn;
}
Let's make some minor white space tweaks, because we live in a world of source control and it's nice when the only reason a line gets reported as changed is because it's doing something different, not because part of it had to line up in a column.
int calcCipherColumn(char keyWord[25], char cipherText[17424],
int rowSize, char message[388])
{
int keyColumn = 0;
int cipherColumn = 0;
int offset = 1;
int nextWord = 1;
while (keyWord[keyColumn] != cipherText[cipherColumn]) {
cipherColumn++;
if (keyWord[keyColumn + offset]
!= cipherText[cipherColumn + (rowSize*nextWord) + nextWord]) {
cipherColumn++;
}
}
return cipherColumn;
}
Yeah, I know it's slightly less readable but otherwise you'll drive people crazy who use vdiff tools to detect changes.
Now let's fix these silly line breaks that we have because we're trying to stay under line length limits.
int calcCipherColumn(
char keyWord[25],
char cipherText[17424],
int rowSize,
char message[388]
) {
int keyColumn = 0;
int keyOffset = 1;
int nextWord = 1;
int cipherColumn = 0;
int cipherOffset = (rowSize * nextWord) + nextWord;
char key = keyWord[keyColumn];
char keyNext = keyWord[keyColumn + keyOffset];
while (key != cipherText[cipherColumn]) {
cipherColumn++;
if (keyNext != cipherText[cipherColumn + cipherOffset]) {
cipherColumn++;
}
}
return cipherColumn;
}
There, now the logic in the loop is focused on what changes in the loop. In fact, everything except cipherColumn
could be marked final
. And hey! Look at that. We now have room to do it.
All I did was add 3 more variables, rename one, and rearrange them a little. And the result just happened to make the lines short enough to fit without a silly linebreak on !=
.
Sure the names key
and keyNext
are not that descriptive, but they each only get used once, don't live that long, and most importantly aren't doing anything that interesting in the loop. So they don't need to be. By introducing extra variables we now have room to make their names long if we need to. Things change, so eventually we may need to. If we do, it's nice that we have breathing room.
I also took the liberty of showing you Jeff Grigg's form 6 variant style of laying out input parameters to respect line length restrictions.
cipherColumn + (rowSize*nextWord) + nextWord
that makes it clear what that calculation is for, for example? I bet that name's shorter than the calculation, so you get a readability benefit and a reduced line length. Also don't align assignments, or you have to move them all if you rename the longest variable.