关于变量/函数命名约定的想法 [英] Thoughts on variable/function naming conventions

查看:92
本文介绍了关于变量/函数命名约定的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程,我的整个生活。我主要编码Perl,但也有一些Java,PHP,C,C ++。我甚至在Emacs Lisp做了一个刺,我做了偶尔的shell脚本。然而,我从来没有实际参与过这个课程,获得任何专业知识 - 其他事情对我有更高的优先级。我不认为自己非常熟练的任何语言,但Perl,现在也是现在Haskell我正在课程现在。

I've been coding on and off my whole life. I've mostly coded Perl, but also some Java, PHP, C, C++. I've even took a stab at Emacs Lisp, and I've done the occasional shell script. However, I've never actually engaged the subject to gain any kind of expertise – other things have had higher priorities for me. I don't consider myself to be really proficient in any language but Perl, and also now Haskell which I'm taking a course in right now.

最近,一直在思考我的编码风格。不是实际代码的样式;作为一个CS学生,我只做项目的乐趣或学校,这使我能写我在我看来是美丽的代码几乎总是。特别是一个问题困扰着我。这是一个很好奇的东西,但仍然是我想听到的其他意见。

Lately, I've been thinking about my style of coding. Not the style of the actual code; as a CS student I only do projects for fun or for school, which enables me to write what in my opinion is beautiful code almost always. One question in particular has been troubling me. It's a rather curious thing, but still something I would like to hear other opinions about.

这里的东西:我发现自己花了相当多的时间来命名我的功能和变量到最容易理解的名字,我可以想到。有时,这个任务可能非常繁琐,即使没有考虑到找到一个传递一段代码含义的变量名的困难。例如,现在我正在创建一个类似这样的函数。

Here's the thing: I find myself taking a fair amount of time to name my functions and variables to the most easily understood names I can possibly think of. Sometimes this task can be very tedious, even when not taking into account the difficulty of finding a variable name that conveys the meaning of a piece of code. For example, right now I'm making a function that looks like this.

这是Haskell代码,但意思应该很清楚。 (这是确切的意思不是那么重要,所以如果你想,只是跳过代码,阅读。)

This is Haskell code but the meaning should be quite clear. (It's exact meaning isn't that important so if you want to, just skip the code and read on.)

-- return the row with least number of Nothing values
bestRow :: [[Maybe Int]] -> Int -> Maybe (Int,Int)
bestRow [] _ = Nothing
bestRow (row:rows) thisIndex
   | nextRow == Nothing && thisFilled > 8 = Nothing
   | nextRow == Nothing       = Just (thisIndex,thisFilled)
   | thisFilled >= nextFilled = Just (thisIndex,thisFilled)
   | thisFilled <  nextFilled = nextRow
     where thisFilled             = length $ filter (/= Nothing) row
           nextRow                = bestRow rows (thisIndex + 1)
           (nextIndex,nextFilled) = fromMaybe (-1,-1) nextRow

我无法决定变量名称。该功能做了它的任务很好,但它不是那么清楚,因为它可能是。一旦我决定了一个解决方案,我花了15分钟命名和重命名的变量。我应该用curIndex,nextIndex || index,nextIndex || ind,indN等? 15分钟后,我决定我做了,我意识到这个功能毕竟不需要:我发现了一个更好的解决我的问题。 BOOM我失去了很多时间,只是清理代码,没有任何人使用,最少对我。或者至少它觉得这样。

I couldn't decide on the variable names. The function did it's task well but it wasn't as clear as it could be. Once I settled on a solution, I spent 15 minutes on naming and renaming the variables. Should I go with curIndex, nextIndex || index, nextIndex || ind, indN etc? 15 minutes after I decided I was done, I realized this function wasn't needed after all: I found a much better solution to my problem. BOOM I had lost a lot of time simply cleaning code to no use to anyone, least of all to me. Or at least it felt that way.

这是发生在我身上多次的事情,很令人沮丧,主要是因为它让我感到蠢。你对这个问题有什么想法?这是你经历的,我的做事方式或只是不可避免的东西有问题吗?

This is something which has happened to me more than once, and is quite frustrating, mostly because it makes me feel dumb. What are your thoughts on this subject? Is this something you've experienced, something wrong with my way of doing things or simply something unavoidable?

有完美的变量名,或者是ok如果他们至少不会模糊您的代码?

Are there "perfect" variable names, or is it "ok" if they at least doesn't obfuscate your code?

感谢,

Stefan Kangas

Stefan Kangas

推荐答案

阅读Robert C. Martin的清洁代码

Read the book Clean Code by Robert C. Martin

细节就是命名。变量和函数名称应尽可能精确。如果你需要一个注释来描述它,函数或者做太多,或者名字可以更清楚。

One thing he goes into detail on is naming. Variable and function names should be as exact as possible. If you require a comment to describe it, the function is either doing too much, or the name can be clearer.

目标是让代码读取像故事。

The goal, is to have code that reads like a story. Changing variable and function names and breaking them down into several functions or variables if necessary to achieve this goal.

很好的书,值得每一分钱。

Great book, worth every penny

这篇关于关于变量/函数命名约定的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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