10 Replies - 3510 Views - Last Post: 20 September 2010 - 01:52 AM

#1 Eclipse Reborn  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 135
  • Joined: 26-June 10

How do you keep your code clean?

Posted 12 July 2010 - 05:30 AM

Any tips on how to keep your code, maintainable, clean, easy to read. Whenever I try to update a program of mine, I usually break something. I use comments regularly, functions, etc.
Is This A Good Question/Topic? 0
  • +

Replies To: How do you keep your code clean?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: How do you keep your code clean?

Posted 12 July 2010 - 05:31 AM

Moved to C++ Programmers.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: How do you keep your code clean?

Posted 12 July 2010 - 05:55 AM

*
POPULAR

Off the top of my head:
  • Avoid globals
  • Never cut and paste where a method will do
  • DRY: Don't repeat yourself
  • Methods should only be smart enough to do a simple task.
  • Never give a method more information than it needs to do it's task.
  • Use const when you can.
  • Use meaningful names.
  • Don't be afraid of extra classes or files, if needed.
  • Refactor: just working isn't good enough, periodically go back and clean it up, applying the above.

Was This Post Helpful? 13
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How do you keep your code clean?

Posted 12 July 2010 - 10:02 AM

Using a code formatter (many IDEs have one) such as artistic style can quickly "beautify" your code formatting which can be a bit help. Since it is a tool it does not suffer from human error (at least it never has for me).
Was This Post Helpful? 2
  • +
  • -

#5 c0dy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 29-June 10

Re: How do you keep your code clean?

Posted 12 July 2010 - 03:30 PM


Breaking a program by updating it shouldn't be blamed on the
layout of the code (I'd think). If that's happening then it's
a design flaw.

As far as keeping code neat... I am a bit obsessive-compulsive
:( I tend to be pretty anal on code formatting but I don't tend
to care how it's formatted as long as it's neat. Planning your
program with diagrams and pseudocode helps. You're less focused
on getting your program to work and more focused on laying it
out in a logical way.

Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: How do you keep your code clean?

Posted 12 July 2010 - 07:54 PM

Eclipse Reborn said:

I use comments regularly, functions

code should be understandable without comments
comments should not be large (any change of code cause rebuilding of its comments)
if you think how works the code - it's a complicated code (read it only one time and when code is clear, you will understand it by one read)

waste ten hours(continuous, one hour per day, perhaps) for commenting everything to get up your skill
don't be lazy and forget to training yourself
Was This Post Helpful? 1

#7 OnlineCop  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 16-April 10

Re: How do you keep your code clean?

Posted 14 July 2010 - 08:11 PM

I put all of my non-local #include's into a single file. All files can safely #include that one file, and not worry about circular inclusion.
// main.h
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>



// file1.h
#ifndef __FILE_1
#define __FILE_1

#include "main.h"

#include "other_local_header.h"
...

#endif // __FILE_1



// file2.h
#ifndef __FILE_2
#define __FILE_2

#include "main.h"

#include "file1.h"
...

#endif // __FILE_2



Then, if you have anything that requires #define's in a certain order (like before #include'ing a specific library), you don't have to worry that two files define them in a different order, causing headaches (for example, #define DONT_USE_UNICODE 1 before calling #include <locale.h> can make a big headache, if somewhere else in your code, [code]#define DONT_USE_UNICODE 1[/il] isn't defined until after that gets #include'ed).

All local headers (not found in the compiler's /include path) should be explicitly included in each file that needs them.
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: How do you keep your code clean?

Posted 14 July 2010 - 11:16 PM

*
POPULAR

Not the best idea OnlineCop. Why put all non-local headers in another header? That's a bad practice. Think about, if in one source file or header file you only need <iostream>, you include that whole header file containing <vector>, <string>, <fstream>, etc ... Therefore, you include a bunch of non needed headers, which will drastically slow the compile time in a bigger project. In fact, including header files in other header files is considered a bad practice. It slows compilation time.

This post has been edited by sarmanu: 14 July 2010 - 11:27 PM

Was This Post Helpful? 5
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: How do you keep your code clean?

Posted 15 July 2010 - 04:46 AM

I agree with sarmanu.

As a rule, you put as little in the header files as you can get away with. You also avoid any "using namespace" declarations up there. So some extent, you can go nuts with the includes and the namespaces in the source file; that can live alone in it's own comfortable little world.

In C/C++, files become a primary organization mechanism. In C, files can be almost like OO classes, isolating logic by task and sometimes a particular instance type. Files should be viewed the same as functions and classes; only give them what they need to do their job and no more.

The reasoning behind this isn't just compile time. If you make a change to your large program, you need to be able to pin point what elements are affected by the change. Putting all the headers in a header is kind of like using globals and is bad for similar reasons.
Was This Post Helpful? 2
  • +
  • -

#10 Eclipse Reborn  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 135
  • Joined: 26-June 10

Re: How do you keep your code clean?

Posted 16 July 2010 - 05:36 AM

Thanks for all the tips, appreciate them.
Was This Post Helpful? 1
  • +
  • -

#11 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: How do you keep your code clean?

Posted 20 September 2010 - 01:52 AM

One thing I find helpful is creating a file Globals.cpp, to put any globals I need.
(except on rare occasions like a mutex only needed in one file)

I never include directly the file but instead use extern to access them when needed, this make using them obvious.

This keeps them all in one place and helps to keep track of any which are unnecessary and later too work on removing them. Its good when you are doing a lot of trial and error.

I know Globals shouldn't be used but I found this as good way to keep track of them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1