As you can see by me posting in the beginner section,i am a beginner.
but for all the code that i write i have a feeling(and most often right)that the code that i have written is 1.ugly 2.can be written in a much shorter better manner.
now the 1st one i suppose will come with experience.
the second problem though,is with the way i implement things.Is this a problem with me not being smart enough to find optimal solutions for problems,or will it also come with experience?
is there a way i can teach myself to think so that i can find optimal solutions for things?
consider the following code,showing my implementation of the rail fence cipher.if you can't be bothered to read it I don't blame you.
it basically counts from zero>number then from number back to zero,cause that's sort of how rail fence works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
while(true){
for(int i = 0;i<key;i++){
char tc = rawText[encryptedLetterCounter];
if(isspace(tc)){
if(haveSpaces_==true){
encryptionMatrix[i][columnCounter]=tc;
encryptedLetterCounter++;
if(encryptedLetterCounter==t){
breakFlag=true;
break;
}
}else{
i--;
encryptedLetterCounter++;
if(encryptedLetterCounter==t){
breakFlag=true;
break;
}
}
continue;
}
encryptionMatrix[i][columnCounter]=tc;
encryptedLetterCounter++;
if(encryptedLetterCounter==t){
breakFlag=true;
break;
}
}
if(breakFlag==true){
break;
}
columnCounter++;
for(int i = (key-2);i>=1;i--){
char tc = rawText[encryptedLetterCounter];
if(isspace(tc)){
if(haveSpaces_==true){
encryptionMatrix[i][columnCounter]=tc;
encryptedLetterCounter++;
if(encryptedLetterCounter==t){
breakFlag=true;
break;
}
}else{
i++;
encryptedLetterCounter++;
if(encryptedLetterCounter==t){
breakFlag=true;
break;
}
}
continue;
}
encryptionMatrix[i][columnCounter]=tc;
encryptedLetterCounter++;
if(encryptedLetterCounter==t){
breakFlag=true;
break;
}
}
if(breakFlag==true){
break;
}
columnCounter++;
}
|
now it gets the job done,but its long and ugly.
can somebody use this to show me what i could have done better
and what i should look to improve in the future?