Archive

Posts Tagged ‘programming’

Importance of Structure and Coding Etiquette

August 7th, 2009 admin No comments

Well, it’s been a very long time since I last updated and I’d like to apologize to all my subscribers for that. I’ve been very busy, but it seems the work load is going down and I’ll have more time to continue writing! Now, on with the article.

So recently, I have just finished a project where one developer had started and then decided he could not finish the work, so I was hired to finish it. The natural thought to one who is inexperienced is, “This will be a cakewalk. Most of the programming is already done!” – wrong. The first thing that went through my mind was, “I wonder how bad this really is.” So, I accept the project (as I had only a few projects at the time) and take a look.

The code was atrocious to say the least. I felt as if this other developer had never learned how to use comments or his tab key/space bar to format. Most of the time on the project was bent around figuring out what the original developer had tried to do. It was a nightmare.

As I started digging through files and files of unnecessary sloppy code, I thought to myself, “I need to write something about this. This kind of work needs to stop.” I was not upset because of the amateur programming, nor the fact that it was undocumented and poorly written. What bugs me is the fact that someone paid for that kind of work. It looked like the developer copy/pasted everything from snippets he or she found online. That being said, one must learn the importance of structure and coding etiquette.

Structure is important for general organization. It keeps code neat and clean looking and much easier for anyone, to include yourself, to go back and fix errors/security holes. Most people see structural formatting as a simple aesthetic quality when in reality it is like formatting a letter. The structure keeps things organized and understandable on a more universal level.

Coding etiquette, on the other hand, is something learned over a long period of time. No new developer can simply logon and expect to program to the standards set right now, but at the same time should begin mimicking the styles of major developers. Examining the work of others is one of the best ways for any developer to learn, so studying (yes, just like in school) the work of past developers, and prominent works of today, one can easily understand how to program professionally. A simple example would be to write functions rather than hardcode functions multiple times. Or rather than using raw MySQL functions, create an SQL wrapper to execute the functions.

There are many resources on learning how to program professionally, and be neat, but it’s up to developers to use the tools. The vast majority of developers, I would say, hold to the standards. However, for those who do not, they are just ripping off their client in the long-run.

Regards,
Dennis M.

The For(); Loop

May 13th, 2009 admin No comments

Many new programmers are perplexed by the “for” loop. However, it is one of the most essential and powerful tools in any developer’s arsenal. Without it, efficient and clean programming would not be as possible. Early on, most developers seem to stick to the while(); loop. I mean sure, while(); is great for some things, but may not exist in some programming languages and not to mention won’t work for everything you need.

Let’s begin by going into the basics about the for(); loop. Well really, a lot depends on the programming language (I will provide both PHP and C++ examples in the source file). But more or less, the for(); loop is simply iteration. It will run until its defined limit is hit providing whatever results you wish. We’re going to do this in more standard languages (e.g.: C/C++ and Java) instead of PHP for mass compatibility. Just note, if you’re looking to do this for php, you do not need to define the type of variable, and your variable will just start with the “$” symbol. So for example, PHP would be for($i=0;…;…){}.

Now we’re going to write a sample program and break it down:

for(int i=0;i<=5;i++){
std::cout<< "This is line number "<< i << std::endl;
}

Now as most of you may have noticed, this is a C++ example, but will effectively explain how the loop works for ALL programming languages!

int i=0; – This simply defines the variable and starting place (0 in this case). In PHP you would simply use $i=0; and so on for the rest of the statements.
i<=5 – This tells the loop when to stop (This <= symbol means less than or equal to). When i>5, the loop will stop and proceed on to the rest of the code.
i++ – This tells what the loop should do after every time it is run. In this case, we’re using i++ which means to add 1 each time around.

Now those are the basics to the loop. What lies within is just simply the code to be executed, which could of course have conditionals and such within it as well (e.g.: if(), etc.). So now we’ll move on to a more complex example.

unsigned int choices=5;
char* choice = new char[choices];
for(int i=0;i<=choices;i++){
std::cout<<"Please enter a value: ";
std::cin>>choice[i];

if(!choice[i]){
std::cout<<"No value for choice "<< i<< std::endl;
}
}
for(int i=0;i<=choices;i++){
std::cout<<"Choice "<< i<<" was "<< choice[i] << std::endl;
}

This will store an array of choices and then after entering them, they will be printed back!

