Trying to get into good code habbits

Trying to get into good code habbits

Author
Discussion

Mr Happy

5,695 posts

220 months

Wednesday 18th March 2015
quotequote all
I agree with the comments above. When commenting, less is (usually) more.

You're writing a program, not a tutorial on programming. Once the basic concepts are understood you can pretty much 'read' any type of programming language, therefore there's no point in explaining the basics in your programs.

To be honest, I found it hard to see the actual code in the text above!

I know it's a heavily Windows-biased language (although there's always the Mono implementation), but C# is very syntactically-similar to Java, so if you want to step up from Python, then either MS Visual Studio Community Edition (for C#) or Eclipse (for Java) are both good, free development environments and are both well supported by online tutorials as well as the good old-fashioned paper based formats.


Edited by Mr Happy on Wednesday 18th March 00:12

dcb

5,834 posts

265 months

Wednesday 18th March 2015
quotequote all
Mr Happy said:
I know it's a heavily Windows-biased language (although there's always the Mono implementation), but C# is very syntactically-similar to Java, so if you want to step up from Python, then either MS Visual Studio Community Edition (for C#) or Eclipse (for Java) are both good, free development environments and are both well supported by online tutorials as well as the good old-fashioned paper based formats.
From Python, I'd go to Java, instead of the closed proprietary world of C#.

Free implementations exist on all Linux and many other platforms for Java.

After Java, try C or C++.

While C is described by a thinnish 250 page or so book, C++ takes over 900 pages and is rightly rated a complex programming language.

Goaty Bill 2

3,407 posts

119 months

Wednesday 18th March 2015
quotequote all
There is some really top advice above.

I work with two distinctly differing language types; procedural and object orientated.
My core experience lies in PL/SQL which was fundamentally a procedural language for many years, but has developed to include full object orientated features and support, and I also work with Java and XML.

While in the end, even the most strictly object orientated designed programs will boil down to some procedural statements somewhere, the approach and conventions will vary widely, though many good practices are fully applicable to both.
People that have worked strictly procedural or 'oop' can learn something from each other as well.

