且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

类和对象真的很难

更新时间:2023-12-01 16:44:40

好的,我按照承诺检查了代码.现在,情况与将代码添加到帖子中之前所看到的完全不同.现在,我会问:那么,您在哪里看到问题?".代码还不错.

我实际上可以看到一些问题,而不是非常严重的问题.让我概述其中的一些:

  • 将所有关键字public替换为internal.您不应允许超出实际需要的访问权限.仅当需要由其他某些程序集引用程序集并获得跨程序集边界的访问权限时,才需要public.仅在需要时更改访问权限.
  • 这根本不是一个错误,只是一个注释:您实现了IComparable<TaxPayer>,这确实是排序所必需的.

    您通常还需要实现System.Object.Equals,这将需要您覆盖System.Object.GetHashCode.你知道为什么吗?语法将要求这样做,但真正的目的是:具有覆盖身份的对象必须能够用作字典和类似的键访问集合中的键.以防万一,定义"=="和!="运算符会有所帮助.
  • 现在,这确实是一个严重的问题:请查看立即常量 10.出现了6次!这是您(针对自己)犯下的真正罪行.假设您下次需要12条记录.您将如何支持它?记住其中一项主要原则: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ].

    您有两个选择.首先,您需要将此10设为显式常数.当您更改它时,所有程序的行为将立即更改为10.很简单,不是吗?

    另一个选择更好:请求用户输入元素数而不是10.不要告诉我您的老师并不需要.我已经解释过,这位老师不是那么有资格.同样,开发人员的主要职责之一是做老板"不需要的事情,如果有充分的理由的话.在这种情况下,这就是为什么:开发工作是相同的,但是如何调试呢?每次重复10次输入?当您无需付出额外的努力时,就应该始终使用最抽象的解决方案.
  • 您是否根本需要立即数? 30000、0.15、0.28怎么样?它们不重复了吗?不,但这仍然是一个问题.做同样的事情:将它们放在显式常量中.将所有显式常量放在一起;他们将向您展示任务的参数.如果这将是真实的"开发,则稍后它们都将转换为变量.设计良好的最终版本现实生活应用程序几乎没有常量,除了0、1或null之类的东西.所有数据最终都来自数据文件,配置文件,网络等.
  • 字符串立即数如何?理想情况下,它们都应转到资源和/或数据文件.如果不重复,至少在研究作业中,您的工作可以被接受.
  • 删除-"的愚蠢操作(是的,这很愚蠢,但是正如我所说,
  • 好吧,您很好地遵循了命名约定,除了两个问题:拼写Ssn,而不是SSN(是的,此建议由Microsoft正式推荐,并且有其原因)和i —真是个坏名字.假设您要查找所有循环变量.搜索功能无法为您提供帮助.因此,所有变量都应基于正确拼写的非缩写英语单词. index一词恰好正确.
OK, I reviewed the code, as I promised. Now, the situation is very different from what I could see before you added the code to the post. Now, I would ask: "So, and where do you see a problem?". The code is not too bad.

I actually can see a few problems, and not very critical ones. Let me overview some of them:

  • Replace all keyword public with internal. You should not allow more access than it is really required. You will need public only when you need to reference your assembly by some other assembly and get access across the assembly boundary. Change access only when you need it.
  • Not a mistake at all, but just a note: you implemented IComparable<TaxPayer> which is really needed for sorting.

    You would also often need to implement System.Object.Equals and that will require you to override System.Object.GetHashCode. Do you know why? The syntax will require that, but the real purpose is: the object with overridden identity needs to be able to serve as a key in dictionaries and similar key-accessed collections. Just in case, definition of "==" and "!=" operators would be helpful.
  • Now, this is a really serious problem: look at the immediate constant 10. It appears 6 times! And this is a real crime you are committing (against youself). Suppose you will need 12 records next time. How will you support it? Remember one of the main principles: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

    You have two options. First, you need to make this 10 an explicit constant. When you change it, all program''s behavior will be changed to 10 at once. Easy, isn''t it?

    Another option is even better: request the user to enter the number of elements instead of 10. Don''t tell me your teacher did not require that. I already explained that this teacher is not so qualified. Also, one of the main real responsibilities of the developer is to do something that your "boss" does not require if there is a good reason for that. Here is why, in this case: the development effort will be the same, but how can you debug it? Repeat 10 inputs every time? You should always make the most abstract solution possible when it does not take extra effort.
  • Do you need immediate constants at all? How about 30000, 0.15, 0.28 — they are not repeated? No, but it''s still a problem. Do the same thing: put them in the explicit constants. Put all explicit constants together; they will show you what a parameters of the task. It if would be "real" development, sooner of later they all will be converted to variables. Well designed final-version real life application have almost no constants except things like, 0, 1 or null. All data ultimately comes from data files, configuration files, network, etc.
  • How about string immediate constants? Ideally, they all should go to resources and/or data files. If they are not repeated, what you do can be considered as acceptable, at least for the study assignment.
  • The stupid operation of removal of "-" (yes, it is stupid, but as I say, this is a fault of your teacher) should better be placed in the implementation of the property SSN.
  • Well, you follow naming convention quite well, except two problem: spell Ssn, not SSN (yes, this recommendation is officially recommended by Microsoft, and it has its reasons) and i — really bad name. Imagine you want to find all loop variables. Search feature won''t help you. By that reason, all variables should be based on correctly spelled non-abbreviated English words. The word index would be just right.


DaSasha写道:
DaSasha wrote:

您将还我的财政援助如果我放弃那门课程?我不这么认为,如果您不愿意的话,无需张贴[拼写和语法引文-SA]

you gonna pay my financial aid back if I drop that course? I don''t think so, if u not gonna help no need to post [Spelling and grammar quotes as is — SA]



