Python parenthesis question - Page 2 - Overclock.net - An Overclocking Community

Forum Jump: 

Python parenthesis question

Reply
 
Thread Tools
post #11 of 18 (permalink) Old 01-30-2019, 10:02 AM
Some call me... Bifford
 
BFRD's Avatar
 
Join Date: Dec 2004
Location: Carrollton, TX
Posts: 5,264
Acecool has certainly hit the nail on the head with what he (or she) posted.

Personally, I very rarely comment anything I write. I believe code should be self-documenting. I am now looking at code that I wrote (for OCN) over a decade ago and know exactly what it does. The fact that I don't even remember writing it is irrelevant! I am just very particular about how I name things. Having a very structured naming convention is essential for self documenting code. Be VERY deliberate in every variable and method name. Make sure that scalar functions are named in a singular fashion. Conversely, if something is plural it should return some sort of array. Also, be wary of long methods. If a method (or function) won't fit on a single screen view, it is probably too big and needs to be split up. Granted there are a lot of variables in that last statement, but the general idea is simplicity. Keep your methods and functions to a single purpose. The last thing you want is a swiss-army-knife function.

Practice is also a good idea. Go back and take a look at some of your previous code and see where it can be improved.



BFRD is offline  
Sponsored Links
Advertisement
 
post #12 of 18 (permalink) Old 01-30-2019, 08:53 PM
RIP OCN
 
Join Date: Dec 2014
Posts: 564
Rep: 25 (Unique: 19)
Quote: Originally Posted by BFRD View Post
Acecool has certainly hit the nail on the head with what he (or she) posted.

Personally, I very rarely comment anything I write. I believe code should be self-documenting. I am now looking at code that I wrote (for OCN) over a decade ago and know exactly what it does. The fact that I don't even remember writing it is irrelevant! I am just very particular about how I name things. Having a very structured naming convention is essential for self documenting code. Be VERY deliberate in every variable and method name. Make sure that scalar functions are named in a singular fashion. Conversely, if something is plural it should return some sort of array. Also, be wary of long methods. If a method (or function) won't fit on a single screen view, it is probably too big and needs to be split up. Granted there are a lot of variables in that last statement, but the general idea is simplicity. Keep your methods and functions to a single purpose. The last thing you want is a swiss-army-knife function.

Practice is also a good idea. Go back and take a look at some of your previous code and see where it can be improved.
For very basic coding this is the case, yes
anti-clockwize is offline  
post #13 of 18 (permalink) Old 01-31-2019, 05:54 AM
Some call me... Bifford
 
BFRD's Avatar
 
Join Date: Dec 2004
Location: Carrollton, TX
Posts: 5,264
Quote: Originally Posted by anti-clockwize View Post
For very basic coding this is the case, yes
I think having a rigid coding discipline only gets more important as code complexity rises. There are more facets that must be considered, but the fundamentals don't change. What aspects of the principles being discussed don't apply to complex code? I am not trying to start anything, I just want to understand. This is a forum and we must be able to discuss both areas of agreement and disagreement.



BFRD is offline  
Sponsored Links
Advertisement
 
post #14 of 18 (permalink) Old 02-01-2019, 05:40 AM
New to Overclock.net
 
Join Date: Jun 2018
Location: Hendersonville, NC
Posts: 100
Rep: 0
Self-documenting code.. I like that term!

Even still - it doesn't hurt to put a comment above a function to explain what it does - also with programming I see too many people over complicate things. They write functions 10,000 lines long and then they end up repeating code 10, 20 times... Instead of that, make yourself building blocks. Simplify things - and I personally don't like to repeat code if I can help it. I turn repetitions into helper functions / building-blocks with a very clear purpose and use those throughout.

I also translate awkward functions - for instance... drawRect( x, y, x2, y2 ) is much more readable using ( x, y, w, h ) and the math becomes a lot less repetitive, less clunky and easier to read.


So there is a lot you can do to reduce the number of comments required to explain your code such as properly using Enumeration, Constants, variable naming, function / class naming, simplifying your code, making building blocks, not repeating code which could be a function, etc..

Also, by reducing the repetition - if that code chunk has an error, you only need to fix it in 1 place instead of every place used - but for this to work the function must be very clear.. For instance, say math.abs had an error where it did |x|+1 or whatever... Imagine having to snake through code to find all of those that shouldn't be there vs those that should... So we know what absolute value does, so if we see math.abs( x ) + 1 then we can see that it looks intended.




Anyway - if you have a strong coding standard and you apply it to everything, eventually you don't need to worry about it no matter how large or small the project is. And, as you become more familiar with coding standards used by other businesses, you can easily adapt if you change jobs. Ensuring the code looks like it was written by a single person or a very well orchestrated team who are all on the same page makes the code a lot easier to maintain because you don't have to discern between different styles making you wonder what to use in each segment of code you're working on, and so on. And, by making it easier, it saves the company money and gives them more of a reason to hold on to you. If the company doesn't have a system like this and you bring it to them, I'm sure they'd be grateful. If I knew nothing of coding and I was told it would save money because less time would be spent trying to figure out what person a does vs person b - I'd be all for it. In a business sense, some may not be - if the code or system is old and there is no major benefit right away, some people may just say leave it, or establish a coding standard, distribute it, and as you work on the code convert the things you work on until it is fully converted - or when you aren't busy on something else.
Acecool is offline  
post #15 of 18 (permalink) Old 04-30-2019, 03:32 PM
New to Overclock.net
 