I find that for all the graduate programmers today; the greatest problem lies in a lack of understanding of good code / program design understanding on the part of the majority of coders.
(Frankly I find this with many of the experienced as well. They become comfortable and don't bother to learn and expand, and are actually frightened by change)
But for our newer crop; this often comes down to the standard of tutors and probably much more so to the curriculum.
For example; when my son was in college, he was given a whole day to discuss and 'learn' object orientation.
He was then 'thrown' some Visual Basic assignments and basically told to 'get on with it'.
Some reading those statements will be shaking their heads, others laughing, still more crying I suspect; yes managers, these kids are coming your way soon! eek

[rant on]
Sadly the majority of people are in our business because someone said that there was a need for people in 'IT' or 'IT pays well' and see it almost as 'get rich quick' or another 'job for life'.
Just as years ago it was; become an accountant or a lawyer.

Truth be told, you can no more 'make' a good developer out of everyone you meet any more than you can 'make' just anyone a good doctor (or good lawyer or accountant for that matter).
[/rant off]


If you one day make the foray into the OOP world of Java, C++ or their ilk, I can recommend no better book than Design Patterns; (Wiki)

It will not teach you how to code, or teach you a particular language; it will help you learn how to think about your code.

There are many many coders, there a few good coders, there are a very select few with the vision for comprehending a good design, and fewer still with the vision to create one.


OP, if you are doing this for the love of it; there is almost no chance of your failure. Some people were born to design, code, create programs or chunks of programs. The rest were not.
Aptitude, passion and a desire to learn will bring success to almost anyone who wants it.


GnuBee

1,272 posts

215 months

Wednesday 18th March 2015
quotequote all
dcb said:
From Python, I'd go to Java, instead of the closed proprietary world of C#.

Free implementations exist on all Linux and many other platforms for Java.

After Java, try C or C++.

While C is described by a thinnish 250 page or so book, C++ takes over 900 pages and is rightly rated a complex programming language.
I kind of see the point but "closed proprietary" has perhaps the wrong connotations here; it's a language targeted at the most prevalent operating system out there across both personal and business use cases.

Then I struggle to agree... So Java and C++ are both OOP, C is not. OOP is the next logical step to take and I'd really struggle to recommend, for example, C++ over C#. Sure if you enjoy sticking pins in your eyes then C++ could be worth it but for a new person pushing OOP around I'm really not convinced.

Java I'm automatically pre-disposed to hate; if you could see the hoops we have to jump through in order to support the alledged OS, client agnostic panacea for all coding ills you'd possibly understand my viewpoint.

I sound like a C# fanboi - and to a certain extent I am. The language I enjoyed the most was actually Borland Delphi - which was an OOP based on Pascal. I've done the C and C++ thing and I'd take C# over either any time - I'd almost even take VB.NET over them.



98elise

26,589 posts

161 months

Wednesday 18th March 2015
quotequote all
scorp said:
Way too many comments.


i = True #Set variable to True


hehe
Also comments are better if they explain WHY something is done, not WHAT its doing. Anyone that can read code can see what its doing.


SystemParanoia

Original Poster:

14,343 posts

198 months

Wednesday 18th March 2015
quotequote all
This thread has turned out to be an absolute gold mine!! Thanks guys.

Today I have mostly been writing task automation scripts to use on the works computer using sikuli.

Group policy on the active directory works machines is pretty tightly locked down. No bat/exe/MSI executions allowed without admin.. Java is completely unrestricted however. Meaning I could install the sikuli java/python environment

annodomini2

6,861 posts

251 months

Wednesday 18th March 2015
quotequote all
Shame there's no popcorn smiley.

Goaty Bill 2

3,407 posts

119 months

Wednesday 18th March 2015
quotequote all
GnuBee;

I tend to agree with a lot of what you say regarding the language specifics.

C# would be too proprietary for me, simply because I never want to work in any form of MS development aside as an enhancement to my main focus. That however is me, and my personal prejudices. Honestly I know little of the language to really comment further on it's qualities as a language.

C++ ? Good analogy biggrin I have had the pins buried deeply in my early days, it's a wonder I can see at all. I don't even need to mention 'pointers'. Not for the rookie developer IMO.

Java, well I can't argue there either; it runs like a lame dog on every platform I have seen it deployed on, or at the least much slower than many other options would have. That and the tendency for the vendors (past and present) to deprecate methods (thereby breaking perfectly servicable programs) make it frustrating as Hell sometimes.
But, (there had to be one), Java is a powerful, fully featured, mature oop language and does have the advantage of allowing one to learn good object orientated design and development principals without the need to choose a particular platform.
Of course, C++ has the same qualities (as Java) in principal, but we are back to 'pins in the eyes' again smile


Personally, I think oop is a bit advanced for a new developer in any case. But many a program (rightly or wrongly) has been coded using the built in objects or libraries as simple utilities, avoiding all sub-classing and the like, with the end result being a straight forward procedural program (or new utility).


Mr Happy

5,695 posts

220 months

Wednesday 18th March 2015
quotequote all
dcb said:
Mr Happy said:
I know it's a heavily Windows-biased language (although there's always the Mono implementation), but C# is very syntactically-similar to Java, so if you want to step up from Python, then either MS Visual Studio Community Edition (for C#) or Eclipse (for Java) are both good, free development environments and are both well supported by online tutorials as well as the good old-fashioned paper based formats.
From Python, I'd go to Java, instead of the closed proprietary world of C#.

Free implementations exist on all Linux and many other platforms for Java.

After Java, try C or C++.

While C is described by a thinnish 250 page or so book, C++ takes over 900 pages and is rightly rated a complex programming language.
C# has recently been open sourced by Microsoft, as well as 3rd party implementations like Mono ( http://www.monodevelop.com/) even allowing cross-compilation into Apple (both desktop and iOS), as well as Android... for a price. http://xamarin.com/platform

http://blogs.msdn.com/b/dotnet/archive/2014/11/12/...

I agree though, at the minute it is a very Windows-centric language, but that will change. If the OP jumped into Java, then C# is easier to switch to than C++, however C++ is the more powerful language.

I wouldn't recommend that a Python hobbyist jump straight into C++, as soon as you try moving away from the command line, it gets very complex, very fast.

Mr Happy

5,695 posts

220 months

Wednesday 18th March 2015
quotequote all
GnuBee said:
I kind of see the point but "closed proprietary" has perhaps the wrong connotations here; it's a language targeted at the most prevalent operating system out there across both personal and business use cases.

Then I struggle to agree... So Java and C++ are both OOP, C is not. OOP is the next logical step to take and I'd really struggle to recommend, for example, C++ over C#. Sure if you enjoy sticking pins in your eyes then C++ could be worth it but for a new person pushing OOP around I'm really not convinced.

