JavaRush /Java Blog /Random EN /Coffee break #65. 5 useful tips for maintaining someone e...

Coffee break #65. 5 useful tips for maintaining someone else's code. Java Collection and answers to the most common interview questions

Published in the Random EN group

5 helpful tips for maintaining someone else's code

Source: Dev.to Is someone leaving your team? Are you being passed legacy code? Are you changing jobs? These are just a few of the reasons why developers may find it difficult to maintain someone else's code.Coffee break #65.  5 useful tips for maintaining someone else's code.  Java Collection and answers to the most common interview questions - 1Working with code that someone else wrote can lead to some problems. The code may refer to technology that is unfamiliar to you. Perhaps this code needs to be improved. Or you may need to urgently fix a bug found in an old codebase. To better deal with such challenges, it is important to be aware of the likely problems and have a systematic way of dealing with them. If you are a professional developer, then you have probably come across similar situations. But if not, trust me, it's only a matter of time! In this article, we will look at a few tips that can make it easier to work with legacy code.

1. Host a code transfer session with the author

If you still have contact with the person who wrote this code, have a transfer session with them. Or two! Or maybe even three. Use these transfer sessions as a way to speed up your codebase familiarization process. Otherwise, you will have to deal with everything yourself, and it will take more time. The developer who created this code can tell you about its logic, as well as point out hidden secrets. Before the transfer session, be sure to review the code yourself and write down any questions you have. This way, when reviewing the code together with its author, you will not forget to clarify all the necessary details. Of course, keep notes during the joint sessions. Sharing code with its author is probably the fastest way to get familiar with a new codebase. Therefore, if you still have the opportunity to contact this developer, be sure to use it. If your colleague is about to leave the company, make sure that the code transfer takes place before he leaves. There are other situations where it's useful to walk through a codebase with someone who knows it well. For example, if you change teams, you need one of your new colleagues to tell you about the structure of the code. This will speed up your familiarity with the new code.

2. Read all available documentation

Documentation is all available documents related to the project for which the code was written. Project specifications? Program specs? README files? Read it all. The time spent reading these documents will save you time in the future. Instead of spending a huge amount of time trying to figure out why the code was implemented the way it was, just read the README. Instead of trying to understand the purpose of sections of code on your own, you can learn all this in software specifications. Reading the documentation is important, even if you've had a code-sharing session with its author. All these documents are written for a reason. And transmission sessions can hardly cover everything. Reading documentation is especially important for legacy software. We are talking about cases where you do not have a chance to meet with the author of the code. Since no transfer session is possible, all you have is the source code and any available documentation. One final piece of advice on this: remember that README files are called READMEs for a reason (read me).

3. Run the program and work with it

This advice is to be taken literally. Run the code and try using the application as intended. This way you can better understand what it does. This may seem obvious, but practice shows that many people ignore this stage. Many developers make the mistake of figuring out a program only from its code. Be sure to run the app and rate it as a normal user. This will give you a general understanding of what the application actually does, as well as an idea of ​​its features and limitations. As a result, it will be easier for you to understand the code. This also applies to software written for specific hardware or platforms. Access everything you need and run the app on these platforms. If you're trying to fix a bug, running the code is the logical first step in the debugging process.

4. Test, test, test!

If you're dealing with an unfamiliar codebase, make sure you have tests in place to verify that the program works before making any changes. Tests will help confirm that nothing is broken after making changes. Safely making changes can be even more difficult than just familiarizing yourself with someone else's code. And assuming that you were only given code to fix bugs and make improvements, then having tests is critical. If there are no tests, try writing them yourself. These can be unit tests, smoke tests, manual tests, or automated tests. Any testing is usually better than no testing.

5. Accept the challenge

When you take on someone else's code, try to take it as a personal challenge. First, you will improve your ability to read and interpret other people's code. This will allow you to better capture the ideas of other developers. With practice, you'll be able to handle program maintenance tasks more easily. Second, by maintaining other people's code, you can learn useful practices and techniques that will improve your own code. Reading unfamiliar code can be considered an indirect learning method. Just be careful - choose only those methods that are considered advanced, and avoid not so good ones. Knowing that you will ultimately benefit from this experience should motivate you. So do not be afraid and accept the challenge thrown to you!

Conclusion

Maintaining code written by someone else certainly comes with a lot of problems. But that's just another side of being a developer that we'll all have to deal with at some point. And you can be much better prepared to work with someone else's code if you not only follow the tips in this article, but also develop your own systematic methods when working with an unfamiliar codebase.

Java Collection and answers to the most common interview questions

Source: DZone To solidify your knowledge of Java, let's take a look back at the Java Collection and answer some of the frequently asked questions that beginners get asked in technical interviews.

Collections in Java

Java Collection is a framework that supports the architecture for storing and managing groups of objects. The collection structure with classes and interfaces is defined in JDK 1.2. The two main interfaces of the Java Collection classes are the Collection interface (java.util.Collection) and the Map interface (java.util.Map). Java collections can perform data operations such as searching, sorting, inserting, manipulating, and so on. The Java Collection Framework provides interfaces such as Set, List, Queue, Deque, and classes such as ArrayList, Vector, LinkedList, HashSet, PriorityQueue, TreeSet, and LinkedHashSet.

The need for a separate collection structure

