whats the best way to plan a code, whats the best way to plan it to use re-usable classes

Pages: 12
@zereo, you could take a disc-cutter to the broad side of a scrap freezer and mount it in an extra large picture frame, cost you a tenth of that...you could sell em to! there's now a gap in the market, even a pane of good glass so long as the background is bright.

Am copying my rectangles on paint now :P
Last edited on
@Hamsterman, and others;

Here are my rectangles, they actually represent functions! They will represent classes, Im going back to learning c++ again because its more familiar, but getting the concepts down in java before approaching them in c++ will help me undersand the documentation better.

My aim for this code was to make a multi-thread network handling server thing that worked with a smooth protocol, I did make one with a tutorial but to prove to myself I understood it I tried to make my own design, thats why i wanted to plan it with rectangles, I also wanted a way of typing commands to change things in, I was hoping it would give me some insight for learning bash, (thats an idea making a faux bash type scripting program for practice--thats what I will practice c++ with again later)

any way heres code, Im not sure if it works but I sure get a lot of errors and theres no close function yet

heres the link to my rectangles: www.dropbox.com/s/8eycdda0gpegl7i/rectangles%20as%20functions.bmp

as for my code

message handling bits:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

public class messagestuff {

    private Scanner speakline = new Scanner(System.in);
    public boolean listening = true;
    static String[] namearray = new String[10];
    public String[] messagearray = new String[3];

    public void commandhandler(String cmdstring){
     if(cmdstring.contains("endlisten101")){
         listening = false;
         msgeoutput("Stoping listening");
     }
    }

    public void msgeoutput (String speak){

        System.out.println(speak);
    }

    public void msginput (){
        String line;
        line = ": " + speakline.nextLine();

        stringmanager(line);
    }

    public void programupdate (String msg){
        String line;
        line = "update101 "+ msg;
        stringmanager(line);
    }

    public synchronized void stringmanager (String X){
        String inputstring = X;
        String message;
        Integer inputcode = null;
        if (inputstring.contains("update101")){       //handle system update messages
            message = inputstring.replace("update101","System: ");
            msgeoutput(message);
        }
        if(inputstring.contains("command101")){  //handle program commands
            commandhandler(inputstring);
        }


        for (int a = 0; a <namearray.length; a++){   //u always mess < up
          //if (namearray=null) ... this was an early bug
          if (inputstring.contains(namearray[a])){
              messagearrayfunction(inputstring);
              msgeoutput(namearray[a]+": " + inputstring);
          }
        }



    }

    public void messagearrayfunction (String s){
    messagearray[0]= s;

    }

}


the server socket:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class getconnected extends messagestuff{


    public synchronized void connectionaccepter (){
        while (listening){
          try {
            ServerSocket connector = new ServerSocket(7001,10);
            programupdate("Server socket OK");

            while (listening){
                new threadmaybe(connector.accept()).run();


            }

          } catch (IOException e) {
            programupdate("Error connecting to server socket;\n"+e.toString());
          }
        }
    }
}


the thread running class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class threadmaybe extends messagestuff implements Runnable {

    PrintWriter outbuf;
    BufferedReader inbuf;
    public String name;
    public Socket threadsock = new Socket();
    static Integer nameindex = 0;
    private String ding;

    public threadmaybe (Socket socket){
        this.threadsock = socket;
        programupdate("Socket in thread connected to server socket");
        try {
            outbuf = new PrintWriter(threadsock.getOutputStream(),true);
            inbuf = new BufferedReader(new InputStreamReader(threadsock.getInputStream()));
            name = inbuf.readLine();
            namearray[nameindex] = name;
            nameindex++;
        } catch (IOException e) {
            programupdate("Error connecting streams to Client: " + e.toString());
        }
    }




    public synchronized void run() {

        outbuf.println("Connected to server");
        while(threadsock.isConnected()){
            outbuf.println(messagearray[0]);
            messagearray[0]="";
            try {
                msgeoutput(inbuf.readLine());

            } catch (IOException e) {
                programupdate("input fuckup" + e.toString());
            }



        }
        programupdate("name" + "has left the session");

    }
}


and the main bit just so you can see how I call this sorry program
1
2
3
4
5
6
7
public class mainpart {
    public static void main(String[] args) {
    getconnected listener = new getconnected();
        listener.connectionaccepter();

    }
}


c++ is more exciting than java, I will go back once I feel I can make someting that does what I want I will go back to improving c++, I actually never learned to use parameterized classes and such, that was tottaly the next step, just I moved to other languages