Java I'm automatically pre-disposed to hate; if you could see the hoops we have to jump through in order to support the alledged OS, client agnostic panacea for all coding ills you'd possibly understand my viewpoint.

I sound like a C# fanboi - and to a certain extent I am. The language I enjoyed the most was actually Borland Delphi - which was an OOP based on Pascal. I've done the C and C++ thing and I'd take C# over either any time - I'd almost even take VB.NET over them.
+1 on all of that! Delphi was great, way more powerful than VB6 and way more intuitive than VC++! It was my default language back in the late 90s.

C# is my weapon of choice at the moment as well, it's so versatile (ASP.net/Razor, Windows and thanks to Mono etc, beginning to be useful for Linux as well). The only negative I have is that it is very easy to inadvertently open-source your work by releasing it online. Tools such as dotPeek make the process of decompiling way too easy, unless you pay ££££ for high-end obfuscation tools.

grumbledoak

31,532 posts

233 months

Wednesday 18th March 2015
quotequote all
Goaty Bill 2 said:
Honestly I know little of the language to really comment further on it's qualities as a language.
Remember Sun workshop? Quite. Sun were rubbish at compilers and didn't get any better at virtual machines. When MS's VM outperformed theirs on the PC, Sun tried cheating the benchmarks! It won't surprise you then, that C# is what Java would have been if it had been done by people who knew what they were doing.

Same story with the IDEs. Borland's IDEs were the last products to properly compete with Visual Studio. And I am including Eclipse in my considerations.


OP, the choice between C# and Java is more religious than technical. Both are available free (as in beer), the C# compiler and .Net Core are going open. I do have a preference, but it is up to you. Java is probably the next Cobol if you are job-for-life minded. You can ignore C++ - it's just an unpleasant chapter in the history books now.

And I wouldn't bother with the "Gang of Four" Design Patterns book right now - it's no use to a beginner.

GnuBee

1,272 posts

215 months

Wednesday 18th March 2015
quotequote all
grumbledoak said:
Same story with the IDEs. Borland's IDEs were the last products to properly compete with Visual Studio. And I am including Eclipse in my considerations.

You can ignore C++ - it's just an unpleasant chapter in the history books now.
Borland really did get it right didn't they - how did it all go so wrong (for them)!

Quoted the 2nd part for posterity; "it's just an unpleasant..." has to be the very definition of classic British understatement.

dcb

5,834 posts

265 months

Wednesday 18th March 2015
quotequote all
grumbledoak said:
Remember Sun workshop? Quite. Sun were rubbish at compilers and didn't get any better at virtual machines.
Thanks for that. I always found the Sun C and C++ compilers to be best in class.

grumbledoak said:
You can ignore C++ - it's just an unpleasant chapter in the history books now.
Blimey. What a novel opinion.

grumbledoak said:
And I wouldn't bother with the "Gang of Four" Design Patterns book right now - it's no use to a beginner.
Now that I can agree with.

grumbledoak

31,532 posts

233 months

Wednesday 18th March 2015
quotequote all
GnuBee said:
Borland really did get it right didn't they - how did it all go so wrong (for them)!
Sadly they had a market Microsoft wanted. I've not seen anyone survive that. A shame, Delphi was great. But the Unis were churning out grads who only knew C and C++, often only on Unix. And so the march of "Worse is Better" continues. Hey-ho.

cornet

1,469 posts

158 months

Wednesday 18th March 2015
quotequote all
This appears to have gone off topic somewhat. So bringing this back on track and playing some code golf to try and help the OP.

I've done very little python so their might not be some common idioms I'm using but here goes.

The goals when writing this were:

  • Make sure functions have a clear purpose and are reusable
  • Catch errors as early as possible in the program
  • Keep logic separate from presentation


#!/usr/bin/env python

def calc_area(base, height):
"""Calculate and return the area of a triangle give the base and height"""
return base * height / 2


def yes_response(resp):
"""Check if string is a yes response and return true if so"""
YES_RESPONSES = ["y", "yes"]

resp = resp.lower()

if resp in YES_RESPONSES:
return True
else:
return False


def main():
while True:
print "\nThis programme calculates the area of a triangle\n"

try:
base = float(raw_input("Enter Base Value: "))
height = float(raw_input("Enter Height value: "))
except ValueError:
print "\nPlease enter numbers and not letters"
continue # Skips to next iteration of the loop

area = calc_area(base, height)

print "The working out is, %d x %d / 2" % (base, height)
print "Base x height =", base * height
print "Once divided by 2 the area of your triangle is", area

