C++ Coding Conventions #TC1017 #Mastery08

C++ Coding Conventions son una serie de sugerencias para tener una manera mas ordenada de escribir código est sirve de dos maneras:

1.- Para que sea mas fácil para tí conocer cada parte de tu programa.

2.- Cuando este solucionando errores de código poder encontrarlo más fácil.

3.- Fácilitar la lectura a otras personas.

Este es un ejemplo.

<stdlib.h>#include <time.h> <iostream> using namespace std; int main() {int num, num2, cont=1;srand(time(NULL));num=rand()%101;cout << “I have a number chosen between 1 and 100.” << endl;cout << “Please guess a number between 1 and 100:”;cin >> num2;while (num !=num2){ if (num<num2)cout << “The secret number is lower.” << endl;else if (num>num2)cout << “The secret number is higer.” << endl;cout << “Try again:”;cin >> num2;cont=cont+1; }cout << “Congratulations the number is:” << num << endl;cout << “You made ” << cont << ” guesses to get the right number” << endl;return 0;}

Se puede escribir código de esta manera porque la computadora si lo entendería, pero como pueden apreciar es muy difícil encontrar un error o entender la difrencia. Y es por eso que se utilizan los ‘Coding Conventions’.

El mismo código escrito pero ordenado sería así:

<stdlib.h>

<time.h>

<iostream>

using namespace std;

 

int main()

{

int num, num2, cont=1;

srand(time(NULL));

num=rand()%101;

cout << “I have a number chosen between 1 and 100.” << endl;

cout << “Please guess a number between 1 and 100:”;

cin >> num2;

while (num !=num2)

{

if (num<num2)

cout << “The secret number is lower.” << endl;

else if (num>num2)

cout << “The secret number is higer.” << endl;

cout << “Try again:”;

cin >> num2;

cont=cont+1;

}

cout << “Congratulations the number is:” << num << endl;

cout << “You made ” << cont << ” guesses to get the right number” << endl;

return 0;

}

CC BY 4.0 C++ Coding Conventions #TC1017 #Mastery08 by Carlos Adrian is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.