尊敬的达萨沙,

首先,我坚持认为,我们将需要他的职位的决定留给SteveAdey.我谨希望您也能授予我类似的特权.

只需更彻底地考虑您的财务状况即可:

您不会免费获得经济援助.我很确定您将面临以下其中一种情况:1)需要全额偿还您的援助金,2)需要偿还这笔钱的一部分,而其余的钱将由***或其他机构支付资金; 3)更有可能需要全额偿还您的援助金以及一些利息.

这笔钱或多或少地动摇了学生的假设,即他们能够完成学业,获得必要的经验,并以此方式获得一定的地位和声誉,这可以帮助他们获得合理的收入,足以维持生活并偿还生活费.债务.

让我们看看你的情况.您刚刚起步,只是获得了非常基础的知识.您不仅没有对最基本的任务有足够的信心(这根本不是问题,迟早会出现),您也没有表现出对激动人心的计算领域初学者非常典型的热情,恰恰相反,您表现出缺乏独立的行为,除了获胜",我不能称呼其他任何东西.大多数感兴趣的学生过高地估计了他们的能力,***的学生充分地估计了他们的能力,但是谁抱怨说这项工作真的很辛苦"?我几乎从来没有观察到这种行为.也许这只是您真诚的沟通方式,但实际上对我来说这似乎是一个极端的弱点.

我对您的老师有进一步的担忧.如果要求使用字符串作为类型,但不要在数字中使用破折号"来自一位老师,则表明该老师对编程,质量,维护等方面几乎一无所知.这样的固定格式结构(如SSN),除了使用由数字成员组成的结构的某种适当的整数类型外,什么都不用,这是愚蠢的.即使目标是简化您的任务,也不会简化任何事情.你看,有一种我称之为教育欺诈"的东西.一切都是完全合法的,只有您为虚假的知识付钱,而您可能不够独立,无法感受到虚假的教育服务或自己学习.我不知道,也许您的老师只是软弱无力,但可能是假的.如果您无法抗拒和自己学习(这通常是学习的主要方式),则可以获得假知识和假经验.您认为您可以从中获得足够的钱吗?

那么,会发生什么呢?如果您改变职业道路,可以放一些钱,但是您有机会在其他一些活动中赚钱.如果您不这样做,则可以节省您的援助金,但是只能保留一段时间,因为无论如何您都必须偿还这笔款项.作为交换,您冒着浪费相当多的时间而感到沮丧的风险,并失去了赚钱的机会.您可能会陷入偿还债务的巨大麻烦,而不仅仅是提供生活.我知道,生活可能真的很艰难.实际上,这很难.

但是,我永远不知道.我不知道多少钱,我不知道您的真正风险.你决定.负责.

我最大的愿望是做错事.是的,我希望看到我做错了,您可以应对和改变事物,并在计算机领域取得辉煌的职业.我真的很希望你.我只会尝试帮助您了解其中涉及的内容.

请原谅我这个离题的帖子.我会看看是否能为您添加到帖子中的代码提供帮助.

***的祝福,

—SA



Dear Da Sasha,

First of all, I insist that we leave the the decisions on the need for his posts to SteveAdey along. I would modestly hope that you would grant similar privilege to me as well.

Just think about your financial situation more thoroughly:

You don''t get financial aid for free. I''m pretty sure you will face one of the following: 1) a need to repay your aid money in full, 2) a need to re-pay part of this money while the rest of it will be covered by government or other funds; 3) more likely, a need to repay your aid money in full plus some interest.

The money is funded to the student in more or less shaky assumption that they are able to complete education, gain essential experience and in this way achieve some status and reputation which could help them to get reasonable earnings, sufficient for making for living and repay of the debt.

Let''s look at your situation. You are at the very beginning, just getting very basic knowledge. Not only you are not confident enough in the most elementary tasks (this is not a problem at all, it will come sooner or later) you don''t demonstrate the passion which is very typical for the beginners in the exciting field of computing, just the opposite, you demonstrate lack of independent behavior and something that I cannot call anything but "wining". Most interested students are over-estimate their capability, best students estimate them adequately, but who complains that the job is "really hard"? I almost never observe such behavior. Maybe this is just your sincere style of communication, but in fact it looks like an extreme weakness to me.

I have further concerns about your teacher. If the requirement "use a string for the type, but do not use dashes within the number" comes from a teacher, this is a strong indication that this teacher has very little idea about programming, its quality, maintenance, etc. The presentation of such a fixed-format structure as SSN using anything but some adequate integer type of a structure made of numeric members is plain stupid. Even if the goal would be to simplify your assignment — it does not simplify anything. You see, there is such thing which I call "education fraud". Everything is perfectly legal, only you pay your money for the fake knowledge while you might be not independent enough to feel the fake education services or learn by yourself. I don''t know, maybe your teacher is just weak, but may be a fake. If you cannot resist and learn by yourself (which is normally the main way of learning), you can get fake knowledge and fake experience. Do you think you would be able to get enough money with that?

So, what can happen? If you change your career path, you can loose some money, but you can get a chance to earn some money in some other activity. If you don''t, you can save your aid money, but only for some period of time as you will have to repay it anyway. In exchange, you risk wasting considerable time of your life to just frustration and loose you chance to earn money. You can potentially get into a great trouble of re-paying the debt, not just providing for living. I know, life can be really hard. It is hard, in fact.

However, I never know exactly. I don''t know how much money, I don''t know your real risks. You decide. Be responsible.

My best desire here is to be wrong. Yes, I wish to see that I was wrong and you can cope and change things and make a great career in computing. I really wish you that. I only try to help you to realize what''s involved.

Please forgive me this off-topic post. I''ll see if I can help you with the code you''ve added to your post.

Best wishes,

—SA