if not yes_response(raw_input("\nDo you wish to continue? Y/N ")):
break # Exits the loop

print "\nGood Bye\n"


if __name__=="__main__":
main()

zippy3x

1,315 posts

267 months

Wednesday 18th March 2015
quotequote all
grumbledoak said:
GnuBee said:
Borland really did get it right didn't they - how did it all go so wrong (for them)!
Sadly they had a market Microsoft wanted. I've not seen anyone survive that. A shame, Delphi was great. But the Unis were churning out grads who only knew C and C++, often only on Unix. And so the march of "Worse is Better" continues. Hey-ho.
Sorry, but I'm going to have to disagree with that. C# is essentially Delphi using a c based language. I'd been doing Delphi for about 7 years when .Net came out. It was remarkably similar to Delphi from an architectural point of view, which is wholly unsurprising when you look at the history of the two.

Borland were in serious financial difficulties before .Net, and Microsoft propped them up with a cash injection. Conspiracy theorists might have a bit of a field day with this, but after a couple of years Microsoft ended up employing Anders Hejlsberg, the guy who basically invented Delphi.

Guess what he's been doing for the last 15 years. Chief Architect of .Net

I've been doing .Net since v1 and it's a brilliant piece of software engineering.

I can see why people might not want to limit themselves to one platform, but the language and framework really are first class. There's no hint of "Worse is better". In this case "Better is better".

TheExcession

11,669 posts

250 months

Wednesday 18th March 2015
quotequote all
An interesting thread here.

My feeling as a coder has always been "what is the least number of keys that I have to press to get the job done".

VB was always so easy to write and read, but so many keys to press in order to get the job done.

C was always short-hand in terms of typing assembler but there were so many things you had to think about and code around.

Java was ace, so long as you could accept you were never going to get hold of a pointer, every thing was there for you.

Every other language has just about becoming a lesson in learning the libraries and the wrappers around these libraries.

Consider PHP or JScript; (interpretted) programming languages, check. Moving towards OO models, yup. How many attempts out there to get your PHP compiled?

I'm a big fan of Java, it is as close to C as you could wish to get, and C is as close to machine-code as you would wish to get.

Now having said all that, as a coder it really boils down to the way you can think, thinking being the way you can visualise data, where it needs to be and what you want to do with it.

ASM is the bottom line for talking to hardware.

C is great for talking to devices/hardware, the raw skipping about the memory addresses is orgasmic, but it lacks the marshalled interface controls.

C++ a great attempt at finally encapsulating the data into 'closed' classes - if you want to change some data, then you have to request that change.

Java just follows on from everything above, but allegedly is better because all the memory management is better....

SO BACK TO THE OP; GOOD CODE.

TL;DR

Good code is independent of language. Good code isn't full of comments, GOOD CODE is poetry with consideration of all the noise that the code has to listen to.

Good code is offered an input, good code sanitises its output, good code does only what it is supposed to do.


For example to the OP what if I send -7 and +2345?











grumbledoak

31,532 posts

233 months

Wednesday 18th March 2015
quotequote all
zippy3x said:
Sorry, but I'm going to have to disagree with that. C# is essentially Delphi using a c based language. I'd been doing Delphi for about 7 years when .Net came out. It was remarkably similar to Delphi from an architectural point of view, which is wholly unsurprising when you look at the history of the two.
Forgive me if I wasn't clear. The "Worse is Better" was a reference to the universities' C/C++ on Unix grads leading to Visual Studio C/C++ prevalence in industry over Borland's Delphi. I think C# is great. It is jut a shame we are stuck with the curly brackets.

/digression (I hope).

ATG

20,575 posts

272 months

Wednesday 18th March 2015
quotequote all
Not sure why one would say it was "stepping up" to move from Python to Java or C#. I know plenty of professional developers who have stepped theother way. Compared to those languages and C++, along with python all of which are fundamentally similar, python has a clean syntax and is extremely terse. It is one of the key strategic languages used by my employer and several of our major competitors building major in house systems.

TheExcession

11,669 posts

250 months

Wednesday 18th March 2015
quotequote all
zippy3x said:
I can see why people might not want to limit themselves to one platform, but the language and framework really are first class. There's no hint of "Worse is better". In this case "Better is better".
You see, the above is written by a programmer, for years now I've always stated that programming/coding is like writing poetry. A good poet/coder will see the requirements in their head and describe the whole lot (in code) and make it work.