Help me become better at coding large programs

Discussion in 'Programming & Software Development' started by Foliage, Dec 9, 2010.

  1. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    So I've got maybe 4-5 years of coding under my belt, everything from assembly, PIC Micro C, Windows C, C++, C#, Java, VB .NET

    Now a lot of my projects have just been small tools for workplaces to help optimise certain things, usually start off fairly simple then expand into a rather medium sized app with 5-10000 lines of code. Some of the PIC micro stuff is into the 100,000 lines realm.

    Now I find I can code well but due to most of my experience being in coding C my coding style generally consists of multiple classes with loads of static methods. In Java and C# I make use of objects to simplify things but I still use a similar coding style, so whilst the coding is solid, the style isn't.

    I find with the medium size GUI apps I've been writing I generally just have a few large classes with a shitloads of methods and it soons gets out of control, I document and use a huge amount of commenting, variables that are almost sentence long sometimes so you can follow the code, it is just convoluted.

    I never studied any proper software engineering classes, did lots of programming classes but they were all centred around small PIC micro stuff and smaller apps where you can get away with just a few large classes.

    Is there some decent guides on the net to efficient design and which models to use and when, would be nice to get on top of this before it gets too engrained. All of my projects I've basically been the sole coder so I've been able to get away with just doing it my way for all this time.
     
    Last edited: Dec 9, 2010
  2. Primüs

    Primüs Member

    Joined:
    Apr 1, 2003
    Messages:
    3,354
    Location:
    CFS
    It really depends on what kind language your referring to, for instance, Java has Java Enterprise Beans, which can be used in a much different way to a lot of other programming languages.

    Basically I (And the company i work for) stick to a fairly standard rule of thumb, that ever table in your database (Assuming storing data and manipulating it in a DB), has an object of its own, with its own modification methods, so if you need to modify x object, the method to modify it is part of class x. Still can get a bit messy though.

    I'll be watching this thread as I am also interested to hear others take on the subject.
     
  3. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    Asking in relation to .Net languages and C mainly, most my C programs are large console or microprocessor apps, .NET are usually 1 page GUI apps with a large backend doing database stuff.
     
    Last edited: Dec 9, 2010
  4. Primüs

    Primüs Member

    Joined:
    Apr 1, 2003
    Messages:
    3,354
    Location:
    CFS
    While .Net is my current role, I'm not as clued up in proper practises with it as i'm still learning it (coming in easy though :)). The biggest application i had to maintain was Java based, using a fairly decent structure, but the structure meant nothing to the age old 'mega classes' that the boss wrote after a 1 day java class. They were impossible to maintain.

    Apparently the whole thing has been scrapped now anyways, so there goes that many years of work!
     
  5. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    Yeah mega classes are my problem, I need some sort of model to adhere to to layer everything properly.
     
  6. tobes

    tobes Member

    Joined:
    Dec 23, 2001
    Messages:
    3,476
    Location:
    Melbourne
    Mega classes are a sign of not enough design abstraction.

    You really need to focus on getting different functionality into different classes. It kind of sounds a bit like there may not be enough upfront planning going into the projects your'e working on. (probably not your fault) And a bit of upfront design (and capturing all the requirements) will help break the projects into smaller chunks.

    If you're ending up with classes that have more than a few thousand lines of code then it probably needs some restructuring.
     
  7. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    Most of the projects are small tools that when the upper management are impressed with request more features until it is 10x as big as initially requested, however even if given all of features in advance I am not sure it would have been that much better designed. Need some decent guides on how to break it down into layers and sections.
     
  8. SaTaN

    SaTaN Member

    Joined:
    Jun 18, 2002
    Messages:
    4,784
    Location:
    Caulfield-ish
    think about it like this... when your C program is big enough to be in more than 1 file you end up making descisions on which file to put stuff in right? and when it doesnt really fit in any existing file you make a new one (or when it fits in two you might refactor a bit to clean up the interface). Ditto with classes then.

    you say you have mammoth classes? have a look and figure out why it is so large, chances are it is doing more than it should be, split it into 2 and use proper abstraction to make it clean.

    if you are allowed to post code then do so and I'm sure you'll get 4 different recommendations to clean it up....


    with OO and java/c# especially, if you have less than 15 layers of classes before getting to the actual logic you arent doing it right :p
     
  9. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    I'll post up the source for one of the projects, it is basically no use to anyone but my company so there's no issue here, just an in house tool. Basically the tool is used to synchronise units, descriptions and high and low limits between a scada server (stores values from gas wells etc) and the scada history server (archives values every 5 seconds for 10 years).

    If you guys don't have visual studio just open form1.vb

    Yeah I know, I should really rename that lol, this is probably my worst example of a mega class, also my first time coding in VB.net as well (forced to use it here).

    All my Java and C projects have up to 20 classes, this is the one that had intense feature creep, however it also has some fairly complex logic in it so it really does need to be cleaned up.

    http://rapidshare.com/files/435795206/HistorySync.zip
    [​IMG]

    I am thinking the first step is to abstract it into two classes, the GUI (all form and user stuff) and all the backend processing, could hopefully further abstract it from there, however it is quite likely the way I've coded it will make this difficult to separate, let me know if you find any obvious bad coding or just general bad practice things in there.
     
    Last edited: Dec 9, 2010
  10. SaTaN

    SaTaN Member

    Joined:
    Jun 18, 2002
    Messages:
    4,784
    Location:
    Caulfield-ish
    oh goody VB! :p (rar is corrupted or something, its not exctracting here)

    complex logic? so presumably you have nasty classes that get a bunch of values and in the same class mess around with them? easy fix is pull the the nasty logic out into a separate class and then just instantiate a instance of it in the first class (or even give it a static function to do the logic)
     
  11. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    Try now
    http://rapidshare.com/files/435795206/HistorySync.zip

    Complex logic as in the database access is tremendously slow so I read it once, save a lot of stuff in memory and then work with that until the user figures out what it wants to change, then I write it all back. Also have infinite undo history which means a few weird if else clauses in the code.
     
  12. SaTaN

    SaTaN Member

    Joined:
    Jun 18, 2002
    Messages:
    4,784
    Location:
    Caulfield-ish
    VB.... :'(

    http://en.wikipedia.org/wiki/Model–View–Controller is probably a good start if you havnt come across that yet.

    "Also have infinite undo history which means a few weird if else clauses in the code"

    I think they call that a bad smell in the refactoring world... I would put the undo manager into a separate class entirely with two public methods (push_action, pop_action)
     
  13. Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    9,447
    Location:
    Sydney
    the interface and the model are pretty integrated here. to illustrate, you would nearly do a complete rewrite to convert it to a website. moving the model out of the interface is one way to tidy it up.

    i wouldnt be too hard on yourself tho. if it works then dont change it. you only add more bugs doing massive rewrites.
     
  14. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    yeah it works flawlessly currently, but I have some spare time and would like to do another version that is coded better just for future practice.

    Have heard of the MVC model before, will do some reading.

    It is honestly not that bad, identical to C# just different syntax, very very fast for coding up windows applications, also runs faster than Java on a windows machine, has come a very long way since VB6.
     
    Last edited: Dec 9, 2010
  15. TheChemist

    TheChemist R.I.P

    Joined:
    Apr 5, 2007
    Messages:
    1,035
    In .NET, you can use partial classes to keep code in nice chunks.

    So if you have a class with 30 different methods, you can keep the code in 6 files with 5 methods in each, or whatever.

    Also, proper use of delegation can be a good way to de-tangle stupidly complex logic (ifs, nested ifs, etc) and can make it much easier to add new features.


    Oh really?

    c#

    int a=0;
    double b=0.5;
    int c = (int)(++a * ++b) << 10;

    VB

    Dim a As Integer = 0
    Dim b As Double = 0.5
    Dim c As Integer = CInt(Math.Truncate(System.Threading.Interlocked.Increment(a) * System.Threading.Interlocked.Increment(b))) << 10


    If you think VB is "very very fast" wait until you use c#
     
  16. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    Uh I said its identical just different SYNTAX, you edited my quote lol, yes the syntax is different but .NET VB and .NET C# compile to almost identical code and use the same .NET code base hence very similar speeds.

    They are essentially identical because they use the same .NET code base.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    you can even convert directly between VB.net to C#.net and back again using this tool.

    Some people even think vb is better than C#
    http://www.vbrad.com/article.aspx?id=65

    personally I prefer the C# syntax but honestly, there is absolutely zero real world difference between the two.
     
  17. tobes

    tobes Member

    Joined:
    Dec 23, 2001
    Messages:
    3,476
    Location:
    Melbourne
    Having worked at a company that uses a Mix of VB and C# (in the same product) I give these broad generalisations:

    • C# Closer to a hardcore programming language, much stricter.
    • VB for lazy coders its better because they don't need to do strict type checking and all that sort of thing.
    • VB tends to be used by those simply concerned with squeezing a feature/whatever out not concerned with writing clean code etc.


    dons flame suit.
     
  18. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    Actually the type checking is identical between the two, same with strictness, they are identical. VB6 was a lot different, all the bad wrap VB.net gets comes from VB6 and prior versions, they were rubbish and suffered from all the things you mentioned.

    I agree with the lazy coders comment though, everyone who codes here are lazy and hence used VB all their lives and continue to, there is only a few of us that do C and asm coding, though we aren't a software company so it isn't a big deal.
     
  19. tobes

    tobes Member

    Joined:
    Dec 23, 2001
    Messages:
    3,476
    Location:
    Melbourne
    Hmm I think I'm thinking of type casting not checking. VB is implicit where as C# needs to be explicit?
     
  20. OP
    OP
    Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,058
    Location:
    Sleepwithyourdadelaide
    If you set Option Strict Off then it can be implicit but any sane programmer always turns option strict on.
     

Share This Page