这是好的代码吗? [英] Is this good code?

查看:58
本文介绍了这是好的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我们所有人在开始时都解决了其中一个问题

编程:给出3个数字打印出更大和更小的一个

。 br />

我试图记住if-then-else结构,当有什么东西击中我b $ b时我写道:


//函数返回两个给定数字中较大或较小的数字,


//由一个标志指定:1表示大(默认),0表示较小


int greater_lesser(int a,int b,bool flag = 1){


int toBeReturned;


if(b) flag)

a> b? toBeReturned = a:toBeReturned = b;

else

a> b? toBeReturned = b:toBeReturned = a;


返回BeBeturned;

}


int main(){


int x,y,z;

cin>> x>> y>> z;

cout<< 更大: << greater_lesser(greater_lesser(x,y),z)<< endl

<< 较小的: << greater_lesser(greater_lesser(x,y,0),z,0)<<结束;


返回0;

}


代码可以正常工作,但看起来很奇怪,如果它可以改善...


有什么想法吗?谢谢!

I found one of that problems all of us have solve when they begin
programming: given 3 numbers print the greater and the lesser one of
the set.

I was trying to remember the if-then-else structure when something hit
me and I wrote:

// Function returns the greater or lesser numbers of two given numbers,

// specified by a flag: 1 for greater (default), 0 for lesser

int greater_lesser(int a, int b, bool flag=1){

int toBeReturned;

if(flag)
a > b ? toBeReturned = a : toBeReturned = b;
else
a > b ? toBeReturned = b : toBeReturned = a;

return toBeReturned;
}

int main(){

int x, y, z;
cin >> x >> y >> z;
cout << "Greater : " << greater_lesser(greater_lesser(x,y),z) << endl
<< "Lesser : " << greater_lesser(greater_lesser(x,y,0),z,0) << endl;

return 0;
}

The code works all right, but it seems odd, as if it can be improved...

Any thoughts? Thanks!

推荐答案

对您的代码内联的评论:


" Gaijinco" < GA ****** @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
Comments on your code inline:

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
我发现当我们开始编程时,我们所有人都解决了其中一个问题:给定3个数字打印出该组中较大和较小的一个。

我试图记住if-then-else结构,当有什么东西击中我和我写道:

//函数返回两个给定数字中的更多或更少的数字,

//由一个标志指定:1表示大(默认),0表示较小

int greater_lesser(int a,int b,bool flag = 1){


bool flag = true

int toBeReturned;


不需要这个。

if(flag)
a> b? toBeReturned = a:toBeReturned = b;


返回> b? a:b;

其他
a> b? toBeReturned = b:toBeReturned = a;


返回> b? b:a;

返回BeReturned;


不需要这个

}

int main(){

int x ,y,z;
cin>> x>> y>> z;
cout<< 更大: << greater_lesser(greater_lesser(x,y),z)<< endl
<< 较小的: << greater_lesser(greater_lesser(x,y,0),z,0)<< endl;

返回0;
}

代码可以正常工作,但看起来很奇怪,好像它可以改进......

有什么想法?谢谢!
I found one of that problems all of us have solve when they begin
programming: given 3 numbers print the greater and the lesser one of
the set.

I was trying to remember the if-then-else structure when something hit
me and I wrote:

// Function returns the greater or lesser numbers of two given numbers,

// specified by a flag: 1 for greater (default), 0 for lesser

int greater_lesser(int a, int b, bool flag=1){
bool flag = true

int toBeReturned;
Don''t need this.

if(flag)
a > b ? toBeReturned = a : toBeReturned = b;
return a > b ? a : b;
else
a > b ? toBeReturned = b : toBeReturned = a;
return a > b ? b : a;

return toBeReturned;
Don''t need this
}

int main(){

int x, y, z;
cin >> x >> y >> z;
cout << "Greater : " << greater_lesser(greater_lesser(x,y),z) << endl
<< "Lesser : " << greater_lesser(greater_lesser(x,y,0),z,0) << endl;

return 0;
}

The code works all right, but it seems odd, as if it can be improved...

Any thoughts? Thanks!




现在,这不是我的方式。如果我想要一个函数来
给我更多或更少的数字,我会允许

元素的数量是任意的。意思是传递对向量的引用和

返回更大的向量。甚至可能已经有一些STL功能

这样做,我对所有STL功能都不太了解。


有很多如何做到这一点,但我会让其他人评论STL方法

在我发布任何其他内容之前做这件事因为我很确定这很简单用

一些容器迭代。



Now, this is not the way I would do it anyway. If I wanted a function to
give me the greater, or lesser of numbers I would allow the number of
elements to be arbitrary. Meaning passing in a reference to a vector and
returning the greater one. There might even already be some STL function
that does that, I don''t know enough about all the STL functions to say.

There are many ways to do that, but I''ll let others comment on STL methods
to do it before I post anything else cause I''m fairly sure it''s simple with
some container iteratation.


Gaijinco写道:
Gaijinco wrote:
我发现其中一个问题我们所有人都解决了开始
编程:给出3个数字打印集合中较大和较小的一个。

我试图记住if-then-else结构当某些东西击中时我和我写道:

//函数返回两个给定数字中较大或较小的数字,
//由一个标志指定:1表示更大(默认值) ,0表示较小的

int greater_lesser(int a,int b,bool flag = 1){

int toBeReturned;

if(flag)
a> b? toBeReturned = a:toBeReturned = b;

a> b? toBeReturned = b:toBeReturned = a;

返回归来;
}
int main(){

int x,y, z;
cin>> x>> y>> z;
cout<< 更大: << greater_lesser(greater_lesser(x,y),z)<< endl
<< 较小的: << greater_lesser(greater_lesser(x,y,0),z,0)<< endl;

返回0;
}

