Programming "The right way"... (New programmers should read)

Cynikal

Member
Licensed User
Longtime User
So, to start off, a bit of background.

I am 26 years old, and have been programming since i was about 12, in the days of VB3. My languages go from VB, to VB.Net, to C#, to some Java, C++, and PHP.


Now, I read a post on here from someone asking if code jumping was supported, and knows its not the "Professional" way. I have read similar statements across other forums as well...

To sum it up, there is NO "Right way" of programming.

If it gets you from point A, to point B without erroring out, i'd say you did it the "right way".

Now, the only difference is, is how you got there, and how I would get there.

In smaller applications, efficiency is not really something you need to worry about, so you can afford to take short cuts.

However, lets be honest.

If the project is to "Take over the world"...

My way, can do it in 15sec. Your "incorrect" way can do it in 18sec.

Is 3 seconds enough to raise a stink about? It would depend on who you ask.

Now, I know enough to get the desired result. I know how to troubleshoot to fix what is broken, and I know enough to keep my code organized for easy modification. I did NOT goto college, I did however goto a class when I was in high school, that taught you VB. However, it was stuff i've never heard of, but the outcome was stuff i've done 10 times over.

I remember in class, the teacher was telling us to make an application that does..something (I do not remember).

I programmed it my way, while the rest of the class programmed it..the teachers way.

My way consisted of about 3/4 the code, and no comments.

I finished about 30min before everybody else. And I remember the teacher telling me I was wrong. I also remember getting into a debate with him in class over this.

Program speeds were similar. Code execution on my end was generally quicker (about a second). So overall, mine was better.

Eventually the teacher decided to ask me questions about the code, versus telling me I was wrong.

These are things you should take into account when programming.

Dont worry about doing it "the wrong way", because when someone says you're doing it wrong (but the outcome is the same), it generally means its not how THEY would do it.

The next time someone says you're doing it wrong, but the outcome DOES come out the same(error free), ask them HOW it is wrong? Maybe they see a potential issue given a certain command; Maybe they see unneeded code; or MAYBE its just not how they would do it.


In the wonderful world of programming, it is one persons creation (however shitty it may be), sparks the imagination and creativity of someone else's. This is a form of competition.

If Ford never had competition, we'd still all be driving around in a Model T. Why? there is nobody else to challenge their design.

If Microsoft didnt have apple, we'd still be on Windows 3.5...

Because if we were on Windows 3.5, with 40MHz processors and 2MB of ram, there would be no need to spark for technology advances.


Sorry for the rant, good day.

Good luck in your programming!
 

Cynikal

Member
Licensed User
Longtime User
I just couldn't disagree more with what you have just posted, but then I was only a professional Software and Hardware Project Manager all my working life so what do I know?

Once again, this proved my point.

You tell me i'm wrong. But, you fail to show HOW I am wrong?


With 14 years of programming experience, making hundreds of pieces of software for hundreds of companies. I feel I am qualified to speak on the matter.

So, with what statements do you most disagree with, and WHY? Then we can debate about it further.
 

agraham

Expert
Licensed User
Longtime User
I've proved nothing and no, I'm not going to debate - your outlook is so fundamentally different to mine that it is pointless.

Read some good books that cover topics such as these.

Coding standards.
Version control.
Build management.
Regression testing.
Software documentation.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will add my 2 cents. Best practices are based on knowledge and experience of many developers, developing complex programs.
You should have a very good reason not to follow those best practices. Sometimes you do have a reason not to follow best practices. But these cases should be rare and well explained.

This is especially important when you are involved in large projects made of multiple components.
 

moster67

Expert
Licensed User
Longtime User
Well, I believe one can approach a certain programming task in different ways - the important thing is that the program does what it is supposed to do and doing it correctly.

The performance of the program may of course vary depending on the approach and the hardware involved. If you program an application for a desktop, most PC these days have hardware-resources that would run even a "badly" programmed application real fast. A different story would be if you were programming drivers or other programmes where speed is crucial but then again we would probably be programing those drivers/applicationa in a different language such a C.

While it is true that lately most devices are really powerful, we still have to remember that their hardware resources are not equal to those of a desktop-PC.

As a matter of fact, I have learnt a lot from programming with mobile devices. I remember when I wrote my spellchecker-library, Agraham kindly introduced me to sorting algorithms such a Hash and binary-search and what a difference that made when my library had to search for words in a dictionary with more than 200.000 words, especially on a mobile device with little memory. Also tecniques such as getting a pipe-stream and deal with it in chunks in order to avoid out-of-memory problems (so common with mobile devices).

So, my point is: try to be "smart" when programming with mobile devices. There are many ways to approch a certain task but try to use the more effective ones when programming with mobile devices. Your app may run perfectly on a high-end device but might fail on a slower device.
 
Last edited:

taximania

Well-Known Member
Licensed User
Longtime User
Have you ever tried to decode/debug someone else's source code.
Randomly jumping from place to place makes it very difficult.

Yes, I write programs 'my' way. For 'myself'.

Surely any serious programmer though, must adhere to some code of standards.
Couldn't non standard code cause errors on systems that the programmer hadn't even considered.

Just my penneth'
 

kickaha

Well-Known Member
Licensed User
Longtime User
I do wish that the "(New programmers should read)" part of the thread title could be removed, because they really shouldnt.

I to am a self-taught programmer, but I realise the importance of well formed and well structured code.

When I write a program I like the flow to be obvious and use intelligent variable names to enhance readability.

