+ 1

Styling code

Just wondering if am on the right track. The thing that you need to improve on is the naming of this code Thank you // Initialise values String s1 = "Hello"; String s2 = "Helloi"; boolean strings_match = true; // Iterate over the input strings by character, checking if they match for (int i = 0; i < a.length; i++) { if (a.charAt(i) != b.charAt(i)) { // Mark that the strings are different and stop strings_match = false; break; } } // Print result println(strings_match); What needs to be improved about the styling of this code? The options are below Indentation Spacing Comments Naming

24th Oct 2021, 6:42 AM
Danika
6 Answers
+ 3
The program looks fine to me. I generally don't put too much of thought into styling of a program and just make sure it is readable and abides the overall convention of the project that I might be working on, for that also I tend to use tools like clang-format and let the editor take care of styling while I focus more on the actual implementation.
24th Oct 2021, 7:19 AM
Arsenic
Arsenic - avatar
+ 3
You can save your code on sololearn playground and attach the link here – this way code is easier to read and other users can test it! The current code is quite short and simple. The comments are useful for learning but as you advance, you won't comment the "obvious". Spacing and indentation are okay for Java (?, please tag the language)
24th Oct 2021, 7:56 AM
Lisa
Lisa - avatar
+ 1
May be the indentation
24th Oct 2021, 7:11 AM
Atul [Inactive]
+ 1
Danika Indentation doens't matter in Java but yes if you proper indent your code then readability of code would be better. //Less readable if(true) { println("abc"); } else { println("123"); } //More readable if(true) { println("abc"); } else { println("123"); } Space also matters for readability. For example a+b=10; //less readability a + b = 10; //this looks more readable Comments are help to others to understand code. So if you put proper comments then people can easily understand what is going on in the code. Naming Convention - we should follow naming convention because we can understand what is class, what is a variable, what is a constant.
24th Oct 2021, 9:31 AM
A͢J
A͢J - avatar
0
Thanks everyone, for your help
24th Oct 2021, 10:43 AM
Danika
0
You should use readable variable naming if you are not looking for a full minified code. What is a? What is b? Shoudn’t they be s1 and s2? In this case if b word is longer than a, but has the same letters than a until a has no more chars then your code prints true but it should print false. Example: a = “hi” b = “hi2”
25th Oct 2021, 7:37 AM
Guillem Padilla
Guillem Padilla - avatar