Pinnacle Fit's Avatar
 
Join Date: Mar 2015
Posts: 1,007
Rep: 23 (Unique: 17)
Quote: Originally Posted by PhotonFanatic View Post
That makes sense. But could it be that they don't want the space taken up? Or maybe the space that a bunch of comments would take up, is negligible.


If taken literally, I think that’s bunk...

Ideal code imo is code that uses OO principles and eliminates redundant code wherever possible. This has nothing to do with commenting, particularly if you’re writing enterprise level code.

I could potentially see how it might be taken to mean that the comments get aggregated separately, like in the form of a java doc for instance.


Sent from my iPhone using Tapatalk

i9-9900k OC to 5.0GHZ @ 1.285V Delidded/Direct Die Cooled
Gigabyte Z390 Aorus Master
GTX 1080Ti FE
Lian Li O11 Dynamic, white

Pinnacle Fit is offline  
post #16 of 18 (permalink) Old 04-30-2019, 03:35 PM
New to Overclock.net
 
Pinnacle Fit's Avatar
 
Join Date: Mar 2015
Posts: 1,007
Rep: 23 (Unique: 17)
Quote: Originally Posted by anti-clockwize View Post
Because good code can be readable to someone who has never seen a coding language before. Comments are still extremely useful, and you should never try to have zero comments in your code, but reducing comments is good.



Take this code for example:

Code:
class Helpers {



    void incrementByOne(int i) {

        i = i + 1;

    }



    void decrementByOne(int i) {

        i = i - 1;

    }



}



class Main {

    static void main() {

        int i = 0;

        Helpers helpers = new Helpers();

        

        helpers.incrementByOne(i);

        helpers.incrementByOne(i);

        helpers.decrementByOne(i);

    }

}


Adding comments is not needed, see below:



Code:
class Helpers {



    void incrementByOne(int i) {

        i = i + 1;  // increments integer i by 1

    }



    void decrementByOne(int i) {

        i = i - 1; // decrements integer i by 1

    }



}



class Main {

    static void main() {

        int i = 0; // start with i = 0

        Helpers helpers = new Helpers();

        

        helpers.incrementByOne(i); // increment i by 1

        helpers.incrementByOne(i); // increment i by 1

        helpers.decrementByOne(i); // decrement i by 1

    }

}


as you can see, the added comments have not helped, only cluttered the code, and forced more reading.

Basic example but you can probably see the point


It depends what language it’s in, and what the function is doing. Simple increments and basic loops need no comments, but when you’re creating inception level ‘for loops’, or passing loops as parameters, you had better have some comments in there.


Sent from my iPhone using Tapatalk

i9-9900k OC to 5.0GHZ @ 1.285V Delidded/Direct Die Cooled
Gigabyte Z390 Aorus Master
GTX 1080Ti FE
Lian Li O11 Dynamic, white

Pinnacle Fit is offline  
post #17 of 18 (permalink) Old 04-30-2019, 04:01 PM
New to Overclock.net
 
Join Date: Dec 2011
Location: 7200 ft above sea level
Posts: 2,709
Quote: Originally Posted by PhotonFanatic View Post
I've heard that "ideal code" doesn't have any comments. Sounds like a recipe for disaster. Why would ideal code have no comments in it? example: #A comment like this in the code for people to read if necessary.

I work in a large mainframe automation environment with lots of REXX, some of it has been running for a decade or longer. This no comments thing is older than dirt and about as useful when you get woke up in the middle of the night to fix something.

Quote:I'm gonna throw in my 2 cents. Not because I'm an expert but because I have a keyboard.


bfromcolo is offline  
post #18 of 18 (permalink) Old 04-30-2019, 04:08 PM
New to Overclock.net
 
Pinnacle Fit's Avatar
 
Join Date: Mar 2015
Posts: 1,007
Rep: 23 (Unique: 17)
Quote: Originally Posted by bfromcolo View Post
I work in a large mainframe automation environment with lots of REXX, some of it has been running for a decade or longer. This no comments thing is older than dirt and about as useful when you get woke up in the middle of the night to fix something.


They’ve been trying to get me to learn cobol and mainframe code for awhile now...

It’s problematic enough having to read my own python code when Im in a rush and don’t comment...I can’t imagine having to read uncommented mainframe code someone else wrote.


Sent from my iPhone using Tapatalk

i9-9900k OC to 5.0GHZ @ 1.285V Delidded/Direct Die Cooled
Gigabyte Z390 Aorus Master
GTX 1080Ti FE
Lian Li O11 Dynamic, white

Pinnacle Fit is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off