EDIT: oh yeah theres bugs galore in it, i still never got it to work like clokwork, I dont think i ever got anything worling like clockwork, I dont think any programmer in the 21st centuary will (unless hes a clockprogrammer)
Last edited on
The first problem with (reading) your diagram is that you do not name the squares, only say what they do. It's unclear whether you're drawing classes or functions and what the arrows mean (but maybe you have all that figured out in your head).

I suggest starting with one rectangle per class. For example...
                    (waits for connection)      (handles socket io)
 --------               ------------               -----------
|mainpart| --starts--> |getconnected| --starts--> |threadmaybe|
 --------               ------------               -----------
                             |                          |
                          inherits                   inherits
                             |       ------------       |
                              ----->|messagestuff|<-----
                                     ------------
                        (handles console io/commands and state)
Now that's not a lot of detail, but the classes are sort of trivial. For more complicated classes, you could draw logic in another diagram. Possibly quite similar to what you already had.

Although, I don't know if that will help you with designing more reusable classes. You don't have it wrong. One class for accepting connections, one for dealing with them. messagestuff is kind of a pile of functions - I suggest splitting it into a class for program state management (I don't fully understand its purpose. is it program state or thread state? is it supposed to be shared? you might want to do this without inheritance then.) and a class for command processing (I'd add one for console io, but you only use the o part of it, so that would be a silly class).

By the way, for threads to actually run as threads, you want to call their start(), not run(), I think.
Last edited on
Thanks for looking, I might make the exact same thing all over again but plan it better, like that,

yeah I over complicated my thought process yet again, that's why im trying to learn how to do what im doingn, it all gets squiffy, how did you [lot] learn to concentrate on what your doing in your head.

In my head a computer program is like several interacting balls of messed up wool, I need to learn to braid this mind wool :P

Implements runnable means you have to start a thread with run, I think its not different its so you can extend summink else





run() is just a regular method, it only does what you tell it to do. start(), on the other hand, was written to actually start a new thread and call run() on it (I wonder if there is some confusion here about Thread, the class, and thread, a bit of code that acts almost like another process, but shares all of its memory with the current one). If you don't believe me, take a look at http://ideone.com/nNM4uc and http://ideone.com/deS8X7 - because run() does not start a thread, sleep pauses the main thread and "thread" is printed before "main" like a regular function would, and because start() does start a thread, while that thread is sleeping, main gets to print "main" first.
oh :P well if thats the case I guess my program wont support multiple servers...or will it?
so theres no system for designin sturff?


I think VS2012 does a crude version of this.

I think VS 2012 does a good job at that. just look up "visual studio class diagram" in google images.

However, I've only really used it with C# and SQL. I've not tried it with a C++ project yet, nor do I know if it works with C++. Once I get home tonight, I'm going to look at that.

One thing though. That's only for the paid version of VS2012.
Last edited on
I like the VS2012 diagrams. They're handy things and since it's integrated into the IDE you can just double click a class to jump right to its header file.
when you guys were getting better at code, how did you create stronger pictures of how your code is working in your brain
I don't think visually. Not everyone is a visual learner - you might not be one. Have you tried to figure out how you think and learn best?
im trying to now, any ideas of how i can suss out what works the best? I find copying my own interpretation of what im learning in a book and updating it occasionaly, the result is a whole load of tutorials written in a way i can understand.

that helps remember stuff, but doesn't help write larger codes
closed account (3qX21hU5)
The only way I can learn to organize and esign bigger projects which is what I think your asking is to justdo it and learn from my failures. Just keep coding and keep trying to make them big projects you want and after a few years you will have more eexperience at it and it should click for you. You can try a bunch of different methods to organize your code better and some will help a little bit but ultimately it is the experience that will help the most.
@devon

Have you heard of the book "Design Patterns - Elements of Reusable Object Oriented Software" by Gamma, Helm, Johnson & Vlissides aka "Gang of Four".

Here is a web site that gives an overview of the 23 design patterns:

http://www.vincehuston.org/dp/


The book clearly gives a much better explanation.

I also have the Scott Meyers Book "Effective C++".

The latter is a bit of an eye opener - it has a lot of really good ideas that help with writing good code and avoid various traps. It requires a good knowledge of all the aspects of C++.

HTH
http://www.stack.nl/~dimitri/doxygen/

Here's what doxygen did with one of my classes:

http://img20.imageshack.us/img20/4020/classdoxygeninstancecal.png

Doxygen can create awesome png inheritance graphs from your code. It was also so easy to use that I forgot actually how I made that graph (it must had been completely automated, cause I don't remember having to read long any documentation).
Last edited on
Topic archived. No new replies allowed.
Pages: 12