Program Design

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 5387 Views - Last Post: 04 July 2015 - 07:19 AM

#1 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Program Design

Post icon  Posted 17 June 2015 - 09:45 AM

Hello folks. I have a quick question for y'all today.

I have read both on these forums and elsewhere that when beginning to write a program that one of the last things to be done it to actually write the code on the computer. The program and its classes should be well thought out before committing them into lines of code. As I begin to write programs I find difficulty in understanding this.

What is it that I should be doing on paper before writing the code? I find myself just writing out java syntax line by line as if I was writing the real thing and then translating it to the computer but this feels like I'm doing it wrong.. Any advice you guys and gals can lend on the subject of planning and design, pre-production, so to speak? Thanks!

Is This A Good Question/Topic? 2
  • +

Replies To: Program Design

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1469
  • View blog
  • Posts: 8,176
  • Joined: 07-September 06

Re: Program Design

Posted 17 June 2015 - 11:49 AM

There is a variety of things that you can write out that isn't code.

The first thing that a lot of people tend to do is write pseudo-code. It is basically writing out the logic behind your program in English (or your native language, if that's not English). This is to get you thinking about the problem and how to solve it (as well as what you will need to solve it).

The next thing that a lot of people like to write out is the classes, members, and how they will all interact with each other. For instance, with a linked list you likely have your container class, and then a node class and the node holds the actual data in the list, but is in itself, just a single item of the list. For small projects this step is likely not necessary, but as you get in to larger and larger problems that need to be solved this can really become useful.

One other thing that you may find as you get going with coding is that you can do some if not all of the design phase in your head before programming. Literally most of the time I am programming I am actually thinking about the problem and trying to decide how best to approach it (what benefits are there to one way over another, etc.). This, however, isn't recommended while you are starting out as it can be very useful to have something physical to see in front of you (or share with others).
Was This Post Helpful? 4
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12135
  • View blog
  • Posts: 45,119
  • Joined: 27-December 08

Re: Program Design

Posted 17 June 2015 - 12:17 PM

Try and getting away from thinking in low-level logic with design. Think about objects and how they will interact. How will the Person interact with the Store? How would you describe a Person? How should the Store handle interactions? What does it have to offer?

These are design questions to ask. Think about working out the interactions on paper, as if you were writing an acting script for the objects to interact.
Was This Post Helpful? 3
  • +
  • -

#4 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 18 June 2015 - 10:04 AM

Thanks for the responses my peeps. I will give your suggestions a try. My problem is getting hung up on the algorithmic parts of programs. I will try to take a macro view and decompose my problems but I am thinking about so many things at once that it tends to lead to confusion.

Also, the fact that there are so many different ways to solve these problems can overwhelm me and cause neurotic behavior from worrying about whether I am doing it the "right" way or the "best" or the "clever" way leads me to frustration. Hopefully these symptoms go away with a better grasp and more experience?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,479
  • Joined: 12-June 08

Re: Program Design

Posted 18 June 2015 - 10:12 AM

With most things- the more experience you have the better you can hone your skill.

I would suggest looking up image examples and tutorials on 'class design diagram', 'system design document', and 'software requirements document'.
Was This Post Helpful? 2
  • +
  • -

#6 BobRodes  Icon User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,069
  • Joined: 19-May 09

Re: Program Design

Posted 18 June 2015 - 04:05 PM

You may find it interesting to look at UML Class Diagrams.

This post has been edited by BobRodes: 18 June 2015 - 04:06 PM

Was This Post Helpful? 1
  • +
  • -

#7 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 19 June 2015 - 08:18 AM

To all: Will do!
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Program Design

Posted 19 June 2015 - 08:54 AM

Great question. Another way to think about this, particularly in Java or other object-oriented languages, is to start telling yourself stories about the objects you're representing. What do those objects look like - not in terms of their code, but in terms of their behavior? To recycle an example I already thought about earlier, in a different thread, suppose you're writing a Reversi game. What objects do you need for this? Well, presumably there's a Board, since it's a board game. What does a Board do? It holds pieces, it defines where they are relative to each other, it knows whether a space is occupied. What doesn't a Board do? Well, probably it doesn't know about the color of a particular piece that it's holding - that's the Piece's responsibility. It probably doesn't know whose move it is. Maybe it knows if the game's winning condition has been met, but probably it doesn't know whether the game's been won or not. (I would think that would be the responsibility of a Game). What does a Piece do? In reversi, it gets put on a square, and then it flips itself over when asked. Okay, now when you put a piece down, what happens? Presumably there's some cooperation between the Board and the Pieces. We'd need to think that out a little.

And so forth. Think about what it's like to work with these objects, not about the lines of code that make them work that way. The advantage of this is, it's a lot harder to change your mind about the design after you've started writing the code, and thinking it through makes it less likely that you'll change your mind. For example, you might realize that your first impression put far too much responsibility in the Board, and instead you needed a Board and a Game - this is something you'll get good at realizing as you write more code.
Was This Post Helpful? 4
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12135
  • View blog
  • Posts: 45,119
  • Joined: 27-December 08

Re: Program Design

Posted 19 June 2015 - 09:11 AM

I've featured this and will move it to the Advanced Discussion forum for better visibility.
Was This Post Helpful? 1
  • +
  • -

#10 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 28 June 2015 - 01:01 PM

Hey guys, im back at it. A few more questions for you all on the topic. I am writing a program and I am having trouble deciding what should be an object, what should be a method.. and even things like what should be in the main method exactly, what should be in a helper method and so on. I know what these things are individually and mostly how they interact. I just don't know where to put what.. Any word of advice on how to decide what should be an object when it isnt a cut and dry real world actual physical object to model after?
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Program Design

Posted 28 June 2015 - 01:35 PM

View Postscatman_crothers, on 28 June 2015 - 03:01 PM, said:

Hey guys, im back at it. A few more questions for you all on the topic. I am writing a program and I am having trouble deciding what should be an object, what should be a method..


As a first cut, nouns are objects and verbs are methods. One of the fundamental ideas of OO design is that verbs belong to objects - that is, methods are part of objects. That is, we write programs where things can do stuff, and the stuff they can do is a big part of the definition of the things.

Quote

and even things like what should be in the main method exactly,


Almost nothing. Seriously: the main method should do as little as possible, always. Its only purpose is to get things started. If you think of the program as a tree, main is the root of the tree: it's the highest point, everything in a java program hangs off of that method. Good design pushes decisions down to the lowest practical level, so it stands to reason that in any given program, almost nothing earns that slot at the top.

Quote

what should be in a helper method and so on.


It might be useful to think of a "helper method" as a way to break out a named action from a function. This is handy. Think about what happens when you extract five or ten lines of code from a function and leave a verb in their place. First, the original function is improved, because it's easier to understand a single verb than five or ten lines of code. Second, the five or ten lines now have a name to tell the next programmer exactly what they're supposed to do, so they're easier to read as well. Third, now you can write unit tests for the helper function, where before you could only test the larger function. So now your unit tests can be more meaningful, and your program is going to be less bug-prone.
In fact, you shouldn't think of them as "helper methods" at all. Instead, you should just always make functions that are simple and single-purposed and short. (I work with a guy who will cry a little bit when he has to commit a function that's longer than ten lines... he has the right sort of ethic)

