标签: coding-style

大多数pythonic方式表示一个变量,它只通过两个'命名'值切换

我有一个非常复杂的迭代,每次循环,我都需要一个"参数"来影响循环中完成的工作.基本上,我一直在做以下事情:

CLOSE_SIDE = 0
FAR_SIDE = 1

....

while (...):
    if (side == CLOSE_SIDE):
        ....
    else if (side == FAR_SIDE):
        ....

    ....

    side = FAR_SIDE if (side == CLOSE_SIDE) else CLOSE_SIDE
Run Code Online (Sandbox Code Playgroud)

我意识到我可以使用布尔值,但我觉得这降低了我正在做的事情的可读性和显而易见性.我希望这两个州被"命名".这项任务虽然紧凑,但感觉非常笨重.做一整个if语句:

if side == CLOSE_SIDE:
    side = FAR_SIDE
else if side == FAR_SIDE:
    side = CLOSE_SIDE
Run Code Online (Sandbox Code Playgroud)

感觉同样笨重.

解决这个问题的最佳方法是什么?它更像是一种风格问题而不是任何东西.想得到社区的一些意见

python coding-style readability

1
推荐指数
1
解决办法
105
查看次数

什么时候我们应该使用公共和私人?

当我们实现现在不在我们团队解决方案中的其他类使用的方法时,Access Modifier应该是Public还是Private?我相信"公众成员说这个成员代表了这个对象提供的关键,记录的功能." 我们只使用私有的"实现细节"方法和所有可能在将来有用的方法我们应该公开,即使现在我们的方法在其他类中没有消费者.但我的对手说这种方法应该是私人的.你怎么想?

补充:让我们更具体一点.例如,有一个类SqlHelper.其中有一些用于SQL Server操作的有用功能.特别是使用了与SQL服务器的连接.但不仅仅是在那个班级.