In the source below, all working examples will be provided!
For(); Loop Tutorial

So, that basically wraps everything up! If you need anymore clarification, just let me know.

Regards,
Dennis M.

Categories: C/C++, Java, Other, PHP Tags: , , , , , ,

C++ Memory Management Tutorial

April 14th, 2009 admin 1 comment

This is the second article in the series that I’ll be working on! For those of you who do not already know the basics of C++ and how to compile, etc. I’ll go through a quick run through, but you should visit other sites (Google is a good resource) to find more information! The basics of compilation will be listed below this tutorial! As usual, there will also be a link below to download the whole script as well as compiled binaries!

Here is the main.cpp file:

/**
* Memory Management Tutorial by Dennis M.
* (C) 2009 Dennis M. All Rights Reserved.
*
* This is the basic file to making everything work!
* This isn’t a “basic” tutorial, so it will be less in
* depth than what many may be used to from me.
*
*/

#include <iostream>

using namespace std;

// We’re keeping this simple, so we’re going to keep everything
// in just one main() :)

int main(int argc, char* argv[])
{
/**
* Some memory allocation:
*
* float naturally uses more memory than int and is slower
* but in many cases is more effective from a personal view
* so we’ll use both here.
*
* This is how it works. We’re going to request memory allocation
* BEFORE we actually use it! We’ll allocate 10 bytes to the float var
* and 90 bytes max for the int!
*
*/
float* val1 = new float[10]; // Handle all numbers & initiate it as 0
int* val2   = new int(90); // Start it as 90

cout<< “Value 2 + 67 = “<< *val2+67 << endl << endl;

// Try to allocate too much :P
// the nothrow doesn’t allow this to be allocated if the system
// doesn’t have it..
int* nogo = new (nothrow) int[999999999];
if(!nogo){
cout<< “Your PC doesn’t have enough Memory Available! =-O *shocker*” << endl << endl;
}

/**
* Just some other simple work:
* Float Addition! :)
*
*/
*(val1+1) = 15.123097;
*(val1+8) = 1000.203998124;
cout<< “val1[1] (“<< *(val1+1) <<”) + val1[8] (“<< *(val1+8) <<”) = “<< *(val1+1)+*(val1+8) << endl;

// Free all the memory we’ve used
/**
* Delete the arrays: where we used allocation – e.g. delete [] VAR
* Delete the val2 normally :)
*
*/
delete [] val1;
delete val2;
delete [] nogo;

// Set everything to 0!
val1 = 0;
val2 = 0;
nogo = 0;
}

Now that’s all there is to it really. The code is pretty well documented and as always is much of the tutorial! Nothing I really need to explain in it seeing as this is a mainly intermediate tutorial.

As I promised, some compilation techniques:

For most *nix systems (must have gcc installed)

g++ main.cpp -Wall -o mem_management

And for Windows users, I suggest using either Visual C++ by M$ (which the compiler is visual and self-explanatory) or a personally I enjoy Ming (with MSYS) or CyGWIN. With either of those installed, run the command mentioned above for the *nix users and it should all run ok!

As usual, the download link is below:
Memory Management Tutorial Download

Keep an eye for the next few tutorials in the series listed a few posts ago! They should all be great ones!

Regards,
Dennis M.

The Next Big Update: What to Look For

April 6th, 2009 admin No comments

Hello everyone,

Just an update here talking about the next big update that I have planned for you guys! But first, I’d like to remind you all of the recent “PHP Password + Salt + Encryption” Tutorial that I recently posted yesterday! It is ready for you all to go take a look. So anyone interested in that, might want to go take a look at that ;)

Alright, back to business. So this next update will be broken up. It’s going to be HUGE! Over the next week (in any order, not necessarily how I have them posted here) you should expect extensive posts about the following to be up:

  • Simple C/C++ Memory Management Tutorial
  • The Importance of RSS Feed and What It Is
  • Creating PHP Classes and How to Use Them Effectively
  • What is CGI/Perl and How Could it be Useful
  • The Basics of Working within a Linux Terminal

Like I said, I’m going to be trying to get these all ready and posted for you guys within the next week or two. Also, as arbitrary as these articles may seem, they come from all of you who have written to me asking for these things. These are among the most popular articles, so please feel free to drop me an e-mail and ask more questions that I could answer!

The e-mail is: dennis@microsonic.org

Thanks again,
Dennis M.