Actual combat: how to reasonably design functions in software development to solve practical problems

Objective: To explore how to design functions to improve program efficiency
Draw a conclusion by using two cases of prime numbers and species writing
Case 1: judge whether a number is a prime number

//Time:
// Judge whether a number is prime
// Only one function is used to implement, which is not good and the code utilization is not high

#include<stdio.h>

int main(void)
{
	int val = 29;
	int i;

	for (i = 2; i < val; ++i)
	{
		if (val % i == 0)
			break;
	}


	if (i == val)
		printf("%d", val);
	else
		printf("bushi");
	return 0;
}

Conclusion: This is not a good way to write, because the code is all written in a pile. First, the reusability is not high, and the main function is too miscellaneous, resulting in a decline in readability
When learning software engineering, I remember that the teacher said that the complex problems in the problem domain are transformed into simple operations through the model. Its essence is to use the human idea of solving complex problems step by step, and layered complex problems are an effective means to simplify the complexity of problems, which is not a simple matter.
Therefore, in the program design stage, there are also methods to improve reusability and readability
The second solution to the same problem:

//Time:
// Judge whether a number is prime
// Using a separate function to implement, the reusability of the code is improved
#include<stdio.h>
bool IsPrime(int val)
{
	int i;
	for (i = 2; i < val; ++i)
	{
		if (val % i == 0)
			break;
	}
	if (i == val)
		return true;
	else
		return false;
}
int main(void)
{
	int val = 29;
	int i;

	if (IsPrime(val))
		printf("%d", val);
	else
		printf("bush222");
	//for (i = 2; i < val; ++i)
	//{
	//	if (val % i == 0)
	//		break;
	//}

	//if (i == val)
	//	printf("%d", val);
	//else
	//	printf("bushi");


	return 0;
}


It can be seen from this function that after using the self-defined function, the whole program becomes more hierarchical. The main function has changed from the original 15 lines to the current 7 lines, which improves the readability of the function. Moreover, our user-defined function only processes the data, then puts back the processing results, and does not operate on the processed results, which increases the readability of the code.

Maybe the above example is too simple and not clear enough!

The second example: find all prime numbers between one and a number (Paul the number) and output them
The first way to write:

//Time:
// Function: find all prime numbers from one to a certain number and output them
//Conclusion: only one function (main function) is used, which has limitations:
 //     1. The reusability of the code is not high
 //     2 the code is not easy to understand
//
#include<stdio.h>

int main(void)
{
	int val = 129;
	int i;
	int j;
	for (j = 2; j <= val; ++j)
	{
		for (i = 2; i <= j; ++i)
		{
			if (j% i == 0)
				break;
		}
		if (i == j)
			printf("%d\n", j);
		
	}


	return 0;
}

Conclusion: it is not difficult to find that the main function is complex and difficult to understand, and the reusability of the whole program is low
To judge multiple prime numbers, the second solution:

//Time: 2
// Function: find all prime numbers from one to a certain number and output them
// Conclusion: a function is used to judge whether a number is a prime number

//
#include<stdio.h>
bool IsPrime(int m)
{
	int i;
	for (i = 2; i <= m; ++i)
	{
		if (m % i == 0)
			break;
	}
	if (i ==m)
		return true;
	else
		return false;
}
int main(void)
{
	int val = 129;
	int i;
	
	for (i = 2; i <= val; ++i)
	{
		 if (IsPrime(i))
				printf("%d\n", i);
		 
	}

	return 0;
}

Conclusion:
Advantages: the readability of the code is slightly improved, and the main function has been reduced a lot, which is easier to understand
High code reusability

Disadvantages: reusability is still not very high
For example, find 1000 numbers, and find each number from one to their own prime
be

for (i = 2; i <= val; ++i)
					{
						if (IsPrime(i))
					printf("%d\n", i);
								}

A thousand times
Therefore, we add a little more detail to the original procedure:

//Time:
// Function: used to find all prime numbers between one and a number (Paul the number) for the implementation of that function and output them
// Conclusion: compared with the "two functions with multiple parameters", this program has less code and higher reusability

//: 
#include<stdio.h>
bool IsPrime(int m); //forward declaration 
//The function of this function is to output all prime numbers between 1 and n on the display
void TraverseVal(int n)
{
	int i;
	for (i = 2; i <= n; ++i)
	{
		if (IsPrime(i))
			printf("%d\n", i);
	}
}
//Function of this function: judge whether m is a prime number, return true instead of false
bool IsPrime(int m)
{
	int i;
	for (i = 2; i <= m; ++i)
	{
		if (m % i == 0)
			break;
	}
	if (i == m)
		return true;
	else
		return false;
}
int main(void)
{
	int val = 129;
	int i;
	TraverseVal(val);
//	for (i = 2; i <= val; ++i)
//	{
//		if (IsPrime(i))
//			printf("%d\n", i);

//	}
	return 0;
}

Conclusion: I think this version basically realizes high reusability and high readability.
Welcome to comment.

Tags: C Back-end

Posted on Sun, 31 Oct 2021 09:35:41 -0400 by rocksolidsr