代码可以正常工作,但看起来很奇怪,好像它可以改进......
I found one of that problems all of us have solve when they begin
programming: given 3 numbers print the greater and the lesser one of
the set.

I was trying to remember the if-then-else structure when something hit
me and I wrote:

// Function returns the greater or lesser numbers of two given numbers,

// specified by a flag: 1 for greater (default), 0 for lesser

int greater_lesser(int a, int b, bool flag=1){

int toBeReturned;

if(flag)
a > b ? toBeReturned = a : toBeReturned = b;
else
a > b ? toBeReturned = b : toBeReturned = a;

return toBeReturned;
}

int main(){

int x, y, z;
cin >> x >> y >> z;
cout << "Greater : " << greater_lesser(greater_lesser(x,y),z) << endl
<< "Lesser : " << greater_lesser(greater_lesser(x,y,0),z,0) << endl;

return 0;
}

The code works all right, but it seems odd, as if it can be improved...




为什么重新发明轮子?


#include< algorithm>

#include< iostream>


int main(无效){

int x,y,z;

std :: cin>> x>> y>> z;

std :: cout<< max: << std :: max(std :: max(x,y),z)<< ''\ n''

<< min: << std :: min(std :: min(x,y),z)<< ''\ n'';

}

最好


Kai-Uwe Bux



Why reinvent the wheel?

#include <algorithm>
#include <iostream>

int main ( void ) {
int x, y, z;
std::cin >> x >> y >> z;
std::cout << "max: " << std::max( std::max( x, y ), z ) << ''\n''
<< "min: " << std::min( std::min( x, y ), z ) << ''\n'';
}
Best

Kai-Uwe Bux




" Gaijinco" < GA ****** @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
我发现当我们开始编程时,我们所有人都解决了其中一个问题:给定3个数字打印出该组中较大和较小的一个。

我试图记住if-then-else结构,当有什么东西击中我和我写道:

//函数返回两个给定数字中的更多或更少的数字,

//由一个标志指定:1表示更大(默认),0表示更小

int greater_lesser(int a,int b,bool flag = 1){

int toBeReturned;

if(flag)
a> b? toBeReturned = a:toBeReturned = b;

a> b? toBeReturned = b:toBeReturned = a;

返回归来;
}
int main(){

int x,y, z;
cin>> x>> y>> z;
cout<< 更大: << greater_lesser(greater_lesser(x,y),z)<< endl
<< 较小的: << greater_lesser(greater_lesser(x,y,0),z,0)<< endl;

返回0;
}

代码可以正常工作,但看起来很奇怪,好像它可以改进......

有什么想法?谢谢!
I found one of that problems all of us have solve when they begin
programming: given 3 numbers print the greater and the lesser one of
the set.

I was trying to remember the if-then-else structure when something hit
me and I wrote:

// Function returns the greater or lesser numbers of two given numbers,

// specified by a flag: 1 for greater (default), 0 for lesser

int greater_lesser(int a, int b, bool flag=1){

int toBeReturned;

if(flag)
a > b ? toBeReturned = a : toBeReturned = b;
else
a > b ? toBeReturned = b : toBeReturned = a;

return toBeReturned;
}

int main(){

int x, y, z;
cin >> x >> y >> z;
cout << "Greater : " << greater_lesser(greater_lesser(x,y),z) << endl
<< "Lesser : " << greater_lesser(greater_lesser(x,y,0),z,0) << endl;

return 0;
}

The code works all right, but it seems odd, as if it can be improved...

Any thoughts? Thanks!




不,这太可怕了!


以这种激进的方式控制函数行为的标志是

经常

令人困惑并且使代码难以理解,尤其是像这样简单的

。也许在一个更复杂的例子中,基本算法可以适应

来执行不同的变化,标志可能更合适,但是我

一般来说

将该功能设为私有并提供基本功能的简单包装

对用户有意义的更多




标志有一个倍增的习惯,所以我们经常在代码中看到两三个标志

参数

影响函数制作的行为代码难以理解。

以下更容易理解?


#include< iostream>

int lessser( int a,int b)

{

返回< b? a:b;

}

int greater(int a,int b)

{

返回> ; b? a:b;

}

int main()

{

int x,y,z;

std :: cin>> x>> y>> z;

std :: cout<< 更大: <<更大(更大(x,y),z)<< " \ n"

<< 较小的: <<较小的(较小的(x,y),z)<< " \ n";


返回0;

}



No, this is horrible!

Flags which control the behavior of a function in such a radical way are
often
confusing and make code difficult to understand, especially something as
simple
as this. Perhaps in a more complex example where a basic algorithm can be
adapted
to perform different variations, flags might be more appropriate, but I
would generally
make that function private and provide simple wrappers of the basic function
to be more
meaningful to a user.

Flags have a habit of multiplying, so we often see two or three flag
parameters in code which
affect the behavior of the function making code harder to understand.
Surely the following is easier to understand ?

#include <iostream>
int lesser( int a, int b)
{
return a < b ? a : b;
}
int greater( int a, int b)
{
return a > b ? a : b;
}
int main()
{
int x, y, z;
std::cin >> x >> y >> z;
std::cout << "Greater : " << greater ( greater (x,y),z) << "\n"
<< "Lesser : " << lesser( lesser(x,y),z) << "\n";

return 0;
}


这篇关于这是好的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