例如,我需要实现公共静态HandleSqlExeption方法(现在仅用于类SqlHelper),它将处理SqlExeptions.但是我希望在所有在异常处理中都有SQL连接操作的类中使用此方法(而不是它很简单,例如:catch(Exception){MsgBox {"SqlError"};就像现在发生的那样.所以我认为公共访问修饰符会告诉其他同事他们可以使用这种方法.而私有将隐藏该方法.如果有人要求使用tsuch方法,我将需要ti更改代码并重新组装.为什么?只有底片.

.net c# coding-style

1
推荐指数
1
解决办法
202
查看次数

在C++中正确使用"接口"?

我从未在C++中进行过硬核开发,因为我大约在5年前转向C#.我非常熟悉在C#中使用接口并一直使用它们.例如

public interface IMyInterface
{
   string SomeString { get; set; }
}

public class MyClass : IMyInterface
{
   public string SomeString { get; set; }
}

// This procedure is designed to operate off an interface, not a class.
void SomeProcedure(IMyInterface Param)
{
}
Run Code Online (Sandbox Code Playgroud)

这一切都很棒,因为你可以实现许多类似的类并传递它们,没有人比你更实际使用不同的类更明智.但是,在C++中,您无法传递接口,因为当您看到尝试实例化未定义其所有方法的类时,您将收到编译错误.

class IMyInterface
{
public:
   ...
   // This pure virtual function makes this class abstract.
   virtual void IMyInterface::PureVirtualFunction() = 0;
   ... 
}



class MyClass : public IMyInterface
{
public:
   ...
   void IMyInterface::PureVirtualFunction();
   ... 
}


// The …
Run Code Online (Sandbox Code Playgroud)

c++ coding-style interface

1
推荐指数
1
解决办法
142
查看次数

为什么它是"array.include?object"而不是"object.in?array"?

我最近发现在Python中你可以这样做:

array = [1, 2, 3, 4]
if 3 in array:
  print("Yep!")
Run Code Online (Sandbox Code Playgroud)

然后,我心想:"嗯,为什么它在Ruby中有所不同?if 3 in array比它更具可读性if array.include? 3." 然后,我意识到,Ruby是纯OOP,这种方法是基于关键字的.

但是,我仍然在想.如果Python方法不是OOP,为什么Ruby中没有其他更短的方式可读性更强?在思考时,我不认为"此列表是否包含该元素?",但"该元素是否在该列表中?".

我们假设,以下代码是可能的:

array = [1, 2, 3, 4]

if 3.in? array
  print "Yep!
end
Run Code Online (Sandbox Code Playgroud)

我看到它是从一个掉头list.method(element)element.method(list).所以,我想知道:哪些红宝石原则/规则反对上面提到的代码?

编辑:糟糕,我写了"基于键盘",但当然意味着"基于关键字".要强调这一点:我不是在寻找行为类似于in的方法?方法; 我正在寻找它没有以这种方式在Ruby中实现的原因.

ruby python arrays coding-style

1
推荐指数
1
解决办法
496
查看次数

LessCSS编码标准

有吗?

切换到更少后,我通常最终得到这样的代码:

.topNav{    
  prop: value;
  ...

  li{    
    prop: value;
    ...

    &:hover{
      ...
    }

    &.active{
      prop: value;
      ...

      a{
        prop: value;
        ...

        &:hover{
          prop: value;
          ...

          span{
            ...
          }
        }

        span{
         ...
        }

      } 

    }

    a{
      prop: value;
      ...

      &:hover{
        prop: value;
        ...
      }    

    }  

  }

}
Run Code Online (Sandbox Code Playgroud)

这不好吗?

css standards coding-style less

1
推荐指数
1
解决办法
1036
查看次数

提高大型attr_accessor的可读性

在定义attr_accessor非常大的常量或符号时,我该怎么办?例如,像这样:

ATTRIBUTES = %w(id name full_name owner private html_url description fork url forks_url keys_url collaborators_url teams_url hooks_url issue_events_url events_url assignees_url branches_url tags_url blobs_url git_tags_url git_refs_url trees_url statuses_url languages_url stargazers_url contributors_url subscribers_url subscription_url commits_url git_commits_url comments_url issue_comment_url contents_url compare_url merges_url archive_url downloads_url issues_url pulls_url milestones_url)

attr_accessor :name, :login, :full_name, :owner, :private, :html_url, :description, :fork, :url
Run Code Online (Sandbox Code Playgroud)

在课堂上很糟糕.这是最好的方法吗?我想知道是否还有其他方法可以提高可读性.

ruby coding-style readability human-readable

1
推荐指数
1
解决办法
453
查看次数

如何从简单的C程序中清除屏幕?

#include <stdio.h>
#include <cstdlib>

rec();

main() 
{
    int a, fact;
    char q, n, y;
    printf("\nEnter any number ");
    scanf("%d", & a);
    fact = rec(a);
    printf("Factorial value = %d\n", fact);
    printf("do you want to exit.....(y/n):");
    scanf("%s" ,&q);
    if (q == 'n')
    {
        system("cls");
        main();
    }
    else
        return 0;
}

rec(int x) 
{
    int f;
    if (x == 1) 
        return 1;
    else 
        f = x * rec(x - 1);

    return f;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用代码块,但我不知道如何清除屏幕.我搜索然后system("cls");在头文件中找到#include<cstdlib>,但它显示错误cstdlib: no such file of …

c coding-style header

1
推荐指数
2
解决办法
5万
查看次数

python函数的编码风格

需要一个意见.

我有一个定义一些数据的函数.我的想法是用户可以告诉它从文件中读取数据:

acquire_data('read_from_file',filename)
Run Code Online (Sandbox Code Playgroud)

或者用户可以直接提供数据:

acquire_data('use_this_list',datalist)
Run Code Online (Sandbox Code Playgroud)

所以函数会有类似的形式

def acquire_data(mode,arg2):
    if mode == 'read_from_file':
        inputs=open(arg2)
        data = #etc.
    else:
        data = arg2  #or deepcopy(arg2) or whatever
Run Code Online (Sandbox Code Playgroud)

嗯,这有效,但似乎有点老套.特别是,"arg2"具有非常不同的功能,具体取决于"模式"的值.那么:这个好代码吗?这是"pythonic"吗?有人看到更好的方法来编码吗?谢谢.

python coding-style

1
推荐指数
1
解决办法
114
查看次数

ORM会导致错误的编码实践吗?

我们在团队中使用PHP中的ORM,我在两个单独的项目中注意到,尽管我们已经详细讨论了良好的MVC设计,但ORM似乎允许人们从View中进行数据库查询分层并创建难以维护的代码.

我倾向于认为ORM太容易在程序员不考虑的掩护下进行查询.通过将ORM对象返回到视图层,程序员实际上是将数据库连接泄漏到不应该拥有它的层.

我在这里正确地考虑ORM吗?如果是这样,为什么它如此受欢迎?如果我没有正确思考,我应该如何解决这些问题?

php orm doctrine coding-style

1
推荐指数
1
解决办法
569
查看次数

有没有办法强制OpenGL标识符从一个而不是零开始?

我将OpenGL标识符存储在类型的变量中GLuint.但我不知道我应该初始化这些变量的价值.我想将它们初始化为零,但不幸的是,这是OpenGL的有效标识符.

必须在存储OpenGL对象之前初始化变量,因为这是稍后异步完成的.那么我能以某种方式强制OpenGL开始用一个而不是零开始索引吗?或者我应该使用GLint并初始化-1

我想以干净的方式解决这个问题.当然,我可以存储一对变量和一个布尔标志或类似的东西.但那将是hacky.你如何处理这个问题?

c++ opengl indexing null coding-style

1
推荐指数
1
解决办法
81
查看次数

标签 统计

coding-style ×10

python ×3

c++ ×2

readability ×2

ruby ×2

.net ×1

arrays ×1

c ×1

c# ×1

css ×1

doctrine ×1

header ×1

human-readable ×1

indexing ×1

interface ×1

less ×1

null ×1

opengl ×1

orm ×1

php ×1

standards ×1