Code files question

Discussion in 'Mac Programming' started by abcdefg12345, Jun 28, 2014.

  1. abcdefg12345 macrumors 6502


    Jul 10, 2013
    Just wondering if its better to have lots of .h and .m files or is it better to merge them all in into bigger files i currently have 40 files in an app and need to add more codes should i add more files or should i try to merge some of them

    iv got between 800- 5000 words in each .m file should i make them smaller or is it fine to add more codes to them

    what is better for apps too much code in few files or smaller amounts of code in more files
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I would aim for smaller files, to the extent that this makes sense. It's not very helpful, but files generally map to a class or grouping of functionality. As such, you should have exactly what you need to do one thing well. If you're shoving too much code into one file it MAY indicate an over-complicated object design that could be broken down into simpler, single-purpose objects that compose the larger piece.

    Don't consolidate for no reason.

  3. ArtOfWarfare macrumors G3


    Nov 26, 2007
    First off, nobody talks about the number of words in a source file. They talk about lines, columns, or sometimes statements or functions or classes.

    My rules of thumb are:
    - Aim for each line to be under 80 columns wide - situations may arise where that isn't possible, though. Don't ever let a line be over 120 columns wide though - find a way to make the line shorter. IE, shorter variable names.
    - Aim for each file being between 200 and 2000 lines long. Situations may arise where that isn't possible - make sure to keep related functionality together. Generally a well organized project will have the file line counts within that range.

    Although really, the entire concept of files is just silly when you're talking about programming. Light Table looks like an interesting IDE... it looks like it's actually ready for Python development so maybe I'll give that a spin right now...
  4. larswik macrumors 68000

    Sep 8, 2006
    I started to read a book a while back called "Clean Code", I need to get back to it again. The book covers how to write good code and teaches good habits.
    I found myself over the years understanding code pretty well but writing it looked really bad. I would come back to projects that I had not worked on in 6 months and be confused as to what I was looking at even though I wrote it.

    I used the same code over and over in methods to do the same thing. My methods and functions did more then one thing. For instance I would have a method generate a random number and the same method would process that data even further. Now I write multiple methods or functions to perform the task that 1 method did before. So now I would have a method just generate a random number and return it. It makes my code cleaner and easier to read.

    For code that is used by many different classes like reading and writing to the drive, I would create a BoilerPlate class where I would have those methods be accessed.

    I don't know what is the right way to write code and break things down. I just strive to make the code clean, short, understandable and as abstract as possible.

Share This Page