To partially quote the OP

My way consisted of about 3/4 the code, and no comments.
............
Program speeds were similar. Code execution on my end was generally quicker (about a second). So overall, mine was better.

I know what I think is better.

If any new programmers are reading this, please try to get a grasp of the basics of good programming practice, and use it along with well laid out and commented code. That way when you come back to it (even after a few weeks) you will be able to pick up where you left off. Also other people will be able to follow you code and help with any problems.

Unlike the OP, I am not sorry for my rant.
 

kickaha

Well-Known Member
Licensed User
Longtime User
I was recently asked (on another forum) to make a short block of code easier to understand, it was a 50/50 selector as in "who wants to be a millionaire".

Look at the original
B4X:
Private Sub imgFifty_Click()
imgFifty.Visible = False 'Hide image as user can only use it once

FIFTY:
'Get random number
Randomize Timer
randomFifty = Int(Rnd * 4) 'Only 4 possible answers


If randomFifty = correctAnswer Then 'If random number same as correct then try again as it can't hide the correct answer
    GoTo FIFTY
ElseIf randomFifty = firstFifty Then 'If random number has already been used to remove the first wrong answer the try again
    GoTo FIFTY
Else 'Hide the answer generated by random number
    If randomFifty = 1 Then
        lblAnswer1.Visible = False
    ElseIf randomFifty = 2 Then
        lblAnswer2.Visible = False
    ElseIf randomFifty = 3 Then
        lblAnswer3.Visible = False
    ElseIf randomFifty = 4 Then
        lblAnswer4.Visible = False
    Else
        GoTo FIFTY
    End If
    
    firstFifty = randomFifty 'Put the first random number in here so it isn't used twice
    
    repeatFifty = repeatFifty + 1 'Increment the number to ensure only 2 answers are hidden
   
    If repeatFifty = 1 Then  'Go for second number
        GoTo FIFTY:
    End If
End If
End Sub

Then at my modified version:
B4X:
Private Sub imgFifty_Click()

imgFifty.Visible = False 'Hide image as user can only use it once

firsthide = correctAnswer ' make loop run at least once
Do Until firsthide <> correctAnswer ' cannot be same
    firsthide = Int(Rnd * 4) 
Loop

secondhide = correctAnswer ' make loop run at least once
Do Until secondhide <> correctAnswer And secondhide <> firsthide  ' cannot be same
    secondhide = Int(Rnd * 4) 
Loop

' hide labels of firsthide and secondhide

If firsthide = 1 Or secondhide=1 Then
    lblAnswer1.Visible = False
End If

If firsthide = 2 Or secondhide=2 Then
    lblAnswer2.Visible = False
End If

If firsthide = 3 Or secondhide=3 Then
    lblAnswer3.Visible = False
End If

If firsthide = 4 Or secondhide=4 Then
    lblAnswer4.Visible = False
End If

End Sub

Now both of these "get the job done" and they would both run in approx the same time - but there is a world of difference in the readability.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Couldn't resist but to add my own thoughts to this thread....

Have you ever tried to decode/debug someone else's source code.
Randomly jumping from place to place makes it very difficult.

A lack of comments and control/variable prefixes can also make life very difficult.
I've recently inherited an Access 2003 DB used to manage routine maintenance of machines and a whole host of other things. All the controls use the default names that was provided my MS Access when first created and the majority of the program lacks any indentation. You wouldn't believe how important a simple thing like indentation is until you attempt to modify a program that prolifically uses conditional statements without any :BangHead:
Little things like renaming a button from "Command1" to "btnPrint" makes all the difference to anyone trying to follow the program!

I've around 15 years programming experience across a range of platforms along with an Engineering Degree, and with every project I learn a little more.

I openly encourage anyone to learn to program and to do so in their own way BUT there are definitely right and wrong ways of programming, good and bad styles. This Forum is an excellent place to start and there are lots of people willing to give up their time and experience to assist other users. Andrew (AGraham) has on numerous occasions helped me along my way to becoming a better programmer.

I often use the analogy that programming is like handwriting.... you could ask several people to write exactly the same sentence and whilst they all read the same they could all look vastly different, some may even be almost illegible!

Regards,
RandomCoder
 

kolbe

Active Member
Licensed User
Longtime User
I often use the analogy that programming is like handwriting.... you could ask several people to write exactly the same sentence and whilst they all read the same they could all look vastly different, some may even be almost illegible!

Good analogy... I was kinda thinking we are discussing the difference between the art of programming and the science or programming and finding the happy middle between the two. If you tend to either extreme and you will end up with problems.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
A person who only writes programs by himself and who answers only to himself can do it anyway he wants, but not so someone who is one of a number of people working on the same program or supervising such an effort or even supervising one person who is the only one working on a program which some day someone else may need to work on.

So anyone who is strictly a lone programmer is probably never going to agree with (nor convert to his way of thinking) someone from the corporate environment, and vice-versa.

That being said, and having always been a lone programmer not reporting to anyone else, my experience is that what raises a program to the next level is to constantly improve the design of the program as I come across ways to make the program better which could not reasonably have been foreseen in the design stage.

Compare that to a corporate programmer who probably was not involved in the design stage in the first place, can't see ways to make his part work better with the whole, and if he could, he probably is given no incentive (or even the permission) to change the design to make it better. No amount of banning GOTO or forcing the following of flow charts is going to make up for that.
 
Last edited:
Top