views:

71

answers:

3

Hey everyone,

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

+1  A: 

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.

p.campbell
From what I've been hearing from my higher ups is that I'm doing this mainly as a research avenue with an eventual demo for a client. As far as I can tell, the client and my immediate supervisors are mainly focused on "Making it pretty" because its an iPad app (and there's no reason to use one of those unless it's pretty...). Other than that, this is essentially a solo research and experiment task for me with the hopes of it becoming a client project.
Adam
+1  A: 

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.

cvista
Thanks a lot for this. To be completely honest, I've never even heard of this before. But I guess that comes with the territory of doing the majority of my school work in x86 and C..After reading through the wiki article, it seems like this can be viewed as a way of getting around the whole "no global variables" by simply putting all of those in a class. Is there a performance increase to this method? It seems interesting and looks like it could clear up a lot of my code.
Adam
Singletons are in general a really bad practice in object oriented development. It makes testing the code and extending it extremely difficult.While it may sound like a cool, simple idea at first, it'll end up just getting in your way. There's more information about it on this thread: http://stackoverflow.com/questions/11831/singletons-good-design-or-a-crutch
Eduardo Scoz
that discussions most popular (in fact most of the answers are) answer is in favor of singletons?! "Singleton is not a horrible pattern, although it is misused a lot." just because people abuse them and there is a discussion about whether or not they are useful doesn't mean they are a "really bad practise"?! what would be the alternative for the OP then? a nice static helper class called utils no doubt ;)
cvista
There's actually not a big difference between global variables and the use of a singleton. And if you add other threads to your app, both approaches will lead to disaster....
NeilDurant
A: 

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 :)

NeilDurant