Quote

I know what these things are individually and mostly how they interact. I just don't know where to put what..


This is something you'll be better at when you've made a few more good mistakes. I don't usually recommend this, but it's sometimes helpful to take on something way beyond your current capacity, try really hard, fail brutally, and learn a shit-ton from the process.

Quote

Any word of advice on how to decide what should be an object when it isnt a cut and dry real world actual physical object to model after?


Can you give an example of a design scenario where you're confused about what should be which and which should be the other? (or maybe it's the other way around...)
Was This Post Helpful? 1
  • +
  • -

#12 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 29 June 2015 - 08:39 AM

Hey thanks for the reply, lots of useful information and clarification for my issue. Thinking in terms of nouns and verbs did help.

If the main method should do as little as possible, is it ok then to have unrelated classes initialize objects of another class and then use them? When an object is passed between methods and then is returned is that a copy of the original object or the same object that was initially passed? How would that effect that objects instance variable, if at all?

Quote

an you give an example of a design scenario where you're confused about what should be which and which should be the other? (or maybe it's the other way around...)


Well let's say I need to have my program simulated a battle sequence with 3 battle phases and associated die rolls.. Should I have a battle class and a separate die rolling class to handle the various types of die to be rolled? Should I have yet another separate class defining each phase of battle?
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,479
  • Joined: 12-June 08

Re: Program Design

Posted 29 June 2015 - 08:58 AM

A dice class sounds reasonable..

As for a 'battle class'.. I am unclear how how it would be used.

You haven't presented any information on why the separate phases of battle are needed as classes.. they very well could just be separate methods or just parts of a same method.
Was This Post Helpful? 0
  • +
  • -

#14 scatman_crothers  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 25-March 14

Re: Program Design

Posted 29 June 2015 - 10:16 AM

Battle class would invoke the dice methods at the appropriate phase of the battle and do the casualties calculations based on the results of the die rolls? Three phases of battle might be skirmish phase and then two assault phases for example with their own stipulations based on user text decisions and casualty calculation based on their individual outcome. This is why Im confused lol, Whatchu think?
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Program Design

Posted 29 June 2015 - 10:42 AM

View Postscatman_crothers, on 29 June 2015 - 10:39 AM, said:

Hey thanks for the reply, lots of useful information and clarification for my issue. Thinking in terms of nouns and verbs did help.

Great, glad I could help.

Quote

If the main method should do as little as possible, is it ok then to have unrelated classes initialize objects of another class and then use them? When an object is passed between methods and then is returned is that a copy of the original object or the same object that was initially passed? How would that effect that objects instance variable, if at all?


Yes, this is a standard approach. For example your Game class might have a main method that would instantiate the various objects required to play a game and then kick off events.


Quote

Well let's say I need to have my program simulated a battle sequence with 3 battle phases and associated die rolls.. Should I have a battle class and a separate die rolling class to handle the various types of die to be rolled? Should I have yet another separate class defining each phase of battle?


A class for Dice might be a good way to encapsulate the generation of random events. Might not be necessary, but if you're rolling dice from various parts of the program, it's nice to be able to define that functionality in one place and call it as needed. If a Battle is a phase of a game, which has its own state, then yes it could easily be a class.

As an example, I work for a startup accelerator. In our data model, a Program is a complete round of acceleration, located in a city and having a start date and an end date. Each program has multiple Judging Rounds, which is a period in which judges are reviewing applications, and judging rounds have Stages (for example, a three-week round might be divided into three stages), and Stages have Scenarios, which spell out how judges and applications are assigned to Panels, which are collections of assignments of judges and panels. There's more details in there, but this is probably enough for now.

All of this means that we have broken down a big problem into small components at the right level of granularity to allow us to focus on each moving part as an isolated unit, and this makes problems easier to solve. So your challenge is to find this "right level of granularity" for your game, and this is largely a matter of trying things and figuring out how whether they make your life easier. You might ask yourself whether a Move should be a class. The answer to this will depend entirely on your needs and your design. When I was learning Java, I wrote a number of games and generic game playing systems, some of which had Move as a class, and some of which didn't, and I think in most cases the ultimate decision was a good one)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2