全局变量还是棘手的变量传递方案?

发布于 2024-09-10 00:12:16 字数 828 浏览 1 评论 0原文

作为一个背景知识,我即将成为 Comp E 的大四学生,目前正在从事一个实习项目,涉及使用 Monotouch 和 C# 进行 iPad 开发。作为一个在学期中同时进行多个项目的学生,为了使最终产品正常工作,代码风格通常被完全忽略。我相信你们都知道那是什么样的。

现在我有更多的时间来开发、规划和设计我的代码,我陷入了“快速简单的方法”或“复杂而棘手的方法”之间。我将概述我的设计(一开始可能很糟糕)并欢迎所有建议。

我正在创建一个简单的 iPad 应用程序,用户可以在其中选择汽车品牌,然后该选择会在屏幕另一侧填充不同型号的图像。现在,当用户选择该型号时,它会将他们带到另一个屏幕,我在其中询问更多信息,例如价格、年份等。在第二个屏幕上,我有所选汽车型号的图像,作为“提醒”随着屏幕的变化,您正在再次搜索。现在填充该图像取决于选择的品牌和型号。这些都是基于二维信息数组完成的。因此 Cars[0][3] 将是 Acura-TSX 车型,Cars[1][0] - BMW 1 系列。

现在试图摆脱将所有东西都投入到一个巨大的 MAIN 中的大学心态,我知道它不漂亮,但它有效,大学是关于点而不是风格,我已经开始将事情分解为函数和辅助函数。当我调用将切换屏幕并最终加载图像的函数时,我必须向其传递 makeIndex 和 modelIndex,它们是在之前选择 make 和模型之后设置的,因此存在大量调用/返回和变量传递。在我看来,解决这个问题的简单方法是使用 2 个全局变量,但我知道这是不好的做法,并且正在尝试开发更好的编码风格。

您对如何做到这一点有什么建议?简单地说“管好习惯”然后选择快速有效的方法会更好吗?或者我应该花时间计算出多个函数的来回传递,因为其中许多函数都需要知道这些信息?

感谢您花时间阅读本文并感谢您的建议。

——亚当

As a bit of a background I'm about to be a senior in Comp E and currently working on a project for my internship involving iPad development using Monotouch and C#. As a student with multiple projects at once during the semesters, code style is often completely ignored in order to get the final product working. I'm sure you all know what that's like.

Now that I have a lot more time to develop, plan out, and design my code I'm stuck between "the fast easy way" or the "complex tricky way". I'll outline my design (which is probably horrible to begin with) and welcome all suggestions.

I'm creating a simple iPad app where a user can select a make of car, then that selection populates images on the other side of the screen with different models. Now when a user selects that model, it brings them to another screen where I ask for more information such as price, year, etc, etc. On the second screen I have an image of the selected car model as a "reminder" of what you're searching for again as the screens changed. Populating that image now depends upon which make was selected and which model. These are all done based upon a 2D array of information. So Cars[0][3] would be an Acura-TSX model, Cars[1][0] - BMW 1 Series.

Now trying to get out of a college mindset of throwing everything into one giant MAIN, I know its not pretty but it works and college is about points not style, I've started breaking things down into functions and helper functions. When I call the function that will switch screens and eventually load up the image I have to pass it makeIndex and a modelIndex which are set after the make and models are selected previously so there is a bunch of call/returns and variable passing. In my head, the simple EASY way to solve this would be 2 Global Variables but I know this is bad practice and am trying to develop a better coding style.

What are your suggestions on how to do this? Would it be better to simply say "screw good practice" and go with what works fast? or should I take the time and work out the passing back in forth for multiple functions as many of them require knowing this information?

Thanks for taking the time to read this and thank you for your suggestions.

--Adam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水般的淡然安静女子 2024-09-17 00:12:16

为什么不使用单例模式并有这样的东西:

public sealed class CarConfigurationSettings
{
    public string Make{get; set; }

    static readonly CarConfigurationSettings instance=
                                    new CarConfigurationSettings();

    static CarConfigurationSettings()
    {
    }

    CarConfigurationSettings()
    {
    }

    public static CarConfigurationSettings Instance
    {
        get
        {
            return instance;
        }
    }
}

然后你可以在应用程序中的任何地方去:

var make = CarConfigurationSettings.Instance.Make;

很好。

Why not use the singleton pattern and have something like this:

public sealed class CarConfigurationSettings
{
    public string Make{get; set; }

    static readonly CarConfigurationSettings instance=
                                    new CarConfigurationSettings();

    static CarConfigurationSettings()
    {
    }

    CarConfigurationSettings()
    {
    }

    public static CarConfigurationSettings Instance
    {
        get
        {
            return instance;
        }
    }
}

Then anywhere in your app you can just go:

var make = CarConfigurationSettings.Instance.Make;

Nice.

溺ぐ爱和你が 2024-09-17 00:12:16

它应该归结为你的目标。您是否有兴趣:

  • 学习和练习良好的风格,从而获得易于维护的应用程序?
  • 完成项目并扔掉代码?

这个实习项目听起来像是针对实际客户的。实习结束后,谁将维护此应用程序?你离开或者实习结束后,谁的时间和精力会花在增强产品上?

我建议坚持你现在走的路。您建议您有更多的时间来规划和设计。您的同事会感激您投入了一些脑力,甚至将其带到社区以获得反馈。

It should boil down to your goals. Are you interested in:

  • learning and practicing good style, leading to easy-to-maintain applications?
  • getting the project done and throwing the code away?

This internship project sounds like it's for an actual customer. Who'll be maintaining this application once your internship is over? Whose time and energy will be spent enhancing the product after you're gone or the internship is over?

I'd suggest sticking with the path you're on. You suggest that you've got more time to plan and design. Your co-workers will appreciate that you've invested some brainpower, and even have taken it to a community to get feedback.

岁月无声 2024-09-17 00:12:16

我会看一下模型-视图-控制器(MVC)模式,其中您的视图正在渲染汽车/信息,您的模型具有底层数据,控制器将它们绑定在一起。然后,您可以干净地保留想要在控制器类中设为全局的这两个变量 - 视图可以从那里很好地访问它们,并且控制器可以使用它们来控制视图上显示的内容。

事实上,根据我为 iPad 进行开发的经验,无论如何,您几乎都能开箱即用 MVC,因此可能只需了解该结构并弄清楚如何使其适合您的设计即可。

使用 MVC 模式,当您切换屏幕时,您将有一个不同的视图,并且您传递给它的变量可以传递到新视图的控制器中,并存储为该类的成员。

关于“搞砸良好实践”,当您开始开发更大、更复杂的软件时,您会意识到,从长远来看,捷径只会给您带来更大的麻烦和更长的开发时间。最好保持事物干净、设计简洁,以降低复杂性。现在是不要打破这个习惯的好时机:)

I would have a look at the model-view-controller (MVC) pattern, where your view is rendering the cars/information, your model has the underlying data, and the controller binds them together. You can then cleanly keep those two variables you wanted to make global in your controller class - the view can access them nicely from there, and the controller can use them for controlling what appears on the view.

In fact in my experience of developing for the iPad, you pretty much get MVC out of the box anyway, so it may just be a matter of getting your head around that structure and working out how to fit your design to it.

Using the MVC pattern, you'd have a different view for when you switch screens, and the variables you pass into it could be passed into the new view's controller, and stored as members of that class.

Regarding "screwing good practice", as you start to develop bigger and more complex software, you'll realise that the shortcuts just give you bigger headaches and a longer development time in the longrun. Better to keep things clean and crisply designed, to keep complexity down. Now would be a good time not to break that habit :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文