If we don't use a collection structure, the standard methods for grouping Java objects are arrays, vectors, or hash tables. All of them do not have a common interface. All their implementations are defined independently and there is no correlation between them. Thus, it becomes very difficult to remember all the different methods, syntax, and constructors present in different classes. For example, to add an element to a vector, we use the addElement() function , while to add an element to a Hashtable, we use the put() function . The advantages of the collection system:
  • Reduces the burden in the programming process: the developer can focus more on making the best use of the collection rather than on the design of the collection. This helps in implementing the abstraction.
  • Improves program execution speed: Collections provide a high-performance implementation of data structures, which improves performance.
Since Java is a widely used programming language, many companies create their products in this language. So let's go through the basic Java questions that will help you succeed in your interview.

Some frequently asked interview questions for newbies

Question 1. What is a framework in Java?

Answer: A framework (structure) is a set of classes and interfaces that support a ready-made architecture. An optimal object-oriented design always includes a framework with a set of classes so that all classes perform the same tasks.

Question 2. What is the collections framework in Java?

Answer: The Java Collections Framework is a set of interfaces and classes that help you store and process data efficiently. Java Collection Framework has interfaces like Set, List, Queue, Deque and classes like ArrayList, Vector, LinkedList, HashSet, PriorityQueue, TreeSet and LinkedHashSet.

Question 3: What are the differences between ArrayList and Vector in Java Collection Framework?

Answer: ArrayList:
  • Doesn't sync.
  • Can increase its size by 50% of the size of the array.
  • Not thread-safe.
  • It is not a legacy class.
Vector:
  • Synchronized.
  • Can double its size.
  • Thread safe.
  • inherited class.

Question 4. What are the differences between Iterator and Enumeration?

Answer: Iterator:
  • It can work with both obsolete and non-obsolete elements.
  • It is slower than Enumeration.
  • It can perform remove operations while traversing the collection.
  • He is unfailing.
Enumeration (Enumerated type):
  • Can only navigate through obsolete elements.
  • It's faster than an iterator.
  • It can only perform traverse operations on the collection.
  • He's not flawless.

Question 5. What is the difference between LinkedList and ArrayList?

Answer: ArrayList:
  • This class implements the list interface.
  • This class uses a dynamic array to store elements.
  • Insertion and deletion operations are O(1) at best and O(n) at worst. A lookup operation (that is, accessing an element at a specific index) takes O(1) time.
  • ArrayList performs better when storing and accessing data.
LinkedList:
  • This class implements both the list interface and the deque interface.
  • This class uses a doubly linked list to store elements.
  • Insertion and deletion operations give O(1) performance. The search operation (i.e. accessing an element at a specific index) takes O(n) time.
  • LinkedList performs better when manipulating stored data.

Question 6. Explain the difference between poll() and remove() in a Queue interface method.

Answer: Both methods return and remove the header of the queue. They differ in their behavior only when the queue is empty; remove() throws an exception, while poll() returns null for an empty queue.

Question 7. What are the differences between Comparable and Comparator.

Answer: Comparable:
  • Provides the compareTo() method for sorting elements.
  • Present in the java.lang package.
  • The sorting logic must be in the same class whose object we want to sort.
  • It provides a single sort sequence.
  • The actual class has changed.
Comparator:
  • Provides a compare() method for sorting elements.
  • Present in the java.util package.
  • The sorting logic should be in another class in order to write different sorts based on different object attributes.
  • It provides multiple sort sequences.
  • The actual class does not change.

Question 8. What is a stack in terms of computer memory?

Answer: The stack is a special area of ​​computer memory that stores temporary variables created by a function. On the stack, variables are declared, stored, and initialized at run time.

Question 9: Make a list of collection views in the Map interface.

Answer: Collection view methods allow you to view map as a collection in three ways:
  • Key-set view: The set of keys contained in the Map.
  • Value-set view: The collection of values ​​contained in the Map. This collection is not a set because multiple keys can appear in the same value.
  • Entry-set view: The set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map.Entry, which is the type of the elements in this set.

Question 10. Define EnumSet.

Answer: It is a Set implementation that can be used with enumerated types. All elements must come from the same enumerated type, either explicitly or implicitly specified. Doesn't sync. NULL keys are not allowed.

Question 11: What methods make an assembly thread-safe?

Answer: Methods:
  • Collections.synchronizedList(list);
  • Collections.synchronizedMap(map);
  • Collections.synchronizedSet(set).

Question 12. What are the differences between Queue and Deque?

Answer: Queue:
  • Also known as a one-way queue.
  • Elements in the queue (Queue) are added or removed from one end.
  • Less versatile.
deck:
  • Also known as a deque.
  • Elements in the queue are added from either end and can be added and removed from both ends.
  • More versatile.

Question 13. What are the differences between hashmap and hashtable?

Answer: Hashmap:
  • Unsynchronized, not thread safe.
  • Inherits the AbstractMap class .
  • Allows one null key and multiple null values.
  • Bypasses the iterator.
Hashtable:
  • Synchronized, thread safe.
  • Inherits the Dictionary class .
  • Does not allow null key or null value.
  • Passes enumerator and iterator.

Question 14. What is an iterator?

Answer: Iterator() is an interface that provides methods for iterating over a Collection. It provides a generic traversal using collection elements and an implementation of the iterator design pattern.

Question 15. What is a Navigable Map?

Answer: The NavigableMap interface is part of the Java Collection Framework and belongs to the java.util package. It is a subinterface of SortedMap that provides convenient navigation methods such as lowerKey, floorKey, ceilingKey, and higherKey. It also helps to create another Map from an existing Map.

Question 16. What is peek() in the queue interface?

Answer: Peek() returns the start of the Queue. It does not remove any elements. It returns null when the queue is empty.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION