Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split code over multiple lines #7

Open
dkfellows opened this issue Feb 19, 2024 · 0 comments
Open

Split code over multiple lines #7

dkfellows opened this issue Feb 19, 2024 · 0 comments

Comments

@dkfellows
Copy link

In my experience it's best to split code over several lines and use {curly braces} even in cases where they're technically not necessary. It's longer but really boosts the readability.

For example, consider this line:

if (j0s != 0) j0s = jj0* log2(jj0); if (j1s != 0) j1s = jj1* log2(jj1); if (j2s != 0) j2s = jj2* log2(jj2); if (j3s != 0) j3s = jj3* log2(jj3); //Gives 0 in entropy!

It would be way better written as this:

     if (j0s != 0) {
         j0s = jj0* log2(jj0);
     }
     if (j1s != 0) {
         j1s = jj1* log2(jj1);
     }
     if (j2s != 0) {
         j2s = jj2* log2(jj2);
     }
     if (j3s != 0) {
         j3s = jj3* log2(jj3);
     }
     //Gives 0 in entropy!

Yes, it takes more lines of code, but there isn't a global newline shortage.

Some libraries can do more mnemonic approaches, by providing higher level concepts like vectors and quaternions and so on. I don't know if any of those would be applicable to your code; adopting one of those would certainly be a lot of work.

@dkfellows dkfellows mentioned this issue Feb 19, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant