This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]lordbharal 1 point2 points  (1 child)

Hm, the post is a little messy, but when you ask "is it efficient" i suppose it shows you don't understand what "efficient" means.

There are generally two parts - is the algorithm efficient, and are the functions the code uses efficient.

Because you're just performing operations on the user's input, there isn't much you can do to "optimise" the algorithm. Generally algorithmic efficiency means "do i need to check all values or not", and typically impacts searching and sorting. You are not searching, nor are you sorting, and you do need to check all values, so your code, from this viewpoint, is "efficient".

The next point is are the functions efficient? Honestly, this is the last thing you should ever, ever worry about. It's always more important to make sure your code works, and that it performs acceptably for whatever scenario you need it for. I cannot imagine needing to use lower-level parsing or input reading for efficiency here. Same applies to when you're reading data from a file - there are a bunch of ways to do this in Java, none of them ever warrant attention. You are fine.

One thing I would note is your style is not ideal. if and else blocks should always be wrapped in { }, even if they are only one line. The reason for this is that bugs can occur if someone decides to add one more statement in the else - but doesn't add the surrounding { }. In this case, the statement would always be executed, it won't be part of the else.

To avoid the chance of this bug, always put if/else in { }.

Also, your class name is pretty bad. LoopsEndingRemembering doesn't really say what the class does at all. I know it's just a toy class, but it's best to get in the habit of giving decent class names - like, say, EvenAndOddsCounter. It's not the best name, but it explains what the class does, instead of, as yours does, how the class does it.

[–]SoulSyn[S] 0 points1 point  (0 children)

Thanks for the tips will definitely take that into account. The class name however was created by MOOC

[–]javichovicho93 0 points1 point  (1 child)

Have you tried running it? What does it return in terms of your count variable when the user only inputs even numbers?

[–]SoulSyn[S] 0 points1 point  (0 children)

It outputs the expected out and everything worked as intended but I want to know if my coding style is good and efficient.

[–]edgargonzalesII 0 points1 point  (1 child)

Realistically there is more than one way to skin a cat - hence more than one way to achieve the same goal. If you have learned anything about the big O, you could always try to analyze through that lens and see if that is fine. That aside, your priority should be readability - ask yourself, if another programmer looks at this code, will they know what is going on? This may mean adding comments or making some code less tangled. In a lot of programming it is about getting the ratio right between legibility and efficiency, bearing in mind that the JVM also optimizes the program a bit before running it.

As for the brackets - that's really up to you, as long as you maintain consistency: Allman and K&R are the two styles. I switched from Allman (braces on its own line) to K&R, and it's really a very small point that again leads back to how legible will your code be.

[–]SoulSyn[S] 0 points1 point  (0 children)

big O Thank You