News: How to Write Better Code

How to Write Better Code

Programming is an essential thing in hacking/pentesting, and at times, I do not feel that it is being addressed enough here, at Null-byte. I understand that this is a forum dedicated for hacking, but I'll address some of the stuff I feel is muy importante about programming. I am not a professional programmer, but I do have experience and I do want to share some of the stuff professionals have taught me.

1. Comments

Always write comments.You want the person to know what you are doing and how they can replicate it. You do not want to tell a story, but provide a clear and concise explanation that does not overly explain.

NOT A GOOD EXAMPLE:
//Hey guys, so I'm going declare x as an int and give it a value of 2

GOOD EXAMPLE:
//Declare int x with given value of 2

2. Organization

Give your variables ,classes, objects, etc. concise names. This shows organization and unity in your code. If you are writing code in an object-oriented programming language, such as C++, this is very important especially working with inheritance and stuff like that.

NOT A GOOD EXAMPLE:
struct CAT{
int Sheperd;
string Bob;
double hello;
}

GOOD EXAMPLE:
struct PERSON { // Declare PERSON struct type
int age; // Declare member types
long ss;
float weight;
char name25;
} familymember; // Define object of type PERSON
(from https://msdn.microsoft.com/en-us/library/64973255.aspx)

3. Names

I guess this is more of a follow up to the second prompt. Give your vars, classes, objects, etc MEANINGFUL names. If you are going to make a variable. After that, we want the user to input their age, and then their age is stored in that variable. Isn't it logical to call the variable "age"? Its not so long, its meaningful, and its logical. Why not?

NOT A GOOD EXAMPLE:
"Im working with a program that prints out the size of a string. Here is my string variable":
string thing;
or
string somethingThePersonWillEnter;

GOOD EXAMPLE:
"Im working with a program that prints out the size of a string. Here is my string variable":
string userInput;

4. Code Review

Stackexchange has a good code review forum that helps with code review. They will catch bugs, and help clean up your code, throwing out unnecessary stuff, as well as help make your code much more effective. Even if you don't like stackexchange (ik ,kinda strict) there are still A LOT of people out there willing to help review your code.

Conclusion

Well thats it for that. I guess in the end, these tips are just common programming practices just about everyone should know.

Thanks for reading, and stay tuned for more!

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

4 Comments

I would add:
-Error Handling
-Use of modules and routines

Good quick guide, it's important to get into good habits especially with code.

ghost_

Nice guide. I also find it helps to read other peoples code(from a Git repository for instance), and try to understand it.

Please I'm just a beginner I need someone to teach me a lot of hacking method and I'm a fast learner

Share Your Thoughts

  • Hot
  • Latest